note 62868 deleted from control-structures.foreach by nlopess
| From: | nlopess@php.net | Date: | Thu, 20 Apr 2006 16:24:34 +0000 |
| Subject: | note 62868 deleted from control-structures.foreach by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-108824@lists.php.net to get a copy of this message | ||
Note Submitter: http://www.vainternet.co.uk
----
This is a little code snippet to generate update queries, to use it, you have to name all the form
fields the same name as your database fields, which is probably good practice anyway.
if ($_POST){
//change the table name for your table
$query = "UPDATE table SET ";
//Set this to the number of fields you have
$a = 13;
//this is the counter, so you don't get submit buttons in the query
$b = 0;
foreach ($_POST as $key => $value) {
if ($b != $a){
$$key = addslashes(trim($value));
$query.= " " . $key . "='" . $value . "' ";
$b++;
}
if ($a == $b){
$query.=" ";
}else{
$query.=",";
}
}
//Apply conditions
$query.=" WHERE id='1' LIMIT 1 ";
//Remove this line after you have debugged
print $query;
if ($result = MYSQL_QUERY($query)){
print "success";
}else{
print "not success";
}
}
http://www.vainternet.co.uk