Jump to content

Unable to insert data in db


cool_techie

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/227513-unable-to-insert-data-in-db/
Share on other sites

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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.