Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem as @skparwal mentioned is that you are trying to insert '' into your primary key. You need to use <code>mysql_error()</code> after your insert to see what errors occur. Have a look at this link on how to do this: <a href="http://php.net/manual/en/function.mysql-error.php" rel="nofollow">http://php.net/manual/en/function.mysql-error.php</a></p> <p>Also, your ajax won't work as expected because you are not returning false for onClick() which means that the form is being submitted each time after the ajax call gets triggered.</p> <p>Furthermore, you should be moving to mysqli or PDO because mysql is deprecated.</p> <p><strong>Edit</strong></p> <p>Try this <strong>but it is not meant to work or be a final implementation,</strong> it is just to get you on the right path. It is untested so there might be some errors:</p> <p>Change your javascript to this:</p> <pre><code>$(function(){ $('#submit').click(postData); }); function postData() { var fName=$("#fName").val(); var lName=$("#lName").val(); var city=$("#city").val(); var data="fName="+fName+"&amp;lName="+lName+"&amp;city="+city+"&amp;submit=submit"; $.ajax({ type:'POST', url:'employeeajaxcodebehind.php', data:data, success:function(data){ $("#results").html(data); } }); return false; } </code></pre> <p>Change your PHP to this:</p> <pre><code>if(isset($_POST['submit'])) { $fName=$_POST['fName']; $lName=$_POST['lName']; $city=$_POST['city']; $Query="INSERT INTO employee(firstname,lastname,city) VALUES ('$fName','$lName','$city')"; if( !( $Result=mysql_query($Query) ) ) exit( '@todo need to check for error here' ); viewRecord(); } </code></pre> <p>Change your HTML to this:</p> <pre><code>&lt;form method="POST"&gt; Firstname: &lt;input type="text" id="fName" name="fName"&gt;&lt;br&gt; Lastname: &lt;input type="text" id="lName" name="lName"&gt;&lt;br&gt; City: &lt;input type="text" id="city" name="city"&gt;&lt;br&gt; &lt;input type="submit" id="submit" name="submit"&gt; &lt;/form&gt; </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