The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> unable to insert data in mySQL, error:nable to add dataYou have an error in your SQL syntax; check the
icy_negi84
post May 21 2014, 01:52 PM
Post #1





Group: Members
Posts: 8
Joined: 21-May 14
Member No.: 20,958



I'm new in PHP getting error while inserting data

error: Unable to add data 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 ''')' at line 1

my code: <?php
if(isset($_POST['add']))
{
$dbhost='localhost';
$dbuser='root';
$dbpass='ricky';
$conn=mysql_connect($dbhost,$dbuser,$dbpass);
if(!$conn)
{
die('unable to connect to database' .mysql_error());
}


/* if(! get_magic_quotes_gpc())
{
$Name=addslashes($_POST['Name']);
$Email=addcslashes($_POST['Email']);
$Contact=addcslashes($_POST['Contact']);
$messgae=addcslashes($_POST['message'])
}
else
{
$Name=$_POST['Name'];
$Email=$_POST['Email'];
$Contact=$_POST['Contact'];
$messgae=$_POST['message'];
}*/
$sql="INSERT INTO contact1".
"(Name,Email,Contact,message)".
"VALUES('$Name','$Email',$Contact,'$message')";
mysql_select_db('hotel');
$retval=mysql_query($sql,$conn);
if(! $retval)
{
die('Unable to add data' .mysql_error());
}
echo "Thank you for contacing us biggrin.gif";

mysql_close($conn);
}
?>

or I tried this also:


<?php
if(isset($_POST['add']))
{
$dbhost='localhost';
$dbuser='root';
$dbpass='ricky';
$conn=mysql_connect($dbhost,$dbuser,$dbpass);
if(!$conn)
{
die('unable to connect to database' .mysql_error());
}


/* if(! get_magic_quotes_gpc())
{
$Name=addslashes($_POST['Name']);
$Email=addcslashes($_POST['Email']);
$Contact=addcslashes($_POST['Contact']);
$messgae=addcslashes($_POST['message'])
}
else
{
$Name=$_POST['Name'];
$Email=$_POST['Email'];
$Contact=$_POST['Contact'];
$messgae=$_POST['message'];
}*/
$sql="INSERT INTO contact1".
"(Name,Email,Contact,message)".
"VALUES('$Name','$Email',$Contact,'$message')";
mysql_select_db('hotel');
$retval=mysql_query($sql,$conn);
if(! $retval)
{
die('Unable to add data' .mysql_error());
}
echo "Thank you for contacing us biggrin.gif";

mysql_close($conn);
}
?>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies
CharlesEF
post May 21 2014, 03:20 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



You should add the database name to the table create section. Also, your column names need to be single quoted, as in:
create table 'hotel'.'contact1'('Name' varchar(20),'Email' varchar(25),'Contact' int(10),'message' varchar(100)); and $sql="INSERT INTO contact1('Name','Email','Contact','message') VALUES ('$Name','$Email','$Contact','message')";

You should test the database creation first, if it passes then continue with the rest. What database are you using? My syntax has 'assumed' a MySQL database.

This post has been edited by CharlesEF: May 21 2014, 03:22 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 13th June 2024 - 06:05 PM