Jump to content

[SOLVED] error in sql statment, someone spot it o rknow why?


jesushax

Recommended Posts

hi here is my code

 

$ID = $_POST["CompanyID"];
$TSQL = "UPDATE tblDirectory SET";
foreach($_POST as $key => $value){
    $TSQL .= " $key='".str_replace("'","''",$value)."'";
}
$TSQL = $TSQL." WHERE CompanyID='$ID'";

mysql_query($TSQL) or die (mysql_error());

 

debug

 

UPDATE tblDirectory SET CompanyID='65' CompanyName='AJ Limited' CompanyType='Sole Trader' CompanyTel='01482 123456' CompanyAddress='123 there' CompanyAge='' CompanyReg='' PLLimit='1,000,000' ELLimit='' RiskIns='0' DesIndem='0' PlanIndem='0' PrevTurn='Other' DomTurn='80' CIS='C' TB1='Federation of Small Business' TB2='test' TB3='' SiteUni='Y' TJioner='Y' TGeneral='Y' AdminEmp='0' TradeEmp='0' SubEmp='0' WHERE CompanyID='65'

 

here is the error message:

 

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 'CompanyName='AJ Limited' CompanyType='Sole Trader' CompanyTel='01482' at line 1

 

thanks

oh if i put the comma in the loop

 

ID = $_POST["CompanyID"];
$TSQL = "UPDATE tblDirectory SET";
foreach($_POST as $key => $value){
    $TSQL .= " $key='".mysql_real_escape_string($value)."', ";
}
$TSQL = $TSQL." WHERE CompanyID='$ID'";

mysql_query($TSQL) or die (mysql_error());

 

then the last value which doesnt need a comma after it, how do i get rid of it when im looping?

 

eg my code now reads

 

where soemthin='something', WHERE ID=...... i dont need that extra comma

ID = $_POST["CompanyID"];
$TSQL = "UPDATE tblDirectory SET";
foreach($_POST as $key => $value){
    $TSQL .= " $key='".mysql_real_escape_string($value)."', ";
}
$TSQL = rtrim($TSQL,',');
$TSQL .= " WHERE CompanyID='$ID'";

mysql_query($TSQL) or die (mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.