The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> 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
icy_negi84
post May 21 2014, 02:07 PM
Post #2





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



Is there any one I need help blush.gif blush.gif blush.gif blush.gif blush.gif blush.gif ???????

This post has been edited by icy_negi84: May 21 2014, 02:08 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
jimlongo
post May 21 2014, 02:35 PM
Post #3


This is My Life
*******

Group: Members
Posts: 1,125
Joined: 24-August 06
From: t-dot
Member No.: 16



There's a problem with your sql statement

$sql="INSERT INTO contact1".
"(Name,Email,Contact,message)".
"VALUES('$Name','$Email',$Contact,'$message')";


What I would do is make sure that works, maybe if you have phpmyadmin you can test the query with some real data. If the query doesn't work the rest is a waste of time.

This post has been edited by jimlongo: May 21 2014, 02:36 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post May 21 2014, 02:39 PM
Post #4


Programming Fanatic
********

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



First, you can't expect to get any help in 15 minutes. Second, you should not be using mysql at all as it is depreciated and could be dropped at any time. You should use mysqli instead. Also, you should clean any user supplied data and be using prepared statements to help combat SQL injections.

That being said, I see a few errors in your code: $messgae=addcslashes($_POST['message']) should have a ; at the end (I know you have it commented out right now). The $sql= has errors, each column name should have singe quotes and you are making 1 long line of code with no separation, ex: $sql="INSERT INTO contact1" . "('Name','Email','Contact','message') "."VALUES('$Name','$Email',$Contact,'$message')";

There may be more errors but I do not use this format so I could be missing them.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 21 2014, 02:43 PM
Post #5


.
********

Group: WDG Moderators
Posts: 8,206
Joined: 10-August 06
Member No.: 7



"$messgae" looks like a typo.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
icy_negi84
post May 21 2014, 02:47 PM
Post #6





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



I tried this also but still getting the error:

here the code:

<?php
$conn=mysqli_connect("localhost","root","ricky","hotel");
//check connection
if(mysqli_connect_errno())
{
echo"unable to connect" . mysqli_connect_error();

} else
{
echo"connected" ;
}

$Name=mysqli_real_escape_string($conn,$_POST['Name']);
$Email=mysqli_real_escape_string($conn,$_POST['Email']);
$Contact=mysqli_real_escape_string($conn,$_POST['Contact']);
$message=mysqli_real_escape_string($conn,$_POST['message']);

$sql="insert into contact1(Name,Email,Contact,message) values ('$Name','$Email',$Contact,'$message')";

if(! mysql_query($conn,$sql))
{
die('error' . mysqli_error($conn));
}
echo "Added";

mysqli_close($conn);

?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
icy_negi84
post May 21 2014, 02:54 PM
Post #7





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



My sql squery is:
create database hotel;
create table contact1(Name varchar(20),Email varchar(25),Contact int(10),message varchar(100));
select * from contact1;
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
jimlongo
post May 21 2014, 02:54 PM
Post #8


This is My Life
*******

Group: Members
Posts: 1,125
Joined: 24-August 06
From: t-dot
Member No.: 16



now you're mixing mysql and mysqli functions, I don't think you can do that.


no the query is = INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

This post has been edited by jimlongo: May 21 2014, 02:56 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 21 2014, 02:58 PM
Post #9


.
********

Group: WDG Moderators
Posts: 8,206
Joined: 10-August 06
Member No.: 7



The OP's last code example doesn't check that the $_POST variables exist before the SQL query takes place, so if the script runs before the form has been submitted you'll get an error.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
icy_negi84
post May 21 2014, 03:08 PM
Post #10





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



Could you please help me with the correct code for this:
This is my table and database:

create database hotel;

create table contact1(Name varchar(20),Email varchar(25),Contact int(10),message varchar(100));

select * from contact1;




my insert.php file is:


<?php
$conn=mysqli_connect("localhost","root","ricky","hotel");
//check connection
if(mysqli_connect_errno())
{
echo"unable to connect" . mysqli_connect_error();

} else
{
echo"connected" ;
}

$Name=mysqli_real_escape_string($conn,$_POST['Name']);
$Email=mysqli_real_escape_string($conn,$_POST['Email']);
$Contact=mysqli_real_escape_string($conn,$_POST['Contact']);
$message=mysqli_real_escape_string($conn,$_POST['message']);

$sql="INSERT INTO contact1(Name,Email,Contact,message) VALUES ('$Name','$Email',Contact,'message')";

if(! mysql_query($conn,$sql))
{
die('error' . mysqli_error($conn));
}
echo "Added";

mysqli_close($conn);

?>



my html file is:



<!doctype html>
<html>
<body>
<form method="post" action="insert.php">
<input type="text" name="Name" id="Name"
style="width: 450px;height: 45px;margin-bottom: 17px;margin-top:0px;font-size: 120%;"></br>
<input type="email" name="Email" id="Email"
style="width: 450px;height: 45px;margin-bottom: 29px;margin-top:10px;font-size: 120%"></br>
<input type="number" name="Contact" id="Contact"
style="width: 450px;height: 45px;margin-bottom: 20px;margin-top:0px;font-size: 120%"></br>
<textarea name="message" id="message" rows="5" cols="15" style="width: 450px;height: 155px;margin-bottom: 0px;
font-size: x-large;font-style: italic"></textarea></br>
<input type="submit" name="add" value="Submit Info" id="add" style="width: 75px;height: 45px;
font-size: large;font-style: italic;margin-bottom:245px;margin-left: 380px;background-color: #535151">
</form>
</body></html>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
icy_negi84
post May 21 2014, 03:17 PM
Post #11





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



Thanks for your help I deed it biggrin.gif biggrin.gif biggrin.gif L:D
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
icy_negi84
post May 21 2014, 03:19 PM
Post #12





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



if(! mysql_query($conn,$sql)) this was my error:

I corrected this to :

if(! mysqli_query($conn,$sql))


thanks : jimlongo and Christian J
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
icy_negi84
post May 21 2014, 03:20 PM
Post #13





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



CharlesEF thanks for help you tooo
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post May 21 2014, 03:20 PM
Post #14


Programming Fanatic
********

Group: Members
Posts: 1,639
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
Dag
post Sep 23 2014, 01:23 PM
Post #15


Member
***

Group: Members
Posts: 89
Joined: 24-October 06
Member No.: 549



QUOTE(icy_negi84 @ May 21 2014, 11:07 PM) *

Is there any one I need help blush.gif blush.gif blush.gif blush.gif blush.gif blush.gif ???????


Do u have table contact1?
AND(!!!)

Try to play with quotes as
'contact1'
or
"contact1"

The same with data.
'data'
or
`data`

It will works.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

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: 22nd August 2019 - 01:18 PM