[SOLVED] Trouble altering a MySQL query depending on $_POST
Posted: Mon Feb 05, 2007 5:13 am
I have a table which retrieves data from a MySQL DB and then displays it in a table. Next to each column in a text field to alter the DB entry and at the bottom of the table a submit button to update the form. The first problem I had is that the empty text boxes were being submitted as values and altering the entries to blanks. So then I decided to run through a loop like the following:
But this is proving difficult to get working because of the placement of the commas. How could I make PHP identify if the $query .= is the last one so it doesn't put the comma before "WHERE ID = '$_POST[id]'".
This is what my typical failing query looks like:
I hope that makes sense to you.
Regards, Stephen
Code: Select all
if ($_POST) {
$query = "UPDATE reminders SET ";
if (!empty($_POST["type"])) {
$query .= "type = '$_POST[type]',";
}
if (!empty($_POST["view"])) {
$query .= "view = '$_POST[view]',";
}
if (!empty($_POST["message"])) {
$query .= "message = '$_POST[message]',";
}
if (!empty($_POST["preRemMess"])) {
$query .= "preRemMessage = '$_POST[preRemMess]',";
}
$query .= "WHERE id = '$_POST[id]'";
echo $query;
mysql_query($query) or die(mysql_error());
}This is what my typical failing query looks like:
Code: Select all
UPDATE reminders SET type = 'yo',WHERE id = '105'
Regards, Stephen