Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.