ngreenwood6 Posted July 31, 2008 Share Posted July 31, 2008 I am trying to set-up a script that will check to see if there email is already there or not. if it is it will give them an error and not insert it into the database and if it is not then it will insert it into the database. this is my code: <?php //include our variables include ("variables.php"); //include database variables include ("db.php"); //variable to connect to database $mysqli_connect = mysqli_connect($host,$db_username,$db_password,$db_name) or die ("Could not connect to database"); //variable to send data to the database mysqli_query($mysqli_connect,$insertinto_db) or die ("Error: ".mysqli_error($mysqli_connect)); //error handling if (!$firstname) { echo ("Please enter a first name"); } else if (!$lastname) { echo ("Please enter last name"); } else if (!$age) { echo ("Please enter your age"); } else if (!$weight) { echo ("Please enter your weight"); } else if ($age > 99) { echo ("Please enter a valid age"); } else if ($weight > 999) { echo ("Please enter a valid weight"); } else { echo ("Thank you for your submission $firstname!"); } ?> This works as far as telling them thank you or giving them the error. The problem is whenever there is an error it still puts the data in the database. Can anyone help? Link to comment https://forums.phpfreaks.com/topic/117633-solved-how-to-check-database/ Share on other sites More sharing options...
Stooney Posted July 31, 2008 Share Posted July 31, 2008 Can you show us the actual query? edit: found it. ignore me Link to comment https://forums.phpfreaks.com/topic/117633-solved-how-to-check-database/#findComment-605024 Share on other sites More sharing options...
akitchin Posted July 31, 2008 Share Posted July 31, 2008 i'm going to guess that it's because you're inserting the data before even running any error checks. that means that even if your script throws an error, it doesn't matter because the insertion has already been performed. move the query to the else case in your big long if-else set: //error handling if (!$firstname) { echo ("Please enter a first name"); } else if (!$lastname) { echo ("Please enter last name"); } else if (!$age) { echo ("Please enter your age"); } else if (!$weight) { echo ("Please enter your weight"); } else if ($age > 99) { echo ("Please enter a valid age"); } else if ($weight > 999) { echo ("Please enter a valid weight"); } else { //variable to send data to the database mysqli_query($mysqli_connect,$insertinto_db) or die ("Error: ".mysqli_error($mysqli_connect)); echo ("Thank you for your submission $firstname!"); } Link to comment https://forums.phpfreaks.com/topic/117633-solved-how-to-check-database/#findComment-605025 Share on other sites More sharing options...
ngreenwood6 Posted July 31, 2008 Author Share Posted July 31, 2008 not sure what you mean by this: move the query to the else case in your big long if-else set: Link to comment https://forums.phpfreaks.com/topic/117633-solved-how-to-check-database/#findComment-605029 Share on other sites More sharing options...
akitchin Posted July 31, 2008 Share Posted July 31, 2008 i posted the code to reflect what i meant. move these lines: //variable to send data to the database mysqli_query($mysqli_connect,$insertinto_db) or die ("Error: ".mysqli_error($mysqli_connect)); which are the lines that are executing your query for you, into the else case of your big conditional block: else { //variable to send data to the database mysqli_query($mysqli_connect,$insertinto_db) or die ("Error: ".mysqli_error($mysqli_connect)); echo ("Thank you for your submission $firstname!"); } Link to comment https://forums.phpfreaks.com/topic/117633-solved-how-to-check-database/#findComment-605035 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.