首先,我已经看过有关此问题的stackoverflow上的其他问题,但我仍然不明白该怎么做,这就是为什么我问这个问题所以请不要因为我问一个重复的问题。
我正在尝试将父母及其子女链接到我的网站。父母首先添加他们孩子的名字,然后创建一个关于孩子的新闻项目。这一切都很好,除了我正在努力完成用户勾选哪个孩子参与该新闻项目然后将这些孩子的名字存储在我的数据库中的部分。
目前,代码只输入表单中勾选的最后一个孩子的childID。我需要一个链接表
到目前为止代码:
<?php
$con=mysqli_connect("localhost","***","***","****");
if (isset($_POST['NewsDate'])&&isset($_POST['NewsDescription'])&&isset($_POST['Children'])){
$NewsDate= $_POST['NewsDate'];
$NewsDescription= $_POST['NewsDescription'];
$Children = $_POST['Children'];
if (!empty($NewsDate)&&!empty($NewsDescription)&&!empty($Children)){
$sql="INSERT INTO News (NewsDate, NewsDescription, Children, AccountID)
VALUES('$NewsDate','$NewsDescription','$Children','".$_SESSION['user_id']."')";
mysqli_query($con, $sql);
/* commit transaction */
if (!mysqli_commit($con)) {
print("Transaction commit failed\n");
exit();
}
} else {
echo'All fields are required';
}
}
/* close connection */
mysqli_close($con);
?>
<form action="addnews.php" method="post">
<p>News Date: <br/> <input type="text" name="NewsDate" id="datepicker"/></p>
<p>News Description (max 255 characters):<br><textarea name="NewsDescription" rows="6" cols="50" maxlength="255"></textarea></p>
<p>Children Involved: <br>
<?php
$con=mysqli_connect("localhost","pytsuemg_brodie","brodie","pytsuemg_brodie");
$result = mysqli_query($con,"SELECT * FROM Child WHERE `AccountID`='".$_SESSION['user_id']."'");
while($row = mysqli_fetch_array($result))
{
echo $row['Firstname'] . " " . $row['Surname']; ?>
<input type="checkbox" name="Children" value=" <? echo $row['ChildID']; ?>" />
<?
}
?>
</p>