note 62868 added to control-structures.foreach

From: Date: Sat, 11 Mar 2006 12:17:25 +0000
Subject: note 62868 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-105268@lists.php.net to get a copy of this message
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 ---- Server IP: 84.18.202.180 Probable Submitter: 86.132.170.156 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- http://master.php.net/note/edit/62868 Del: integrated -- http://master.php.net/note/delete/62868/integrated Del: useless -- http://master.php.net/note/delete/62868/useless Del: bad code -- http://master.php.net/note/delete/62868/bad+code Del: spam -- http://master.php.net/note/delete/62868/spam Del: non-english -- http://master.php.net/note/delete/62868/non-english Del: in docs -- http://master.php.net/note/delete/62868/in+docs Del: other reasons-- http://master.php.net/note/delete/62868 Reject -- http://master.php.net/note/reject/62868 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#105268) next »