2

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

ZeJanIt
  • 61
  • 6

2 Answers2

0

Your code was incorrect, but this should work:

if (isset($submit)) {
    $date = date("Ymd");
    $cleanTitle =  $_POST['title'];
    $querytitle = mysql_real_escape_string($_POST['title']);
    $queryalbum = mysql_real_escape_string($_POST['album']);
    // !!! $artist is not actually set anywhere here..
    $checkTitle = mysql_query("SELECT * from lyrics WHERE artist = '$artist'");
    if (!$checkTitle) {
        die('Query Failed');
    } 
    /* now that you have run the query, you need to get the result: */
    // (This is assuming your query only returns one result)//
    $result = mysql_fetch_array($checkTitle);
    // now check the value for 'title'
    if ($result['title'] == $cleanTitle)  {
        // do whatever
    }
    print_r($result); // the data returned from the query
}

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:

// will print the 'title' for each results
while ($row = mysql_fetch_array($checkTitle)) {
    echo $row['title'];
}

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:

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 23,712
  • 16
  • 99
  • 132
burmat
  • 2,448
  • 1
  • 20
  • 28
  • the code works , but it seems like it is only grabbing the first entry to that artist not all of them and checking against them all. there could be 100 entries to the artists of just one – ZeJanIt Nov 07 '12 at 03:56
  • @user1804952 in that case, use the `while` loop example I posted underneath in the second block of code instead of `$result = mysql_fetch_array($checkTitle);`. This will cycle through all of your results one at a time until it reaches the bottom. If this answer helps you, please be sure to accept it. – burmat Nov 07 '12 at 04:01
  • and change if ($result['title'] == $querytitle) to if ($row['title'] == $querytitle) ? – ZeJanIt Nov 07 '12 at 04:10
  • @user1804952 if you want to compare 'title' for every row, then yes, you can use that within your while loop. `$row` and `$result` really indicate the same thing in my answer, they are both arrays of an entire row of results. `$results` in my answer is just the first result that is returned, while `$row` is used in the while loop to represent every row as they are read one at a time. – burmat Nov 07 '12 at 04:21
  • Why check the title in the while loop? Why not add it to the query? – Barmar Nov 07 '12 at 04:24
  • I need the if to look at each title . if i nest the if within the while it goes through each song title n does not die if match foiund – ZeJanIt Nov 07 '12 at 04:38
  • So if you need the loop to break, just use `break;` if a match is found. Simple solution.. – burmat Nov 07 '12 at 04:59
0

Here is very simple code to check if that title has any post (you may already know, that at first, the file needs to require wp-blog-header.php).

$title = 'mytitlee';

global $wpdb;
$id_ofpost_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = $title");
$id_ofpost_title = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = $title");
if ($id_ofpost_name || $id_ofpost_title) {echo 'Exists, here is the id:'.$id_ofpost_title.$id_ofpost_name;} 
else {echo 'post wasnt found';}
T.Todua
  • 44,747
  • 17
  • 195
  • 185