概述 我是新來的PHP,所以忍受着我。我似乎無法在這裏找到答案,儘管我可能不知道它是否會打擊我。如何將多個複選框選擇值放入SQL表中的單獨列中?
我正在構建Knowladgebase系統。我有一個表單將新的KB文章插入到數據庫中。我希望用戶能夠使用多個複選框來選擇新KB所適用的類別。
我有這兩個相關的表 - 第一個是類別這是類別列表和秒category_kb_members這將保持其知識庫文章與每個類別相關的記錄。
這是到目前爲止我的PHP代碼:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['title']) && isset($_POST['type']) && isset($_POST['category']) && isset($_POST['article'])){
$title = mysqli_real_escape_string($dbc,$_POST['title']);
$type = mysqli_real_escape_string($dbc,$_POST['type']);
$category = mysqli_real_escape_string($dbc,$_POST['category']);
$article = mysqli_real_escape_string($dbc,$_POST['article']);
$now = gmdate(DATE_RFC822);
$mod = "Never";
$author = $_SESSION['username'];
// Insert into article database
$q1 = $dbc->prepare("INSERT INTO articles (title, type, author, createdate, moddate, article) VALUES (?, ?, ?, ?, ?, ?)");
$q1->bind_param("ssssss", $title, $type, $author, $now, $mod, $article);
$q1->execute();
$q1->close();
//Query the KB Number and store in variable
$kbnumber = mysqli_query($dbc, "SELECT idarticles FROM articles WHERE createdate = '$now'");
//Get Categories as array and insert KB id's into relevent columns.
$query = mysqli_query($dbc,"SELECT * FROM categories");
while($row = mysqli_fetch_assoc($query))
{
mysqli_query($dbc,"INSERT INTO category_kb_members (".$row['name'].") VALUES ($kbnumber)");
}
}else{$error = "Isset is false";
}
}else{$error = "Request methos is false";
}
mysqli_close($dbc);
我與PHP的一些HTML內嵌:
<div id="kbform">
<?php echo $error ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
Title/Description: <input type="text" name="title" maxlength="255" placeholder="Please enter a Title/Description" required/><br />
KB Type:
<select name="type">
<option value="Fault" selected="selected" maxlength="20">Fault</option>
<option value="Guide" maxlength="20">Guide</option>
</select>
<br />
<br />
Please select any categories that this KB matches:
<div class="form-group">
<?php
require "admin/connection.php";
$query = mysqli_query($dbc,"SELECT * FROM categories");
while($row = mysqli_fetch_array($query))
{
echo "<input type='checkbox' name='category' value=".$row['name'].">".$row['name']." | ";
}
mysqli_close($dbc);
?>
</div>
<br />
<p>Enter you KB Info. HTML is supported.<textarea name="article" rows="10" cols="40" required placeholder="Describe the fault, any errors, relevent environmental factors and the steps took to resolve. Try to add as much useful information as possible."></textarea></p>
<p>Entered By: <?php echo $_SESSION['username'] ?></p>
<input type="submit" name="submit" value="Submit" />
</form>
</div>
列在類別表 'idcategories' 和「名」。第二個表中的列是'idcategory_kn_members',然後是'category1'和'category2'等等,它們將被添加。
我目前得到的錯誤是「PHP可恢復的致命錯誤:類mysqli_result的對象無法轉換爲字符串」,我堅持如何實現這一目標。我發現每行都有這樣的文章,但不是每欄。
任何幫助非常感謝。
唔查詢的每行的數據庫可能會給你的服務器帶來沉重的負擔,同樣你的錯誤也解釋了所有'$ kbnumber = mysqli_query($ dbc,「SELECT idarticles FROM articles WHERE createdate ='$ now'」);'不是字符串 – mrahmat