我试图使一个页面下拉列表,我希望用户添加他想要的电影名称,并通过mysql查询发送到数据库。
现在,这就是我从数据库中获取下拉列表所做的(这部分是工作的)
<select name="cinema_id" id="cinemaid">
<?php
require('../classes/cinema_class.php');
$cinemas = cinema::get_cinemas_name();
$cinema_id = $cinema['cinema_id'];
$cinema_name = $cinema['cinema_name'];
foreach($cinemas as $cinema)
{
$selected = '';
if( $cinema['cinema_id'] == $_GET['cinema_id'])
{
$selected = 'select="selected"';
}
echo "<option {$selected} >
{$cinema['cinema_name']}
</option>";
}
?>
</select>
和这是mysql查询更新
<?php
$theaterid=$_POST['theaterid'];
$theatername=$_POST['theatername'];
$cinemaid=$_POST['cinemaid'];
if(empty($theatername)){
echo "Theater Name is a must";
}
else{
$update_movie=mysql_query("UPDATE `memoire`.`theater`
SET `theater_name`= '$theatername',`cinema_id`= '$cinemaid' //the $theatername is working, i think the $cinemaid is wrong
WHERE `theater_id`='$theaterid'");
if($update_movie)
{ echo "Add Succesfull";}
else
{ echo "error in registration".mysql_error(); }
}?>
我认为问题是如何调用下拉列表中的值并将其发送到更新
顺便说一下,页面打印:无法添加或更新子行:外键约束失败(memoire
。theater
,约束theater_ibfk_1
外键(cinema_id
)引用cinema
(cinema_id
) ON DELETE CASCADE ON UPDATE CASCADE
我认为cinema_id是当前的cinema_name。修改这一行:
echo "<option {$selected} >
echo "<option {$selected} value='{$cinema[cinema_id]}' >
在第一个代码片段中,需要有一个value属性。
echo "<option value='{$cinema['cinema_id']}' {$selected} >
{$cinema['cinema_name']}
</option>";
至少会向服务器发送正确的数据。在第二个代码片段中,您引用的是html选择的id,而不是名称。你应该使用这个名字
$cinemaid=$_POST['cinema_id']; // notice the underscore
这应该给你正确的值在服务器上,一旦发布。
作为旁注,您的脚本对SQL注入和跨站点脚本是开放的。你可能想看看这些
我想我现在可以问更好的问题了。在我的数据库中,我需要添加cinema_id。下拉列表给我cinema_name…所以我需要用户选择cinema_name,但我需要将cinema_id添加到更新查询
谢谢
这个问题与上面的代码片段无关,而是与您如何在数据库中设置外键有关。你能张贴你上面引用的表的结构,以及电影院表指出你是如何设置你的参考键?