PDA

View Full Version : SQL Syntax Error when trying to insert to Database


tux
06-19-2009, 08:10 AM
Hi all,

I am trying to create another insert as part of an application I'm building but am getting 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 ')' at line 2"

I already have an INSERT that works fine. This is the code that works okay.

<?php

require_once("../Connection/PTPconnection.php"); // database connection
///////////////////////////////////////////
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$town = $_POST['town'];
$county = $_POST['county'];
$country = $_POST['country'];
$postcode = $_POST['postcode'];
$telephone = $_POST['telephone'];
$mobile = $_POST['mobile'];
$submitted = $_POST['submitted'];
///////////////////////////////////////////
if($submitted && username && $password){
$query = sprintf("SELECT * FROM users WHERE username = '$username' AND password = '$password'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
}
/////////////////////////////////////////
if($rowAccount){
header("location:register.php?error=2");
exit;
}
if(!$rowAccount && $submitted && $firstname && $lastname && $username && $password && $email && $address1 && $town && $county && $country && $postcode && $telephone){
$query = sprintf("INSERT INTO users (firstname, lastname, username, password, email, address1, address2, town, county, country, postcode, telephone, mobile)
values ('$firstname', '$lastname', '$username', '$password', '$email', '$address1', '$address2', '$town', '$county', '$country', '$postcode','$telephone', '$mobile')");
mysql_query($query)or die(mysql_error());
}
elseif($submitted && !$firstname || !$lastname || !$username || !$password || !$email || !$address1 || !$town || !$county || !$country || !$postcode || !$telephone){
header("location:register.php?error=1");
exit;
}

?>

My INSERT that throws the error is this, I have stripped it down to basics.

<?php

require_once("../Connection/PTPconnection.php"); // database connection

$imageID = "1";
$userID = "3";
$userbid = "60.00";

$query = sprintf("INSERT INTO bids (imageID, userID, userbid)
values ('$imageID', '$userID', '$userbid',)");
mysql_query($query)or die(mysql_error());

?>

Can anyone shed some light on why I am getting the error. I've been pulling my hair out over it for ages. Thanks guys.

Paul

tux
06-19-2009, 08:15 AM
Would you believe that seconds after I post this question I looked at my code and instantly saw the extra comma that had crept in at the end of the INSERT statement after $userbid.

Thanks any everyone who looks at this. :lol: