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! Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/ 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. Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551044 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? Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551083 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()); Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551098 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' Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551110 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()); Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551111 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? Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551123 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"); Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551127 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; Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551141 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. Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551144 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" Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551149 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"); Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551151 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. Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-551155 Share on other sites More sharing options...
Darkmatter5 Posted May 29, 2008 Author Share Posted May 29, 2008 Excellent it worked great!! Thanks!! Link to comment https://forums.phpfreaks.com/topic/107502-parse-error-help/#findComment-552538 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.