我正在尝试在我的数据库中将标志值更新为1,但它不起作用,我不知道它有什么问题。我在SQLAdministrator中执行相同的SQL语句,它可以工作。但它在我的PHP代码中不起作用。
<?php
$dc = $_POST['discount_code'];
$success = "success";
$fail = "fail";
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect to the database! ' . mysql_error());
}
/*Accessing the database */
$db_selected = mysql_select_db("db", $con);
if (!$db_selected) {
die ('Database error!' . mysql_error());
}
else{
$data = mysql_query("SELECT * FROM discount_code WHERE disc_code = '$dc'") or die(mysql_error());
$info = mysql_fetch_array($data);
if($info['flag'] == "0"){
$res = mysql_query($con,"UPDATE discount_code SET flag=1 WHERE disc_code='$dc'");
}
if($info['flag'] === "1"){
echo $fail;
}
}
?>
<form action="" method="post" name="disc_code_form" >
<input type="text" name="discount_code" size="25"></input>
<input type="submit" name="submit" value="Submit">
</form>
答案 0 :(得分:0)
试试这个:
if($info['flag'] == "0"){
echo 'Updating..'; //so you know whether this condition met or not
$res = mysql_query($con,"UPDATE discount_code SET flag=1 WHERE disc_code='$dc'");
}
// run select statement again to get the updated row
$data = mysql_query("SELECT * FROM discount_code WHERE disc_code = '$dc'") or die(mysql_error());
$info = mysql_fetch_array($data);
if($info['flag'] == "1"){
echo $fail;
}