First stackoverflow question ever woot!
FUNCTION : To check and see if data exist before allowing INSERT – trying to make it non-case senstive and as open as possbile since the title I’m trying to avoid a dup is only for a specifc artistid (explained below)
The table row structure is as follows
- id (auto_increment)
- artist (specfic id number only assigned to that artist)
- title (what we are trying to make sure we don’t get a dupicate only for this artist
ISSUE : Does not get needed data from database or post defined error, might be wrong in if statement = unknown exactly what is issue
$_POST[‘title’]; is passed from user input
if (isset($submit)) {
$date = date("Ymd");
$cleanTitle = $_POST['title'];
$querytitle = mysql_real_escape_string($_POST['title']);
$queryalbum = mysql_real_escape_string($_POST['album']);
// Check to see if Title exist for specfic Artist
$checkTitle = mysql_query("SELECT * from lyrics WHERE artist = '$artist'");
if (!$checkTitle) {
die('Query Failed');
}
if ($checkTitle == $cleanTitle) {
// do whatever
}
print_r($checkTitle); // the data returned from the query
UPDATE : INSERT IGNORE wouldn’t work sicne I’m inserting the data via $artist and need to check and see if title exist on that artist first. or i might be wrong. i’m unsure on how to do it
$artist is a specfic ID number defined higher in the code
Your code was incorrect, but this should work:
You were running the query, but you were not getting the results of the query. You use
mysql_fetch_array()to get the results. To get the results of multiple entries, you can use the following:Now with all of that said, you should know that mysql_* is going through a deprecation process and should be replaced with mysqli or PDO. Please read the following: