0

I have the following prepared statement which works and allows me to enter information into the table as required. However I'm not getting any return for the if-not part of the statement. I'd like the error message to appear on the website if the information is not input into the table for example if I have a duplicate entry it will not work with my table but I just get the else{} part and booking has been registered when in fact the table has not changed.

$stmt = mysqli_stmt_init($connection);
if(!mysqli_stmt_prepare($stmt, $sql2)){
  
  echo "
  <div class='registered'>
  Cannot book same activity twice
  </div>";

 } else {
  mysqli_stmt_bind_param($stmt, "ssss", $activityID, $db_id, $date_of_activity, $number_of_tickets);
  mysqli_stmt_execute($stmt);

  echo "<div class='registered'>
  <h2>Your booking has now been registered<h2>
  </div>
  ";
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
DeeJJx
  • 11
  • 4
  • Try this `if(mysqli_stmt_execute($stmt)){ echo "your message ";}` and search for form validation using php –  Jan 10 '21 at 12:41
  • I'm still getting "booking has been registered" when I should get "cannot book" – DeeJJx Jan 10 '21 at 12:50
  • You dont do validating for in coming data example : `if(empty($activityID)){ echo "field is empty";}` if you dont validate or check for values coming from html form it will insert data null or empty on your database setup. Again search for form validation using php. see this example scrol down to Create page https://www.tutorialrepublic.com/php-tutorial/php-mysql-crud-application.php –  Jan 10 '21 at 12:55
  • Remove all these `if` statements and enable proper error reporting instead. – Dharman Jan 10 '21 at 13:09

1 Answers1

-2

You are only checking if you're prepared statement can be created... you are not actually executing the query there.

You should check if this line actually succeeded:

mysqli_stmt_execute($stmt);

Dennis
  • 371
  • 1
  • 9