PrinceOfDragons Posted September 13, 2009 Share Posted September 13, 2009 What is the simplest way to exclude empty $_POST[''] from updating a database instead of writing an if statement for each variable that’s needs to be updated ? Link to comment https://forums.phpfreaks.com/topic/174061-solved-form-and-database-help/ Share on other sites More sharing options...
Garethp Posted September 13, 2009 Share Posted September 13, 2009 $SQL1 = ""; $SQL2 = ""; foreach($_POST as $k=>$v) { if(empty($v)) { continue; } $SQL1 .= "`$k`, "; $SQL2 .= "'$v', "; } $SQL1 = substr($SQL1, 0, -2); $SQL2 = substr($SQL2, 0, -2); $SQL = "INSERT INTO `Table` ($SQL1) VALUES ($SQL2)"; mysql_query($SQL); Link to comment https://forums.phpfreaks.com/topic/174061-solved-form-and-database-help/#findComment-917513 Share on other sites More sharing options...
PrinceOfDragons Posted September 13, 2009 Author Share Posted September 13, 2009 Thank You It Worked, but i had to remove the single quotes from the following line. $SQL1 .= "$k, "; $SQL2 .= "$v, "; Link to comment https://forums.phpfreaks.com/topic/174061-solved-form-and-database-help/#findComment-917876 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.