note 63160 added to control-structures.foreach

From: Date: Tue, 14 Mar 2006 22:13:46 +0000
Subject: note 63160 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-105818@lists.php.net to get a copy of this message
A friend of the UPDATE query maker, this generates insert queries and is used in the same way <? if ($_POST){ //change the table name for your table $query = "INSERT INTO table ("; //Set this to the number of fields you have $a = 2; //this is the counter, so you don't get submit buttons in the query $b = 0; $c = 0; foreach ($_POST as $key => $value) { if ($b != $a){ $$key = addslashes(trim($value)); $query.= " " . $key . " "; $b++; } if ($a == $b){ $query.=" "; }else{ $query.=","; } } //Add data $query.=")VALUES("; foreach ($_POST as $key => $value) { if ($c != $a){ $$key = addslashes(trim($value)); $query.= "'" . $value . "'"; $c++; } if ($a == $c){ $query.=" "; }else{ $query.=","; } } $query.=")"; //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: 85.116.0.130 Probable Submitter: 86.132.170.160 ---- 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/63160 Del: integrated -- http://master.php.net/note/delete/63160/integrated Del: useless -- http://master.php.net/note/delete/63160/useless Del: bad code -- http://master.php.net/note/delete/63160/bad+code Del: spam -- http://master.php.net/note/delete/63160/spam Del: non-english -- http://master.php.net/note/delete/63160/non-english Del: in docs -- http://master.php.net/note/delete/63160/in+docs Del: other reasons-- http://master.php.net/note/delete/63160 Reject -- http://master.php.net/note/reject/63160 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#105818) next »