2016-05-04 50 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>'; 
} 
相關問題