0
我想要做的是在表RECORD中添加一條新記錄。 這個新記錄可以與來自表CATEGORY的多個類別連接。 現在,我已經做了下面的代碼,所以我可以選擇所有需要與記錄連接的類別。但是,我不知道如何將所選類別發佈到RECORD表中。 因此,例如,如果CATEGORY表中有5個類別,分別命名爲'one','two','three','four'和'five'(ID 1,2,3,4,5),並且您選擇'1'和'3'添加到新記錄中時,應將其記錄到RECORD表中的列category_id中,其ID爲1,3。PHP - 用複選框將多個類別添加到mysql
我已經在表中有一列'category_id'記錄,但它不會添加任何內容。我有以下代碼。真的希望有人能幫助我,我會很感激大的時間。我搜索了不少問題,但我仍然無法提前解決它:(非常感謝!
record.php
<form method="post" action="<?php echo $home; ?>add_form.php">
<div class="alert alert-info">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong><i class="icon-user icon-large"></i> Test</strong>
</div>
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
</table>
<br />
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<table cellpadding="0" cellspacing="0" border="0">
<?php
$query=mysql_query("select * from category")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['id'];
?>
<tr>
<td width="22%"></td>
<td width="5%" style="padding-bottom: 4px"><input name="selector[]" type="checkbox" value="<?php echo $id; ?>"></td>
<td width="20%" style="padding-top:3px; padding-bottom: 1px"><?php echo $row['type'] ?></td>
</tr>
<?php } ?>
</table>
</tr>
<tr>
<td><br /><br /></td>
</tr>
</table>
<input name="save" type="submit" id="save" class="btn btn-success" value="tEST">
</form>
add_form.php
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/dbconnect.php');
//specify here the Database name we are using
$name = $_POST['name'];
$category_id = isset($_POST['$id[$i]']) ? 1 : 0;
$sql = "INSERT INTO `test`.`record` (`id`, `name`, `category_id`, `date_added`)
VALUES (NULL, '{$name}', '{$category_id}', CURRENT_TIMESTAMP());";
//using mysql_query function. it returns a resource on true else False on error
$retval = mysql_query($sql, $conn);
if(! $retval)
{
die('Could not enter data: ' . mysql_error());
}
?>
<script type="text/javascript">
alert("New Record is Added to the Database");
window.location = "record.php";
</script>
<?php
//close of connection
mysql_close($conn);
?>
順便問一句,'$ id [$ i]'是什麼? –