数据库更新问题,无法更新

时间:2013-09-13 09:38:22

标签: php html mysql sql

我正在尝试在我的数据库中将标志值更新为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>

1 个答案:

答案 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;
}