updating with the help of fieldname

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

updating with the help of fieldname

Post 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
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

i solved it.

instead of using

Code: Select all

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

Code: Select all

array("'myname'")
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: updating with the help of fieldname

Post 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.
Post Reply