Jump to content

[SOLVED] What's wrong with this PHP SQL Query?


tomhoad

Recommended Posts

Hi there.

 

My database table is as follows:

 

users_songs_user_id  |  users_songs_songs_id

-------------------------------------------------------

  [email protected]    |                1

[email protected]  |                2

 

My code is as follows:

 

$song_id = mysql_real_escape_string($_GET['song_id']);
$user_id = mysql_real_escape_string($_GET['user_id']);
$user_id = urldecode($user_id);

$search = "SELECT users_songs_song_id FROM users_songs WHERE users_songs_user_id = '$user_id'";

$result = mysql_query($search) or trigger_error('Error: ' . mysql_error());

$numrows = mysql_num_rows($result);

if($numrows < 1)
{
$query = " INSERT INTO users_songs (users_songs_user_id, users_songs_song_id) ". 
" VALUES ('$user_id', '$song_id')"; 
        mysql_query($query) or die('Error : ' . mysql_error());

print "Success!";
}
else
{
        print "Fail";
}

 

The $search SQL query appear to always return '1' as the numrows, and therefore I always get 'Fail'. I can't work out why - any help very much appreciated!

 

Cheers

maybe try adding a value into the query directly as a test

 

$search = "SELECT users_songs_song_id FROM users_songs WHERE users_songs_user_id = '[email protected]'";

 

or try selecting * (all) see if it makes any difference

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.