繁体   English   中英

如何比较两个值?

[英]How to compare two values?

从我的代码中,我可以第一次输入一个值(当数据库为空时),即使对于使用不同工厂名称的不同用户,第二次也不起作用。 如果用户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;
}

您对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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM