dsgk0386
2016-05-04 22:14
浏览 53
已采纳

使用PHP预处理语句将数组插入MySQL数据库

I have an input array that I want to insert to database. I don't want to have 1 row in database for each item in the array. I want them all to go on same row. So this is my code:

<?php
session_start();
include('../../config/dbconf.php');

mysqli_select_db($conn, $webdb);

$stmt = $conn->prepare("INSERT INTO changelog (title, type, content, author, post_date) VALUES (?, ?, ?, ?, ?)");

$title = $_POST['title'];
$type = $_POST['type'];
$change = $_POST['change'];
$author = $_SESSION['waradmin'];
$date = time();

foreach($_POST['change'] as $key => $value) {
   $changes = "<li>" . $change[$key] . "</li>";
}

$stmt->bind_param("sissi", $title, $type, $changes, $author, $date);
if($stmt->execute()) {
   header('location: ../?p=dashboard');
}else{
   echo "Error: " . $stmt->error;
}

$stmt->close();
?>

The query runs in database but only the first list item from the array... Do I need to use the implode function? I have never used it so if that is the only solution can someone show me how i use that in this query?

图片转代码服务由CSDN问答提供 功能建议

我有一个我要插入数据库的输入数组。 我不希望数组中的每一项都有1行。 我希望他们都能在同一排。 所以这是我的代码:

 &lt;?php 
session_start(); 
include('../../ config / dbconf.php'); 
 \  nmysqli_select_db($ conn,$ webdb); 
 
 $ stmt = $ conn-&gt; prepare(“INSERT INTO changelog(title,type,content,author,post_date)VALUES(?,?,?,?,?)  “); 
 
 $ title = $ _POST ['title']; 
 $ type = $ _POST ['type']; 
 $ change = $ _POST ['change']; 
 $ author = $  _SESSION ['waradmin']; 
 $ date = time(); 
 
foreach($ _ POST ['change'] as $ key =&gt; $ value){
 $ changes =“&lt; li&gt;”  。  $ change [$ key]。  “&lt; / li&gt;”; 
} 
 
 $ stmt-&gt; bind_param(“sissi”,$ title,$ type,$ changes,$ author,$ date); 
if($ stmt-&gt;  execute()){
 header('location:../?p = dashboard'); \\}}} {}} {}}  $ stmt-&gt;错误; 
} 
 
 $ stmt-&gt; close(); 
?&gt; 
   
 
 

查询在数据库中运行 但只有数组中的第一个列表项...我是否需要使用内爆函数? 我从来没有使用它,所以如果这是唯一的解决方案,有人可以告诉我如何在此查询中使用它吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题