从我的代码中,我能够第一次输入一个值(当数据库为空时),第二次它不起作用,即使对于具有不同植物名称的不同用户也是如此。如果用户1第一次输入植物名称,例如Apple,它将被保存,但不会在以后的任何时间保存。当我想要比较两个值时,这是编写MySQL代码的正确方式吗?
$plant = $_SESSION['plant'];
$user = $_SESSION['user'];
$q = mysqli_query($con, "select plantname='$plant' from tree where
user = '$user'");
if (mysqli_num_rows($q) > 0) {
Header('Location: home.php');
exit;
} else {
$query = "insert into tree(plantname,user)
values('$plant', '$user')";
$q = mysqli_query($con, $query)or die("Could Not Perform the Query");
Header('Location: home.php');
exit;
}发布于 2019-02-10 23:49:56
选择表from后,您必须传递查询条件,否则您的查询条件会出错
$plant=$_SESSION['plant'];
$user=$_SESSION['user'];
$q = mysqli_query($con,"select * from tree where user='$user' AND plantname='$plant'");
if (mysqli_num_rows($q)>0){
Header( 'Location: home.php' );
exit;
}else{
$query="insert into tree(plantname,user) values ('$plant','$user')";
$q=mysqli_query($con,$query)or die("Could Not Perform the Query");
Header( 'Location: home.php' );
exit;
}https://stackoverflow.com/questions/54618084
复制相似问题