cjbeck71081 Posted April 18, 2007 Share Posted April 18, 2007 I am working on a project. I want to safegaurd against a user entering a number into the database multiple times. So what i want to do is run a query and check the users entry against the database, and then return an error if it exists. Here is how i thought it would work The script would take the Text Field posted and asign it a variable $var = $POST_['textfld']; Then i thought it might work if i were to say $var2 = mysql_query("SELECT item FROM table WHERE var = '$var'"); Then i could have a statement similar to if { $var = $var2 echo "Number already entered into Database"; } else echo "Thank You"; I thought that would be a good way to make it work, but its not working so far, any suggestions? Link to comment https://forums.phpfreaks.com/topic/47621-check-to-see-if-a-variable-exists-in-an-sql-database/ Share on other sites More sharing options...
freakstyle Posted April 18, 2007 Share Posted April 18, 2007 hey there cjbeck71081, this should be what your looking for: $var = $POST_['textfld']; $result = mysql_query("SELECT item FROM table WHERE var = '$var'"); if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } while ($row = mysql_fetch_assoc($result)) { $var2 = $row["textfld"]; } mysql_free_result($result); ----------------------------------- the issue is your $var2 is a resullt of the query and not the actual data. for more info check here: http://us.php.net/manual/en/function.mysql-fetch-assoc.php good luck! Link to comment https://forums.phpfreaks.com/topic/47621-check-to-see-if-a-variable-exists-in-an-sql-database/#findComment-232519 Share on other sites More sharing options...
cjbeck71081 Posted April 19, 2007 Author Share Posted April 19, 2007 Thanks for your response. I tried it with no luck, it seems to pass right over the code. Any suggestions. I'll post the actual code tomorrow and see if anyone has any suggestion. Link to comment https://forums.phpfreaks.com/topic/47621-check-to-see-if-a-variable-exists-in-an-sql-database/#findComment-232765 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.