Hi guys,

I have this sql statement that is trying to insert data from some fields

<?php
$con = mysql_connect("xxx","xx","xx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xxx", $con);
$sql="INSERT INTO guests (guest_name, guest_email) VALUES";
for($i=0;$i<sizeof($_POST);$i++){
$sql.="('".mysql_real_escape_string($_POST[$i])."','".mysql_real_escape_string($_POST[$i])."')";
if(!$i==sizeof($_POST)){
$sql.=",";
}
}
//
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "record added";
?>

but it returns the error

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 '' at line 1

you have forgotten a period after $sql"

$sql.="INSERT INTO guests (guest_name, guest_email) VALUES";

cheers Ryan. have added the period but the error still happens

Print your query and execute it in mysql console/phpmyadmin.. Also show us the query ..

Print your query and execute it in mysql console/phpmyadmin.. Also show us the query ..

not sure what you mean here. please elaborate?

$sql="INSERT INTO guests (guest_name, guest_email) VALUES";
for($i=0;$i<sizeof($_POST['username']);$i++){

$sql.="('".mysql_real_escape_string($_POST['username'][$i])."','".mysql_real_escape_string($_POST['email'][$i])."')";
if(!$i==sizeof($_POST['username'])){
$sql.=",";
}
}
echo $sql;

What does it print ?

oh i understand now.

it prints :
INSERT INTO guests (guest_name, guest_email) VALUES

Thats it ? Its not entering the for loop ? Well, then there is the problem. Check your for loop and see if the values are being posted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.