Question:
How to insert data into mysql database table using php?
anonymous
2011-07-05 12:02:38 UTC
I'm trying to insert data into mysql database table using php
but it's not working.
I have table " check " with these fields (id , email).
Now I'm trying to insert data in this blank table by this script

session_start();

$email = $_SESSION['email'];


if(isset($_POST['submit'])) {
$error = "Unable to Connect.";
$connect = mysql_connect("localhost","root","") or die ($error);
mysql_select_db("mydatabase") or die ($error);


mysql_query("INSERT INTO check VALUES ('', '$email')") or die(mysql_error());
}
else
echo "Error";


?>

and it is giving this error
" 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 'check VALUES ( ' ', 'sallu_98@yahoo.com')' at line 1 "

Tell me how to solve it.?
Four answers:
Gitlez
2011-07-05 13:08:52 UTC
Although SurferTim is correct, it is good practice to declare the fields you are going to insert data into, it is not necessary as long as you have an insert value for each of your fields. Which you have done. The error is produced because you have used a mysql keyword for your table name. Change the tablename from 'check' to something else, like 'useremails' and you will no longer have the error.





mysql_query("INSERT INTO tablename(email) VALUES('{$email}')") or die(mysql_error());

Not need to insert an ID as it is auto_increment.





Cheers,

Gitlez
Dashmesh Singh
2014-05-11 20:41:37 UTC
can check this: easy step by step



http://dash10mesh.blogspot.com/2013/12/how-to-insert-php-form-data-into-mysql.html
guru
2014-06-17 03:43:33 UTC
Create table:



create table check (id int not nul auto_increment primary key, email varchar(50))



Check whether session contains value or not.


if($_SESSION['email']!='')

{

$email=$_SESSION['email'];

}



if(isset($_POST['submit']))

{

mysql_connect('localhost','root','') or die(mysql_error());

mysql_select_db('db') or die(mysql_error());

$q=mysql_query("insert into check values('','".$email."')") or die(mysql_error());

if($q)

{

echo'Values are inserted';

}

else

{

echo'Values are not inserted';

}

}

?>
?
2011-07-05 12:16:25 UTC
You did not specify which fields to insert the data. Should be something like this:



$result=mysql_query("INSERT INTO check (field1,field2) VALUES ('', '$email')")

or die(mysql_error());



Substitute your field names for field1 and field2.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...