从我的代码中,我能够第一次输入一个值(当数据库为空时),即使对于具有不同工厂名称的不同用户,第二次也不起作用。如果用户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;
}
答案 0 :(得分:0)
您误认为query
,必须在选择表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;
}