Darkmatter5 Posted May 27, 2008 Share Posted May 27, 2008 Can someone help me figureout the syntax error in the following code? <?php if(isset($_POST['editclient'])) { include 'library/dbconfig.php'; include 'library/opendb.php'; $result=mysql_query("SELECT FirstName,LastName,CompanyName FROM byrnjobdb.clients WHERE ClientID = " .$_POST['Client']); $row=mysql_fetch_array($result); $fields=array("ClientID","FirstName","LastName","CompanyName","ContactTitle","Address","City","State","ZipCode","HomePhone","WorkPhone","WorkPhoneExtension","FaxPhone","Email"); for($i=1; $i<=13; $i++) { if(isset($_POST['$fields[$i]'])) { mysql_query("UPDATE byrnjobdb.clients SET $fields[$i]=$_POST['$fields[$i]'] WHERE $fields[0]=$_POST['$fields[0]']"); } } if($_POST[FirstName]!=NULL) { $row["FirstName"]=$_POST["FirstName"]; } if($_POST[LastName]!=NULL) { $row["LastName"]=$_POST["LastName"]; } if($_POST[CompanyName]!=NULL) { $row["CompanyName"]=$_POST["CompanyName"]; } if($_POST[CompanyName]==NULL && $row["CompanyName"]==NULL) { echo "Client " .$_POST["Client"]. " (" .$row["LastName"]. ", " .$row["FirstName"]. ") updated!"; }else { echo "Client " .$_POST["Client"]. " (" .$row["LastName"]. ", " .$row["FirstName"]. " of " .$row["CompanyName"]. ") updated!"; } include 'library/closedb.php'; } ?> The exact error I get is "Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/byrndb/test.php on line 148". Line 148 in the overall php page is the one starting with "SET $fields2[$1]". Thanks in advance! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 27, 2008 Share Posted May 27, 2008 Enclose $_POST[$fields[$i]] in {}. And the other $_POST var. Quote Link to comment Share on other sites More sharing options...
Darkmatter5 Posted May 27, 2008 Author Share Posted May 27, 2008 Excellent it worked, but why does the code for($i=1; $i<=13; $i++) { if(isset($_POST['$fields[$i]'])) { mysql_query("UPDATE byrnjobdb.clients SET $fields[$i]=$_POST['$fields[$i]'] WHERE $fields[0]=$_POST['$fields[0]']"); } } Not update the table record? I also created an echo of echo "UPDATE byrnjobdb.clients SET $fields[$i]={$_POST[$fields[$i]]} WHERE $fields[0]={$_POST['Client']} <br>"; to see what's actually being passed to the database. The result from the echo looks good, so why when I look in the table are the values of the records not changing? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 27, 2008 Share Posted May 27, 2008 mysql_query("UPDATE byrnjobdb.clients SET $fields[$i]=$_POST['$fields[$i]'] WHERE $fields[0]=$_POST['$fields[0]']") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
Darkmatter5 Posted May 27, 2008 Author Share Posted May 27, 2008 Here's the result of the example I ran with your idea. UPDATE byrnjobdb.clients SET FirstName=Joe WHERE ClientID=5366 Unknown column 'Joe' in 'field list' Quote Link to comment Share on other sites More sharing options...
jonsjava Posted May 27, 2008 Share Posted May 27, 2008 he had it right, just forgot 2 single quotes. mysql_query("UPDATE byrnjobdb.clients SET $fields[$i]='$_POST['$fields[$i]']' WHERE $fields[0]=$_POST['$fields[0]']") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
Darkmatter5 Posted May 27, 2008 Author Share Posted May 27, 2008 Using the following code mysql_query("UPDATE byrnjobdb.clients SET $fields[$i]='$_POST['$fields[$i]']' WHERE $fields[0]=$_POST['$fields[0]']") or die(mysql_error()); produced the following 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 'WHERE ClientID=5366' at line 1" Any thoughts? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 27, 2008 Share Posted May 27, 2008 you might just need more quotes, but i'd like to see the SQL $sql = "UPDATE byrnjobdb.clients SET $fields[$i]='$_POST['$fields[$i]']' WHERE $fields[0]=$_POST['$fields[0]']"; mysql_query($sql) or die(mysql_error() . ": $sql"); Quote Link to comment Share on other sites More sharing options...
Darkmatter5 Posted May 27, 2008 Author Share Posted May 27, 2008 Here's the SQL for the clients table CREATE TABLE `clients` ( `ClientID` int(11) NOT NULL auto_increment, `FirstName` varchar(50) NOT NULL, `LastName` varchar(50) NOT NULL, `CompanyName` varchar(50) default NULL, `ContactTitle` varchar(50) default NULL, `Address` varchar(30) NOT NULL, `City` varchar(15) NOT NULL, `State` varchar(20) NOT NULL default 'Texas', `ZipCode` varchar(10) NOT NULL, `HomePhone` varchar(12) default NULL, `WorkPhone` varchar(12) default NULL, `WorkPhoneExtension` varchar(4) default NULL, `FaxPhone` varchar(12) default NULL, `Email` varchar(50) default NULL, PRIMARY KEY (`ClientID`) ) ENGINE=InnoDB AUTO_INCREMENT=5368 DEFAULT CHARSET=latin1; Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 27, 2008 Share Posted May 27, 2008 i meant i'd like to see the entire SQL that is causing the error message, using the code I posted. Quote Link to comment Share on other sites More sharing options...
Darkmatter5 Posted May 27, 2008 Author Share Posted May 27, 2008 Okay here's the results, but first I need to say each time I run the code as $sql="UPDATE byrnjobdb.clients SET $fields[$i]='$_POST['$fields[$i]']' WHERE $fields[0]=$_POST['Client']"; mysql_query($sql) or die(mysql_error() . ": $sql"); I get the original error of "Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/byrndb/test.php on line 150" If I run the code as $sql="UPDATE byrnjobdb.clients SET $fields[$i]={$_POST[$fields[$i]]} WHERE $fields[0]={$_POST['Client']}"; mysql_query($sql) or die(mysql_error() . ": $sql"); as suggested by Darkwatter, I get the following result "Unknown column 'none' in 'field list': UPDATE byrnjobdb.clients SET FirstName=none WHERE ClientID=5366" Quote Link to comment Share on other sites More sharing options...
jonsjava Posted May 27, 2008 Share Posted May 27, 2008 $sql="UPDATE byrnjobdb.clients SET $fields[$i]='{$_POST[$fields[$i]]}' WHERE $fields[0]={$_POST['Client']}"; mysql_query($sql) or die(mysql_error() . ": $sql"); Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 27, 2008 Share Posted May 27, 2008 probably should be: $sql="UPDATE byrnjobdb.clients SET $fields[$i]='{$_POST[$fields[$i]]}' WHERE $fields[0]='{$_POST['Client']}'"; mysql_query($sql) or die(mysql_error() . ": $sql"); always quote every value in SQL and you never have to worry about what to quote or not. Quote Link to comment Share on other sites More sharing options...
Darkmatter5 Posted May 29, 2008 Author Share Posted May 29, 2008 Excellent it worked great!! Thanks!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.