【发布时间】:2009-08-11 16:39:04
【问题描述】:
我的数据库中有 3 个表(actors、categories、actor_cats)。
我正在使用表单在数据库 (mysql) 中插入新信息。大多数信息进入演员表。 但是在标签“类别”中,我有一个复选框输入类型,其中包含我从类别表 [动作 (id1)、喜剧(id2)、戏剧(id3)] 中获得的 3 个字段。我想将此信息插入到我的 actor_cats 表中。 我想要做的是在演员表中创建一个新演员,并在演员表中插入复选框选择(可能是所有 3 个选项)。该表作为演员 ID 和类别 ID,所以我想要的是这样的:
表 actor_cats
第 1 行 |演员 (1) |类别ID (1)
第 2 行 |演员 (1) |类别ID (2)
举个例子……
我如何使用 mysqli 实现这一点?
提前致谢!
我仍然无法写入 actor_cats 表。这就是我正在做的:
//插入新actor后
$jid = $mysql->insert_id;
if (isset($_POST['cats'])) {
$cats = $_POST['cats'];
} else {
$cats = array();
}
$numCats = 0;
foreach ($cats as $catID) {
$cats_sql = "INSERT INTO actor_cats VALUES(?,?)";
$stmt = $mysql->stmt_init();
if ($stmt->prepare($cats_sql)) {
$stmt->bind_param('ii', $jid, $catID);
$stmt->execute();
if ($ok) {
$numCats = $numCats + 1;
} else {
echo "<p>Error inserting $catID: " .
$mysql->error . '</p>';
}
}
<?php
else:
$cats = 'SELECT id, type FROM categories';
$stmt = $mysql->prepare($cats);
$stmt->execute();
$stmt->bind_result($catid,$cattype);
?>
在复选框区域的表单中,我获取我的类别表并获取值,所以当我提交表单时,我的选择将被插入
//excerpt from form
<label>Category:</label><br />
<?php while($row = $stmt->fetch()) : ?>
<input type="checkbox" name="cats" value="<?php echo $catid; ?>" /><?php echo $cattype; ?>
<br /><br /> <?php endwhile; ?>
希望这很清楚! 感谢大家的帮助!
【问题讨论】:
-
感谢所有帮助!我设法使它工作!干杯!