Page 1 of 1

updating with the help of fieldname

Posted: Tue Jul 31, 2007 7:12 am
by shivam0101
Hello,

Code: Select all

function Update($fieldName, $value, $tableName, $cond)
  {
	for($i=0;$i<count($fieldName);$i++)
	{
	  $ins_value.="$fieldName[$i]='$value[$i]', "; //problem occurs here
	}
	
	$ins_value=substr($ins_value,0,-2);
   
    if($cond)
	  $cond="WHERE $cond";
         echo "UPDATE $tableName SET $ins_value $cond";
    	 $query_upd=mysql_query("UPDATE $tableName SET $ins_value $cond");
		 if(query_upd)
		  return $query_upd;
		 else 
		  return mysql_error();
		 
  }
This is the function i wrote for updating.

fieldname and values are arrays

It works fine, but if i wanted to update a value by using fieldname it does not work.

for example, if there is a field name called counter and i want to update it by counter=counter+1 it does not work. So i modified the code from

Code: Select all

$ins_value.="$fieldName[$i]='$value[$i]', "; 

to

$ins_value.="$fieldName[$i]=$value[$i], ";
Now, it works fine for updating using fieldnames. But, it does not work if i update without using fieldnames.

Can anyone tell me how to solve this?




Thanx

Posted: Tue Jul 31, 2007 7:31 am
by shivam0101
i solved it.

instead of using

Code: Select all

array('myname')
while passing the values, i used

Code: Select all

array("'myname'")

Re: updating with the help of fieldname

Posted: Tue Jul 31, 2007 7:33 am
by superdezign
A more internal solution would be to determine if your values were strings or not with regex and such.

I.e. fields should be surrounded by backticks (`) and integers should only contain numbers.