HBiz
HBiz

Reputation: 31

Why is MySQL INSERT statement not working without error

A php/mySQL booking function that's been working well suddenly stopped inserting booking entries into the database, with no changes to the code and a functioning database connection.

I run a parallel version of the page that is working on another website; the only difference between the two is that the broken version is running on php 5.6, the functioning one is still on 5.4.

Adding an error log brings no results even though the table doesn't update and I can't see any deprecated statements between php 5.4 and 5.6.

Can anyone spot the problem I'm missing?

 //If the confirm button has been hit:
if (isset($_POST['submit'])) {

//Create the foreach loop
  foreach ($_POST['class_id'] as $classes) {
  $class_id = (int)$classes;
  //UPDATE the bookings table **THIS PART IS NOT WORKING**:
  $query = "INSERT INTO bookings (user_id, booking_name, class_id, time_stamp) VALUES ('$user_id', '$username', '$class_id', NOW())";
  mysqli_query($dbc, $query);

}
foreach($_POST['class_id'] as $classes){
  $class_id = (int)$classes;
  //Change the booking numbers **THIS WORKS FINE**:
  $increase = "UPDATE classes SET online_bookings = (online_bookings + 1), total_bookings = (total_bookings + 1), free_spaces = (free_spaces - 1) WHERE class_id = $class_id";
  mysqli_query($dbc, $increase);
  }
   mysqli_close($dbc);

..and the table that provides the $_POST data:

echo'<div class="container">';
echo'<div class="span8 offset1 well">';
echo'<p class="lead text-info">Do you want to reserve space at these classes?</p>';



//table header
echo '<table id="dancers" class="table table-bordered table-hover">';
  echo '<thead><tr><th>Date</th><th>Time</th><th>Venue</th><th>Who\'s going?</th></tr></thead>';
//create the form
echo '<form id="makebkg" method="post" action="' . $_SERVER['PHP_SELF'] . '">';


//Get the class IDs from the GET to use in the POST

    foreach ($_GET['sesh'] as $class_id) {

    $sql = "SELECT class_id, DATE_FORMAT(date, '%a, %d %b') AS new_date, DATE_FORMAT(time, '%H:%i') AS new_time, venue FROM classes WHERE class_id = '$class_id'";
    $data = mysqli_query($dbc, $sql);


//get table data
    while ($row = mysqli_fetch_array($data)) {
    $date = $row["new_date"];
    $time = $row["new_time"];
    $venue = $row["venue"];
    $class_id = $row["class_id"];
  }


//Show a table of the selected classes


      echo '<tr><td>' . $date . '</td>';
      echo '<td>' . $time . '</td>';
      echo '<td>' . $venue . '</td>';
      echo '<td>' . $username . '</td></tr>';
      echo '<input type="hidden" name="date[]" value="' . $date . '" />';
      echo '<input type="hidden" name="time[]" value="' . $time . '" />';
      echo '<input type="hidden" name="venue[]" value="' . $venue. '" />';
      echo '<input type="hidden" name="username[]" value="' . $username . '" />';
      echo '<input type="hidden" name="class_id[]" value="' . $class_id . '" />';



    }
 echo'</table>';



    //Go Back button
    echo '<a class="btn btn-link pull-left" href="classes.php"><i class="icon-arrow-left"></i> Go back</a>';

  // Make booking button - LIVE
      echo'<div id="confirmbtn">';
     echo '<input type="submit" id="confirm" name="submit" class="btn btn-large btn-primary pull-right" value="Confirm">';
      echo '</div>';

Upvotes: 0

Views: 2925

Answers (2)

HBiz
HBiz

Reputation: 31

OK, I finally fixed the problem. It turns out that the hosting company had changed the MySQL mode to 'strict'.

The INSERT statement here left some table columns blank and strict mode rejects the entire insert as a result. Changing the mode right before the insert command was a quicker way to get around the problem than updating the insert command:

  // TURN OFF STRICT MYSQL MODE
  $strict = "SET sql_mode = ''";
  mysqli_query($dbc, $strict);

Thanks for all the advice and tolerance of an indolent coder.

Upvotes: 2

Mario
Mario

Reputation: 520

Did you try to check your query?

error_reporting(1);

$q = mysqli_query($dbc, $query);

if (!$q)
{
  echo 'Error' . mysqli_error($dbc);
}

Do same for other query.

Upvotes: -1

Related Questions