寻找有关使用和/或语句来停止数据添加到数据库的指导

时间:2016-03-05 14:55:52

标签: php if-statement mysqli

您好,感谢会员在这里我设法排除了输入的电台名称,如果它在艺术家字段中,现在我被困在使用和/或声明..

基本上,有时候田径场也有车站名称,但不仅限于车站名称....

这是我到目前为止的代码......

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个字母中的电台名称。对此的任何指导都将受到赞赏,因为花了几个小时试图研究这个无济于事

1 个答案:

答案 0 :(得分:0)

首先,您应该从$ 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.

}

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