tomhoad Posted September 23, 2009 Share Posted September 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175259-solved-whats-wrong-with-this-php-sql-query/ Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 have you checked the contents of $user_id to see if it contains anything? Quote Link to comment https://forums.phpfreaks.com/topic/175259-solved-whats-wrong-with-this-php-sql-query/#findComment-923695 Share on other sites More sharing options...
tomhoad Posted September 23, 2009 Author Share Posted September 23, 2009 All the 'gets' are working and pulling in the correct variables. Quote Link to comment https://forums.phpfreaks.com/topic/175259-solved-whats-wrong-with-this-php-sql-query/#findComment-923698 Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175259-solved-whats-wrong-with-this-php-sql-query/#findComment-923703 Share on other sites More sharing options...
tomhoad Posted September 23, 2009 Author Share Posted September 23, 2009 You were almost there: $search = "SELECT * FROM users_songs WHERE users_songs_user_id = '$user_id' AND users_songs_song_id = '$song_id'"; Thanks a lot nuttycoder!! Quote Link to comment https://forums.phpfreaks.com/topic/175259-solved-whats-wrong-with-this-php-sql-query/#findComment-923706 Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 not a problem glad you got it working. Quote Link to comment https://forums.phpfreaks.com/topic/175259-solved-whats-wrong-with-this-php-sql-query/#findComment-923710 Share on other sites More sharing options...
tomhoad Posted September 23, 2009 Author Share Posted September 23, 2009 how do I mark as solved? Quote Link to comment https://forums.phpfreaks.com/topic/175259-solved-whats-wrong-with-this-php-sql-query/#findComment-923712 Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 erm now your asking lol I think the is a button either at the top of the post or the bottom on the right hand side. Quote Link to comment https://forums.phpfreaks.com/topic/175259-solved-whats-wrong-with-this-php-sql-query/#findComment-923715 Share on other sites More sharing options...
tomhoad Posted September 23, 2009 Author Share Posted September 23, 2009 Very obscure 'solved' link bottom left :-\ Quote Link to comment https://forums.phpfreaks.com/topic/175259-solved-whats-wrong-with-this-php-sql-query/#findComment-923716 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.