我有一个选择框,它应该通过选项更改来更新我的SQL数据库。因为在这种情况下我没有正常的提交输入字段,if(isset($_GET['submit']))
不起作用,但我不知道如何以正确的方式编写语句:
<?php
if(isset($_GET['submit'])){
$status = $_GET['status'];
$id = $_GET['id'];
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE colors set status = ? WHERE id = '$id'";
$q = $pdo->prepare($sql);
$q->execute(array($status));
Database::disconnect();
}
?>
<form action="index.php" method="get">
<select name="status" onchange='this.form.submit()'>
<option value="red">red</option>
<option value="blue">blue</option>
</select>
<?php echo "<input id='id' type='hidden' value='"$id"'>";?>
</form>
答案 0 :(得分:2)
尝试替换此行
if(isset($_GET['submit'])){
带
if(array_key_exists('status',$_GET) && $_GET['status']!=""){
答案 1 :(得分:1)
可能就像
一样简单if(!empty($_GET['status'])){
....
}
答案 2 :(得分:0)
试试这个:
<?php
$id = "YourID";
if(!empty($_GET["status"])){
$status = $_GET['status'];
if(isset($_GET['id']) && $_GET['id'] != ""){
$id = $_GET["id"];
//continue...
}else{
//display error (ID has not been set.)
}
}
?>
<form action="index.php" method="get">
<select name="status" onchange='this.form.submit()'>
<option value="red">red</option>
<option value="blue">blue</option>
</select>
<?php echo "<input name='id' id='id' type='hidden' value='$id'>";?>
</form>