jeff5656 Posted January 31, 2012 Share Posted January 31, 2012 I have some code that will update a record and is generic, meaning any POST variables can be used - whatever you have on the form. See below: $set = array(); foreach($_POST as $field => $value){ $field = mysql_real_escape_string($field); $value = mysql_real_escape_string($value); $set[] = "`{$field}` = '{$value}'"; } $query .= implode(", ",$set) . " WHERE $id_name = '".$id."' LIMIT 1"; mysql_query($query) or die(mysql_error()); My question is, how would I modify this to insert a NEW record (not update an existing one). I'm not sure how to order this within a foreach statement because the add query has a different form: insert into tabel (all the fieldnames here) VALUES (all the values here) Quote Link to comment https://forums.phpfreaks.com/topic/256138-use-a-foreach-statement-to-add-a-record-to-a-table/ Share on other sites More sharing options...
digibucc Posted January 31, 2012 Share Posted January 31, 2012 here's what i did: <?php foreach ($entry as $key=>$value){ if ($value != '' && $value != 'Submit'){ $cols .= mysql_real_escape_string($key). ', '; $vals .= '\''. mysql_real_escape_string($value). '\', '; } } $columns = substr($cols,0,-2); // trim trailing "'," , $values = substr($vals,0,-2); $sql="INSERT INTO table ( $columns )VALUES ( $values )"; i'm sure there are other ways to handle the trailing "',", but this works for me Quote Link to comment https://forums.phpfreaks.com/topic/256138-use-a-foreach-statement-to-add-a-record-to-a-table/#findComment-1313070 Share on other sites More sharing options...
PFMaBiSmAd Posted January 31, 2012 Share Posted January 31, 2012 Also, there's a version of the INSERT query that uses the SET column=value,... syntax. Quote Link to comment https://forums.phpfreaks.com/topic/256138-use-a-foreach-statement-to-add-a-record-to-a-table/#findComment-1313082 Share on other sites More sharing options...
jeff5656 Posted January 31, 2012 Author Share Posted January 31, 2012 Thanks digibucc that worked nicely. PFMaBiSmAd: that would be a cool feature. Does that depend on what version of php I'm using? Quote Link to comment https://forums.phpfreaks.com/topic/256138-use-a-foreach-statement-to-add-a-record-to-a-table/#findComment-1313116 Share on other sites More sharing options...
AyKay47 Posted January 31, 2012 Share Posted January 31, 2012 Thanks digibucc that worked nicely. PFMaBiSmAd: that would be a cool feature. Does that depend on what version of php I'm using? no, it has to do with mysql http://dev.mysql.com/doc/refman/5.1/en/insert.html Quote Link to comment https://forums.phpfreaks.com/topic/256138-use-a-foreach-statement-to-add-a-record-to-a-table/#findComment-1313117 Share on other sites More sharing options...
jeff5656 Posted January 31, 2012 Author Share Posted January 31, 2012 Thanks, BTW I love your sig quote: "In Coding, Automatic means you write code to do it" So true! Quote Link to comment https://forums.phpfreaks.com/topic/256138-use-a-foreach-statement-to-add-a-record-to-a-table/#findComment-1313120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.