As the title says, I'm trying to insert a row into a MySQL table using data sent in an HTML form. Some of the form fields can be left blank, however. Previously, for a select statement, I did this loop to check what fields were not empty and form an SQL query string (just for illustration), where $sql is a statment like "SELECT * FROM table WHERE":
for ($i = 0; $i < $size; $i++) {
if( $i != $size - 1 ) {
if( !empty($formData[$i]) ) {
$sql = $sql . " {$formFields[$i]} LIKE \"%{$formData[$i]}%\" and";
}
} else {
if( !empty($formData[$i]) ) {
$sql = $sql . " {$formFields[$i]} LIKE \"%{$formData[$i]}%\";";
}
else {
$sql = substr($sql, 0, strlen($sql)-4 ) . ";";
}
}
}
However in this case now, some of the insert fields can be blank, where previously it didn't matter because I was doing a "SELECT *" and I only had to worry about the WHERE clause. Is there an easier way to do this, other than having a monster of a for loop?