【问题标题】:PHP Dynamic fields INSERT MySQLPHP 动态字段插入 MySQL
【发布时间】:2018-05-05 01:06:32
【问题描述】:

我有一个动态表单,其中包含以下字段:

 <input type="text" name="firstname">
 <input type="text" name="surname">
 <textarea name="description">

假设我在 Mysql 上有一个具有相同列名的表。

当我发布时,我希望将数组拆分为列名和值,然后将其插入到准备好的语句中:

我尝试了以下方法:

foreach($_POST as $key => $value) {
$keys .= $key.',';
$values .= $value.',';
$q.= '?,';
$type .= 's';

}
rtrim($keys,',');
rtrim($values,',');
rtrim($q,',');

 $insert_stmt = $mysqli->prepare("INSERT INTO ".$form_table." (".$keys.") VALUES (".$q.")");
 $insert_stmt->bind_param($type, $values);  
 $insert_stmt->execute();

似乎失败了 bind_param() 失败

我猜它把 $values 看作一个长字符串..我接近了吗?

【问题讨论】:

  • idownvotedbecau.se/noresearch。这里有许多教程和线程,以及您可以使用的示例。
  • 您能直接转到与我的具体问题相关的相关帖子吗?

标签: php mysql dynamic insert implode


【解决方案1】:
//i - integer
//d - double
//s - string
//b - BLOB

$type = '';
$keys = '';
$values = '';
foreach($_POST as $key => $value) {
    $keys .= $key.',';
    $values .= $value.',';

    //change type according to your need or based on your data type.
    $type .= 's';
}
rtrim($keys,',');
rtrim($values,',');


//use keys, type and values

【讨论】:

  • 似乎在 build_param - $values 上失败
猜你喜欢
  • 1970-01-01
  • 2019-03-14
  • 2021-09-21
  • 2012-08-31
  • 2017-04-21
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多