> I always found this way of inserting data into a database messy. Here is > a handy function to do array inserts and it builds the sql for you. > > function arrayINSERT($a,$tablename) > { > $sql = "INSERT INTO $tablename ("; > foreach($a as $key => $value) > { > $sql .= $key .","; > } > $sql[strlen($sql)-1] = ')'; > $sql .= " VALUES ("; > foreach($a as $key => $value) > { > if (gettype($value) == 'string') > { > $sql .= "'". addslashes($value) ."',"; > } > else > { > $sql .= $value .","; > } > } > $sql[strlen($sql)-1] = ')'; > return $sql; > } > > if you do this : > > $a['field1'] = $blah; > $a['field2'] = $here; > $a['field3'] = $etc; > $sql = arrayINSERT($a,"tablename"); > > > it builds the sql statement for you. It covers 99.9% of the inserts > your likely to need. I use an update and insert function like this all > the time. :-) > > Mark > > > > Richard Davey wrote: > > > > > $sql = " > > INSERT INTO > > tablename > > ( > > field1, > > field2, > > field3 > > ) > > VALUES > > ( > > '$blah', > > $here', > > '$etc' > > ) > > "; >
ok sounds like fun and a way to keep code compacted but say inparticular that i have an array that needs to be inserted in a certain way?? like $_SESSION['add'][..] or something of that sort??the $_SESSION['add'] array elements have to be inserted in a certain order...or is that just a matter of creating the array in a certain order on the page before it?? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php