我编写了这段代码,我确信代码和服务器中的所有内容都是正确的,但它不起作用!! 怎么了 ?!
<?php
$id = isset($_POST["id"]);
$decrease = isset($_POST["dis"]);
$conn = mysqli_connect('localhost', 'root','');
$select = mysqli_select_db($conn, "test");
if ($decrease=='10%')
$q1 = 'update book set Price=Price-Price * 0.1 where ID="$id";';
else
$q1 = 'update book set Price=Price-Price* 0.3 where ID="$id";';
$b = mysqli_query($conn, $q1);
$query = "select * from book where ID = '$id';";
$res = mysqli_query($conn, $query);
$r = mysqli_fetch_row($res);
print("<table border='1'><tr><td>ID</td><td>Name</td><td>Price</td> </tr>");
echo '<td>'.$r[0].'</td><td>'.$r[1].'</td><td>'.$r[2].'</td>';
?>
答案 0 :(得分:0)
您将isset
值分配给变量,这意味着它将始终为true或false。相反,在if语句中使用isset
,然后运行其下的所有其他内容:
<?php
if(isset($_POST["id"]) && isset($_POST["dis"])) {
$id = $_POST["id"];
$decrease = $_POST["dis"];
$conn = mysqli_connect('localhost', 'root','');
$select = mysqli_select_db($conn, "test");
if ($decrease=='10%')
$q1 = "update book set Price=Price-Price * 0.1 where ID='$id'";
else
$q1 = "update book set Price=Price-Price* 0.3 where ID='$id'";
$b = mysqli_query($conn, $q1);
$query = "select * from book where ID = '$id'";
$res = mysqli_query($conn, $query);
$r = mysqli_fetch_row($res);
print("<table border='1'><tr><td>ID</td><td>Name</td><td>Price</td> </tr>");
echo '<td>'.$r[0].'</td><td>'.$r[1].'</td><td>'.$r[2].'</td>';
} else {
// Do something else if it hasn't been posted
}
?>
此处也要注意引号的变化。变量不会在单引号字符串中处理,因此您需要在更新行上更改为双引号字符串。但是,最好将prepared statements与bind_param一起使用,这样您就不必担心引用或SQL注入。
答案 1 :(得分:0)
第一期
$id = isset($_POST["id"]);
isset()
函数将根据数组元素的可用性返回true或false。
正确的方法是
if(isset($_POST["id"]))
{
$id = $_POST["id"];
}
问题
$conn = mysqli_connect('localhost', 'root','');
$select = mysqli_select_db($conn, "test");
mysqli_connect的正确语法是:
$link = mysqli_connect("localhost", "root", "", "test");