我想更新多行
这是我的HTML代码:
<form action="" method="POST">
<input type="text" name="name" placeholder=""/>
<input type="text" name="phone" placeholder=""/>
<input type="text" name="blood" placeholder=""/>
<input type="text" name="id" placeholder=""/>
<input type="text" name="email" placeholder=""/>
<input type="submit" name="sub" value=""/>
</form>
这是我的PHP代码:
if(isset($_POST['sub'])) {
$name = $_POST['name'];
$phone = $_POST['phone'];
$blood = $_POST['blood'];
$id = $_POST['id'];
$email = $_POST['email'];
if($name or $phone or $blood or $id or $email) {
$res_11 = mysql_query("UPDATE `general_info` SET `name`='$name',`phone`='$phone',`blood`='$blood',`id_2`='$id',`email`='$email' WHERE `id`='1'") or die(mysql_error());
}
}else{
echo "ERROR";
}
我有这样的表
name | phone | blood
我希望如果某个机构只更新一行更新
如果只是新手机更新只是手机在数据库中
答案 0 :(得分:1)
试用此代码:
if(isset($_POST['sub'])) {
$subQuery = "";
if(isset($_POST['name']) && $_POST['name'] != "") {
$subQuery .= " name = '". $_POST['name'] ."',";
$name = $_POST['name'];
}
if(isset($_POST['phone']) && $_POST['phone'] != "") {
$subQuery .= " phone = '". $_POST['phone'] ."',";
$phone = $_POST['phone'];
}
if(isset($_POST['blood']) && $_POST['blood'] != "") {
$subQuery .= " blood = ". $_POST['blood'] .",";
$blood = $_POST['blood'];
}
if(isset($_POST['email']) && $_POST['email'] != "") {
$subQuery .= " email= '". $_POST['email'] ."',";
$email = $_POST['email'];
}
if(isset($_POST['id']) && $_POST['id'] != "") {
$subQuery .= " id_2 = '". $_POST['id'] ."',";
$id = $_POST['id'];
}
if($name or $phone or $blood or $id or $email) {
$res_11 = mysql_query("UPDATE `general_info` SET ". substr($subQuery, 0, -1) ." WHERE `id`='1'") or die(mysql_error());
}
}else{
echo "ERROR";
}
答案 1 :(得分:0)
我认为这就是你想要做的。但是不要使用 mysql 使用PDO或mysqli。
x+3