Jump to content

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


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());

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.