Hi guys, I am quite new to PHP and MySQL. I have created a form for a booking form and i want to check the username is valid against one in the database. If it is not valid i would like an error message to come up. Does anyone know the syntax for this?
$username = $_POST['user']; //Gets username from the form $query = mysql_query("SELECT * FROM `tablename` WHERE `user` = $username"); //Searches table "tablename" for user $num = mysql_affected_rows($query);//Counts the amount of rows returned/affected if($num != 0 ){ //if # of rows affected is not equal to 0 echo "user found!"; //user found }else{ //otherwise echo "user NOT found!"; //user not found } PHP:
small issue: sql injection use this code instead $query = mysql_query("SELECT * FROM `tablename` WHERE `user` = '".mysql_real_escape_string(stripslashes($username))."'); PHP: Another issue In your case (SELECT command) instead of mysql_affected_rows, use mysql_num_rows($query);