MySQL server version for the right syntax to use near '' at line 4 Error?

I tried this coding and it issues “MySQL server version for the right syntax to use near ‘’ at line 4” . I can not figure out the reason and once I comment the UPDATE statement the remaining code works fine. Please help me to solve this

[php]

<?php //error corde. error_reporting(E_ALL^E_NOTICE); $con=mysql_connect('localhost','root',''); mysql_select_db("ydms")or die(mysql_error()); // Search Button if (isset($_POST['search'])) { $cust_id=$_POST['cust_id']; $cust_name=$_POST['cust_name']; $cust_shop_name=$_POST['cust_shop_name']; //retriving data using customer number if($cust_id!='') { $query="SELECT * FROM customer WHERE cust_id='$cust_id'"or die(mysql_error()); $myquery=mysql_query($query); $sql=mysql_fetch_array($myquery)or die(mysql_error()); } //retriving data using customer name if($cust_name!='') { $query="SELECT * FROM customer WHERE cust_name='$cust_name'"or die(mysql_error()); $myquery=mysql_query($query); $sql=mysql_fetch_array($myquery)or die(mysql_error()); } //retriving data using shop name if($cust_shop_name!='') { $query="SELECT * FROM customer WHERE cust_shop_name='$cust_shop_name'"or die(mysql_error()); $myquery=mysql_query($query); $sql=mysql_fetch_array($myquery)or die(mysql_error()); } else { //echo "Please select one method to search"; } } //Update Button if(isset($_POST['Update'])){ $udtID=$sql['cust_id']; echo $udtID; $cust_contact_name=$_POST['cust_contact_name']; if (!preg_match('/^[a-zA-Z- ]+$/',$cust_contact_name)) { $valid = true; die( ' '); } $cust_contact1=$_POST['cust_contact1']; if (!preg_match('/^[0-9]{10}$/',$cust_contact1)) { $valid = true; die( ' '); } $cust_contact2=$_POST['cust_contact2']; if (!preg_match('/^[0-9]{10}$/',$cust_contact2)) { $valid = true; die( ' '); } $cust_remark=$_POST['cust_remark']; } $udtSql="UPDATE customer SET cust_contact_name='$cust_contact_name', cust_contact1='$cust_contact1', cust_contact2='cust_contact2', cust_remark='$cust_remark' WHERE cust_id=$cust_id "; $custUdt=mysql_query($udtSql) or die(mysql_error()); ?>

[/php]

Exact Error :- You have an error in your SQL ; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’ at line 4

It is hard to tell from here because there is no line numbering here. I think the syntax error in MySQL is caused when you type an incorrect keyword, I mean typo in the keyword, therefore, one solution could be to echo out all the queries and see what query is causing the problem.

Let’s see what TopCoder has to say about it.

I checked it… once I commented the Update query it doesn’t shows the error , but I can’t find any error in that query and this part is the top most part in that page.

@tanzeelniazi Thanks for dragging me into this!

Change…

[php]$udtSql="UPDATE customer
SET cust_contact_name=’$cust_contact_name’, cust_contact1=’$cust_contact1’,
cust_contact2=‘cust_contact2’, cust_remark=’$cust_remark’
WHERE cust_id=$cust_id ";[/php]

To…

[php]$udtSql=“UPDATE customer
SET cust_contact_name=’” . $cust_contact_name . “’, cust_contact1=’” . $cust_contact1 ."’,
cust_contact2=’" . $cust_contact2 . “’, cust_remark=’” . $cust_remark . “’
WHERE cust_id=’” . $cust_id ."’";[/php]

Ok. Now I got it. Thanks TopCoder.

@SwanWhite, You can also use {} to enclose the variables in the query.

Thank you so much guys for your kind support…

now it dosn’t give that error anymore, but the updated values also not shown in the db either. The updated fields are shown as empty.

Thank you so much guys for your kind support...

now it dosn’t give that error anymore, but the updated values also not shown in the db either. The updated fields are shown as empty.

Maybe $cust_id that you are updating doesn’t exist in the database…

Put this just before you do the update

[php]echo 'got here: ’ $cust_id;
exit;
[/php]

So you can see what ID you are trying to update and check the database to see if it exists.

Still not working… Please help me to correct this… I’m sending you the whole php file. please please help me


Update_cust.txt (10.9 KB)

What printed out when you echo’d the customer Id?

when I tried it ryt after search button’s qery it works ok. it gave correct cust id . but whn i plced it ryt b4 the update qry or as a 1st line in update value allocation it returns nothing.

I see why now…

[php]if (isset($_POST[‘search’]))
{
$cust_id=$_POST[‘cust_id’];
$cust_name=$_POST[‘cust_name’];
$cust_shop_name=$_POST[‘cust_shop_name’];
[/php]

You are setting the $cust_id inside the If condition of the search, so when it’s an update the $cust_id is never set.

You need to stick it under this line, like below.
[php]
if(isset($_POST[‘Update’])){
$cust_id=$_POST[‘cust_id’];
[/php]

I didn’t get it … isn’t that the same ? (I’m sorry if it’s stupid thing to ask… but really I didn’t get it)

Did you make the change?

Yep but nothing chaged


Now when you add the echo $cust_id before the update statement, is there a value?

yes… if I write it over the update block it works

Perfect…

That’s progress… Now the easy part…

Look at your update statement…

[php]$udtSql=“UPDATE customer
SET cust_contact_name=’” . $cust_contact_name . “’, cust_contact1=’” . $cust_contact1 ."’,
cust_contact2=’" . $cust_contact2 . “’, cust_remark=’” . $cust_remark . “’
WHERE cust_id=’” . $cust_id ."’";[/php]

You added 1 line of code to retrieve the cust_id right?

[php]$cust_id = $_POST[‘cust_id’];[/php]

Looks like you need to retrieve all the other values ($cust_contact_name , $cust_contact1, $cust_contact2 , $cust_remark)

Because right now all those values are empty and that’s why nothing working for you.

nope, nothing changed…

Show me your new code now…

if i set it after the search and before the update , still it won’t show any value.

Sponsor our Newsletter | Privacy Policy | Terms of Service