Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL INSERT automatically adding duplicate rows
    text
    copied!<p>I'm trying to update a database using html form + jquery + php</p> <p>When I click on the Submit button on the form it works but it is adding <strong>two same records</strong> in the database for each click.</p> <p>I can't find out what is going wrong and therefore I'm posting the whole script I'm using here. If anyone can find the problem then please point it out.</p> <p>Here are the scripts I'm using</p> <p><strong>HTML</strong></p> <pre><code>&lt;div id="result" class="results"&gt;&lt;/div&gt; &lt;form id="person-form" class="person-form" method="post"&gt; &lt;fieldset&gt; &lt;legend&gt;&lt;strong&gt;Add a new member&lt;/strong&gt;&lt;/legend&gt; &lt;table&gt; &lt;tr&gt;&lt;td&gt;&lt;label for="Name" &gt;Name&lt;/label&gt;&lt;/td&gt; &lt;td colspan="2"&gt;&lt;input type="text" name="name" value="Enter the Name, 55 char max." /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;label for="email"&gt;Email&lt;/label&gt;&lt;/td&gt; &lt;td colspan="2"&gt;&lt;input type="text" name="email" value="Enter the email" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;label for="subscribe"&gt;Subscribe&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="checkbox" name="subscribe" value="Yes" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="submit" value="Add member" id="add-member" class="add-member"/&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;div style="clear:both;"&gt;&lt;/div&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p><strong>jQuery</strong></p> <pre><code>$("#add-url").live("click", function() { var name = $('input[name=name]').val(); var email = $('input[name=email]').val(); var subs = $('input[name=subscribe]:checkbox:checked').val(); var data = 'name='+name+'&amp;email='+email+'&amp;subs='+subs; $.post(add_member_script.ajaxurl, data, function(data) { $('#results').html(data); }); return false; }); </code></pre> <p><strong>And PHP</strong></p> <pre><code>$connect = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$connect){ die('Could not connect: ' . mysql_error()); } mysql_select_db(DB_NAME, $connect); if (empty($_POST['name']) || empty($_POST['email'])){ echo 'enter some value'; } else { if($_POST['subs']== 'yes') { $sql="INSERT INTO $table (name, email, subs) VALUES ('$_POST[name]','$_POST[email]','$_POST[subs]')"; } else { $sql="INSERT INTO $table (name, email) VALUES ('$_POST[name]','$_POST[email]')"; } if (!mysql_query($sql,$con)){ mysql_error(); } else { list_links_table (); } mysql_close($con); die(); } </code></pre>
 

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