简体   繁体   中英

Stopping an entry being added to mysql database if the value = given parameter

I am building a database of latest tracks played on a station and I do not want entries with the station name being added to the database.

I know that I will need to use if else and else if because of what I want to do as I am already checking if entry exists with if and else so am just looking for some pointers on how to stop entries being added if radio name is in the $trackartist field

I know I need to do something along the lines of if $trackartist = station name do nothing else check if entry exists else if do the insert.

Here is the code I am using to fill the rows.

include('config.php');

$str = file_get_contents('xxxxxxxx.xxx');
$json = json_decode($str, true);
$artist = $json['now_playing']['artist'];
$track = $json['now_playing']['track']; 
$cover = $json['cover'];

$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);
 };

Just nest all of your code in an if statement:

if($artist != "Station Name"){

    $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);
     };

 }

This will run your code as long as $artist does not equal whatever string you substitute for "Station Name" if it does match this code will do nothing.

You could also block multiple things if you want like this:

$starray = array("Station Name 1","Station Name 2","Station Name 3");

then change if($artist != "Station Name") to if(!in_array($artist,$starray))

First: really, relly use prepared statement and DO NOT use variables in your query (potential SQL Injection)!

I assume that $statinName is sth you have predefined and can use.

To prevent yourself from going into more and more complicated or nested if statements I would suggest you to use exceptions, for example sth like this:

Add new file RecentTrackDataInvalid.php

<?php
class RecentTrackDataInvalid extends Exception {}

Make your code to do sth like this:

require('RecentTrackDataInvalid.php');
include('config.php');

$str = file_get_contents('xxxxxxxx.xxx');
$json = json_decode($str, true);
$artist = $json['now_playing']['artist'];
$track = $json['now_playing']['track']; 
$cover = $json['cover'];

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

try
{
  if ($trackartist === $stationName) {
    throw new RecentTrackDataInvalid("Track artis equals station name");
  }

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

  if($checkrows>0) {
    throw new RecentTrackDataInvalid("track exists");
  }  

  $sql = "INSERT IGNORE INTO recent (trackartist, tracktitle, coverurl)
  VALUES ('$artist', '$track', '$cover')";
  $result = mysqli_query($mysqli, $sql) or die('Error querying   database.');
} catch(RecentTrackDataInvalid $e) {
  echo $e->getMessage();
}

mysqli_close($mysqli);

Code executon in try block will be stop when any Exception will be thrown. This way you can add as much validators as you want and do not need to test or combine conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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