Jump to content

Parse error help!


Darkmatter5

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.