开发者

Looking for a Function that insert "Any" form values to mysql

开发者 https://www.devze.com 2022-12-25 10:55 出处:网络
I am Looking for a Function that insert \"Any\" form values to mysql , update or delete 开发者_Python百科with php. (or can we do this with one function?)

I am Looking for a Function that insert "Any" form values to mysql , update or delete 开发者_Python百科with php. (or can we do this with one function?) (not OOP) I have found a function here:

example: get all fields of form and ınsert into database.

Thanks in advance


You can't do it entirely automated. Field names must be hard-coded in your script. The rest is pretty easy.
May be this one would help you.

/* the only 2 variables you have to edit for the particular table: */
$table="contacts";
fields=explode(",","name,surname,lastname,street,city,region,zip,country");

$query="INSERT INTO $table SET ".dbSet($fields);
mysql_query($query) or trigger_error(mysql_error().$query);

function dbSet($fields='') {
  $q='';
  foreach ($fields as $v) $q.=$v."='".mysql_real_escape_string($_POST[$v])."', ";
  return trim($q,", ");
}

The $fields array contain field names which must be the same in the form and the database. E.g. you have a table with a name field. So, the form <input> must have the same name.` Consequently, field contents will go to the right column in the database.

If you're looking for a tool to edit an unknown database, PHPMyAdmin may help you. You can try it here http://demo.phpmyadmin.net/STABLE/ (login root, empty password).

0

精彩评论

暂无评论...
验证码 换一张
取 消