0

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

1 回答 1

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;
}
于 2013-09-13T09:54:19.590 回答