簡體   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