2016-05-04 46 views
1

我有一个要插入数据库的输入数组。我不希望数组中的每个项目都有一行。我希望他们都在同一排。所以这是我的代码:使用PHP的准备语句将数组插入到MySQL数据库中

<?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(); 
?> 

查询数据库中运行,但只有第一个列表项从阵列...我需要使用破灭功能?我从来没有使用过,所以如果这是唯一的解决方案,有人可以告诉我如何在这个查询中使用它?

回答

4

而不是替换变量的值,你必须串联

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

这是如何让多一点清洁:

$changes = ''; 
foreach($_POST['change'] as $value) { 
    $changes .= '<li>' . $value . '</li>'; 
} 
相关问题