Php 正在寻找有关使用and或语句停止向数据库中添加数据的指导

Php 正在寻找有关使用and或语句停止向数据库中添加数据的指导,php,if-statement,mysqli,Php,If Statement,Mysqli,您好,多亏了这里的一位会员,我成功地排除了正在输入的电台名称,如果它在艺术家字段中,现在我一直在使用and或语句 基本上,有时田径场也有站名,但不限于站名 这是我到目前为止的代码 if($artist != "stationname" ){ $check = mysqli_query($mysqli,"select * from recent where trackartist='$artist' and tracktitle='$track' and coverurl='$co

您好,多亏了这里的一位会员,我成功地排除了正在输入的电台名称,如果它在艺术家字段中,现在我一直在使用and或语句

基本上,有时田径场也有站名,但不限于站名

这是我到目前为止的代码

if($artist != "stationname" ){

$check = mysqli_query($mysqli,"select * from recent where        trackartist='$artist' and tracktitle='$track' and coverurl='$cover'");
$checkrows=mysqli_num_rows($check);

if($checkrows>0) {
  echo "track exists";
 } else {  

  $sql = "INSERT IGNORE INTO recent (trackartist, tracktitle, coverurl)
  VALUES ('$artist', '$track', '$cover')";
  $result = mysqli_query($mysqli, $sql) or die('Error querying     database.');

  mysqli_close($mysqli);
 };

 }

我需要的一些指导是,如果$artist=station name&或$track在$track的前7个字母中包含station name,则如何停止输入。如果您花费数小时试图对此进行研究,但没有结果,请提供任何指导。

首先,您应该从$track创建一个子字符串,该子字符串是第一个字符的子字符串7封信。之后,在if语句中使用它就相当容易了。 以下是我的建议:

   $substr_tracks=substr($track,0,6)//if $track is for example 'computer' than $substr_tracks will be 'compute'.
    //now you create a boolean which will check if  'stationname' is in the substring we created.
    $bool=false;
    //now we do an if statement and use the strpos function which will check the occurence of a string or character in another string and return a number or false.

if(strpos($substr_tracks,'stationname')!=false){

    $bool=true;

} 

    //now you can do your main stuff:

if(($artist != "stationname" && !$bool) && ($artist!='stationname' || !$bool )){


//enter data into database


}else{

//do not enter into database.

}

我希望我明白你在找什么。我已准备好回答任何进一步的问题。

如果我正确理解仅检查artist=station name&trackname是否包含,我需要它并编辑代码,但您也可以使用elseif语句来生成条件。代码示例中的括号是一个简单的技巧。这段代码告诉我一个错误,说Parse error:syntax error,意外的“$bool”(T_变量)在$bool=false上;代码中应该有错误,否则此错误与$bool=false无关。我能看看你的完整代码吗?