Note that there are some explanatory texts on larger screens.

plurals
  1. PODuplicated entries to mySQL using jQuery ajax and PHP
    primarykey
    data
    text
    <p>I am using jQuery (version 1.8.1) with PHP (version 5.3) to submit a form adding an entry into a mySQL database, what is happening is on the first submit everything is fine but for each subsequent submission without a page refresh it adds an additional entry. </p> <p>In addition I'm also using Bootstrap (version 2.1.1) and the upload widget from Jasny for Bootstrap (version j1a) in the UI. I have not yet connected the upload widget to the processing or submit as I detected the duplication problem when I was implementing it. </p> <p>Please note that this is a proof of concept system so the code is rough as I'm not going to invest in cleaning it up until the project is confirmed. Due to this, you will notice some inline mySQL queries, I know that this isn't the best way to do it however it works for the purpose of demonstration and will be cleaned up later. Also as a POC system it is on an internal server currently, I can share the code but cannot show an example site at this time unfortunately. </p> <p>Now back to the issue, as an example, the first post for "Company 1" has 1 record added for "Company 1", the second record for "Company 2" adds 2 records for "Company 2", the third record for "Company 3" adds 3 records for "Company 3" and so on. If I reload the form page in any way (refresh or a new request) the problem restarts from the first submission. </p> <p>I am using jQuery <code>serialize</code> with <code>ajax</code> to post the data to the PHP processor. I have logged all of the posts being received by the processor and I see the processor is receiving multiple records from the form, I thought it may have been caused by a <code>foreach</code> loop in the PHP but this is not the case. </p> <p>I have removed the jQuery functions and it works perfectly each time without any duplicates on normal PHP submit. I have manually processed the entries via jQuery instead of <code>serialize</code> but as there is a dynamic array via PHP I still used <code>serialize</code> on that array, this produced the duplicates as described above. I have searched the issue for a number of days but cannot find anything definitive to clear up the issue, all suggestions on blogs and forums that looked to be related did not work, I have tried around 10-15 different options. </p> <p>The combination of all of this leads me to believe the issue is coming from the jQuery <code>serialize</code> and/or <code>ajax</code> functions but my eyes have become glazed each time I look at this code now. </p> <p>I am also considering placing the form in an external file and reloading it fresh via ajax or cleaning the form setting it back to defaults via jQuery for each new entry required however I do not believe either of these approaches will solve the problem. </p> <p>Any help is greatly appreciated, thanks in advance for the help! </p> <p><strong>jQUERY code</strong></p> <pre><code>&lt;script&gt; $(document).ready(function() { $('.fileupload').fileupload('name:logo'); $('.help-inline').hide(); $("#btn_process").click(function() { $('form').submit(function() { $('.help-inline').hide(); var company_name = $("#company_name").val(); if (company_name === "") { $("div#name_group").addClass("error"); $("span#name_error").show(); return false; } var dataString = $('form').serialize(); $.ajax({ type: "POST", url: "inc/addcompany.php", data: dataString, success: function(html) { if(html === 'success') { $('#message') .addClass("label label-success") .css("margin-bottom","20px") .html("&lt;h3&gt;Login successful&lt;/h3&gt;&lt;p&gt;Company added&lt;/p&gt;") .slideDown(1500, function() {}); } else { $('#message') .addClass("label label-important") .css("margin-bottom","20px") .html("&lt;h3&gt;Error&lt;/h3&gt;&lt;p&gt;There was an error, please check the information and try again&lt;/p&gt;") .slideDown(1500, function() {}); $("div#name_error").addClass("error"); $("span#name_error").show(); $("div#type_error").addClass("error"); $("span#type_error").show(); return false; } } }); return false; }); }); }); &lt;/script&gt; </code></pre> <p><strong>HTML markup</strong></p> <pre><code>&lt;form class="form-horizontal" id="add_company" method="POST" action=""&gt; &lt;fieldset&gt; &lt;div id="message"&gt;&lt;/div&gt; &lt;div id="name_group" class="control-group"&gt; &lt;label class="control-label" for="company_name"&gt;Company name &lt;/label&gt; &lt;div class="controls"&gt; &lt;input type="text" id="company_name" name="company_name" /&gt; &lt;span id="name_error" class="help-inline"&gt;This needs to be more than 3 characters&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="type_group" class="control-group"&gt; &lt;label class="control-label"&gt;Company type &lt;/label&gt; &lt;div class="controls"&gt; &lt;? $sql = "SELECT description,id FROM types ORDER BY description"; $qry = mysql_query($sql) or die("ERROR: could not get company types =&gt; ".mysql_error()); while($company_type = mysql_fetch_array($qry)) { echo ' &lt;label class="checkbox inline"&gt;&lt;input type="checkbox" name="type[]" value="'.$company_type['id'].'" /&gt; '.$company_type['description'].' &lt;/label&gt;'; } ?&gt; &lt;span id="type_error" class="help-inline"&gt;Please select a minimum of 1 type&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="website_group" class="control-group"&gt; &lt;label class="control-label" for="website"&gt;Website &lt;/label&gt; &lt;div class="controls"&gt; &lt;input type="text" id="website" name="website" placeholder="www.something.com" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="logo_group" class="control-group"&gt; &lt;label class="control-label"&gt;Logo &lt;/label&gt; &lt;div class="controls"&gt; &lt;div class="fileupload fileupload-new" data-provides="fileupload"&gt; &lt;div class="fileupload-new thumbnail" style="width: 50px; height: 50px;"&gt;&lt;img src="/img/50x50.png" /&gt;&lt;/div&gt; &lt;div class="fileupload-preview fileupload-exists thumbnail" style="width: 50px; height: 50px;"&gt;&lt;/div&gt; &lt;span class="btn btn-file"&gt;&lt;span class="fileupload-new"&gt;Select image&lt;/span&gt; &lt;span class="fileupload-exists"&gt;Change&lt;/span&gt; &lt;input type="file" /&gt;&lt;/span&gt; &lt;a href="#" class="btn fileupload-exists" data-dismiss="fileupload"&gt;Remove&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;input type="hidden" name="action" value="add_company" /&gt; &lt;button class="btn" data-dismiss="modal" aria-hidden="true"&gt;Close&lt;/button&gt; &lt;button class="btn btn-primary" name="btn_process" id="btn_process"&gt;Save changes&lt;/button&gt; &lt;/form&gt; </code></pre> <p><strong>The PHP processor</strong></p> <pre><code>$error = false; $error_company_name = false; $error_type = false; $error_website = false; $company_name = $_REQUEST['company_name']; $type = $_REQUEST['type']; $website = $_REQUEST['website']; $logo = $_REQUEST['logo']; if(empty($company_name)) { $error = true; $error_company_name = true; } include_once('db.php'); $sql = "SELECT description,id FROM company_types"; $qry = mysql_query($sql) or die("ERROR: could not get company types =&gt; ".mysql_error()); $type_count = 0; while($array = mysql_fetch_array($qry)) { $type_count = $type_count+1; } if($type_count == 0) { $error = true; $error_type = true; } $ic = 0; foreach($_REQUEST['type'] as $item) { $ic = $ic+1; } if($ic == 0) { $error = true; $error_type = true; } if(isset($website) &amp;&amp; $website != ' ') { $url = 'http://'.$website; if(!filter_var($url, FILTER_VALIDATE_URL)) { $error = true; $error_website = true; } } if($error == false) { $sql = "INSERT INTO company_list (name,website,logo) VALUES('$company_name','$website','$logo')"; $qry = mysql_query($sql) or die ("ERROR: could not add company =&gt; ".mysql_error()); $company_id = mysql_insert_id($link); if($company_id == '' || $company_id == null || empty($company_id)) { echo 'fail'; exit; } foreach($_REQUEST['type'] as $company_type) { $sql = "INSERT INTO companies_types (companies_id,type_id) VALUES('$company_id','$company_type')"; $qry = mysql_query($sql) or die("ERROR: could not link company type: =&gt; ".mysql_error()); } echo 'success'; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload