note 22513 added to ref.mysql

From: Date: Thu, 20 Jun 2002 15:27:30 +0000
Subject: note 22513 added to ref.mysql
Groups: php.notes 
Request: Send a blank email to php-notes+get-32051@lists.php.net to get a copy of this message
This may be useful to some people - it creates an update query using the form data passed to it. $link = mysql_connect("localhost", "username", "password"); mysql_select_db("database"); $fields = mysql_list_fields("database", "tablename", $link); $columns = mysql_num_fields($fields); $SQL = "UPDATE tablename SET "; foreach ($_POST as $key => $value) { if ($value != "") { for ($i = 0; $i < $columns; $i++) { if(mysql_field_name($fields, $i) == $key) { if ($i == 0) { if (is_numeric($value)) { $SQL = $SQL . "$key = $value"; } else { $SQL = $SQL . "$key = '$value'"; } } else { if (is_numeric($value)) { $SQL = $SQL . ",$key = $value"; } else { $SQL = $SQL . ",$key = '$value'"; } } } } } } $result = mysql_query($SQL); -- http://www.php.net/manual/en/ref.mysql.php http://master.php.net/manage/user-notes.php?action=edit+22513 http://master.php.net/manage/user-notes.php?action=delete+22513 http://master.php.net/manage/user-notes.php?action=reject+22513

« previous php.notes (#32051) next »