cool_techie Posted February 13, 2011 Share Posted February 13, 2011 Hello....I am using ajax,jquery with php to insert data in db.....but the script is not working. form.html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> label{ display:block; } </style> <script type="text/javascript" src="jquery-1.5.min.js"> </script> <script type="text/javascript"> $(function() { $('#submit').click(function(){ $('#container').append('<img src="ajax-loader.gif" id="loading" alt="image" />'); var name=$('#name').val(); var email=$('#email').val(); var d=$('#d').val(); var m=$('#m').val(); var y=$('#y').val(); var add=$('#add').val(); var phone=$('#phone').val(); $.ajax({ url: 'process.php', type: 'POST', data: 'name=' + name + '&email=' + email + '&d=' + d + '&m=' + m + '&y=' + y + '&add=' + add + '&phone=' + phone, success: function(result){ $('#response').remove(); $('#container').append('<p id="response">' + result + '</p>'); $('#loading').fadeOut(500,function(){ $(this).remove(); }); } }); return false; }); }); </script> </head> <body> <h2>User Registeration</h2> <form action="process.php" method="POST"> <div id="container"> Name:<br> <input type="text" name="name" id="name" /><br> Email:<br> <input type="text" name="email" id="email" /><br> Date Of Birth:<br> <input type="text" id="d" name="d" size="2" /> <input type="text" id="m" name="m" size="2" /> <input type="text" id="y" name="y" size="4" /><br> Address:<br> <input type="text" id="add" name="add" /><br> Phone:<br> <input type="text" id="phone" name="phone" /><br> <input type="submit" name="submit" id="submit" value="GO!" /> </div> </form> </body> </html> process.php My db id named "jquery" and table is "tab" <?php $conns=mysql_connect("localhost","root",""); if(!$conns) echo "error in connection"; mysql_select_db("jquery", $conns); $name=$_POST['name']; $email=$_POST['email']; $m=$_POST['m']; $d=$_POST['d']; $y=$_POST['y']; $add=$_POST['add']; $phone=$_POST['phone']; echo $name,"<br>"; echo $email,"<br>"; echo $m, "<br>"; echo $d,"<br>"; echo $y,"<br>"; echo $add, "<br>"; echo $phone,"<br>"; $query="INSERT INTO tab (name,email,d,m,y,add,phone) VALUES ('$name',$email','$d','$m','$y','$add','$phone')"; if(!mysql_query($query,$conns)) echo "Error"; else echo "DATA inserted"; ?> The output shown is: pulkit [email protected] 1 26 1991 lucknow 987576787 Error For some values i entered. Quote Link to comment https://forums.phpfreaks.com/topic/227513-unable-to-insert-data-in-db/ Share on other sites More sharing options...
Fergal Andrews Posted February 13, 2011 Share Posted February 13, 2011 Hi cool_techie, I can't see any obvious mistakes in your code but try entering the SQL statement directly in MySQL to make sure that is not producing any errors. Are any of the fields numeric in the db table? If so, remove the quotes around the corresponding values in the SQL. You might also want to do a var_dump($_POST) in process.php to make sure nothing strange is getting posted. Best of luck, Fergal Quote Link to comment https://forums.phpfreaks.com/topic/227513-unable-to-insert-data-in-db/#findComment-1173520 Share on other sites More sharing options...
lastkarrde Posted February 13, 2011 Share Posted February 13, 2011 Rather than just echo "Error", why not echo the MySQL error?? echo mysql_error($conns); Quote Link to comment https://forums.phpfreaks.com/topic/227513-unable-to-insert-data-in-db/#findComment-1173525 Share on other sites More sharing options...
cool_techie Posted February 13, 2011 Author Share Posted February 13, 2011 When i use mysql_error($conns); the output is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add,phone) VALUES ('pulkit',[email protected]','26','3','1991','lucknow','9000463' at line 1 I am still unable to find out the error. NOTE:-All my table entries are VARCHAR Quote Link to comment https://forums.phpfreaks.com/topic/227513-unable-to-insert-data-in-db/#findComment-1173649 Share on other sites More sharing options...
kenrbnsn Posted February 13, 2011 Share Posted February 13, 2011 You're missing a quote at the start of the email value. Ken Quote Link to comment https://forums.phpfreaks.com/topic/227513-unable-to-insert-data-in-db/#findComment-1173652 Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 In addition to what kenrbnsn pointed out, 'add' is a MySQL reserved word. To use it as a table or field name, you need to enclose it in `backticks` whenever using it in that context in a query string. Quote Link to comment https://forums.phpfreaks.com/topic/227513-unable-to-insert-data-in-db/#findComment-1173653 Share on other sites More sharing options...
cool_techie Posted February 13, 2011 Author Share Posted February 13, 2011 can u just clarify the use of backticks.. a little more.! Quote Link to comment https://forums.phpfreaks.com/topic/227513-unable-to-insert-data-in-db/#findComment-1173657 Share on other sites More sharing options...
cool_techie Posted February 13, 2011 Author Share Posted February 13, 2011 Thnx, I just changed 'add' to 'adds' and now it is working.! Quote Link to comment https://forums.phpfreaks.com/topic/227513-unable-to-insert-data-in-db/#findComment-1173660 Share on other sites More sharing options...
Jessica Posted February 13, 2011 Share Posted February 13, 2011 Thnx, I just changed 'add' to 'adds' and now it is working.! Those are apostraphes, not ticks. ' != ` Quote Link to comment https://forums.phpfreaks.com/topic/227513-unable-to-insert-data-in-db/#findComment-1173813 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.