简体   繁体   中英

Unable to insert data into MySQL using php form, database is connected

I'm doing a school assignment and a particular page I'm working on requires me to enter data into MySQL using php. The database is connected and I've referred to my codes from a previous class which is almost the same, apart from the variable names. I've tried many ways but somehow I can't get data to be inserted. Unable to work on my retrieval page unless this is up and running.. Please help!

<p class="lead">
<?php


  $submitted = isset($_POST['formSubmitted']);

  if ($submitted) {

        if (!empty($_POST['BookingDate'])) {
            $BookingDate=$_POST['BookingDate'];
        } else {
            $BookingDate=NULL;
            echo '<p><font color="red">You forgot to choose your date of booking!</font><p>';
        }

        if (!empty($_POST['CustName'])) {
            $CustName=$_POST['CustName'];
        } else {
            $CustName=NULL;
            echo '<p><font color="red">You forgot to enter your Name!</font><p>';
        }

        if (!empty($_POST['CustContact'])) {
            $CustContact=$_POST['CustContact'];
        } else {
            $CustContact=NULL;
            echo '<p><font color="red">You forgot to enter your contact number!</font><p>';
        }

        if (!empty($_POST['TableType'])) {
            $TableType=$_POST['TableType'];
        } else {
            $TableType=NULL;
            echo '<p><font color="red">You forgot to choose your table type!</font><p>';
        }

        if (!empty($_POST['TableLocation'])) {
            $TableLocation=$_POST['TableLocation'];
        } else {
            $TableLocation=NULL;
            echo '<p><font color="red">You forgot to choose your table location!</font><p>';
        }

        if (!empty($_POST['ICNumber'])) {
            $IcNumber=$_POST['ICNumber'];
        } else {
            $ICNumber=NULL;
            echo '<p><font color="red">You forgot to enter your IC Number!</font><p>';
        }

  if ($BookingDate && $CustName && $CustContact && $TableType && $TableLocation && $ICNumber) {
      echo '<h2><font color="green">Booking information has been entered successfully!</font></h2></p>';

      $BookingDate=$_POST['BookingDate'];
      $CustName=$_POST['CustName'];
      $CustContact=$_POST['CustContact'];
      $TableType=$_POST['TableType'];
      $TableLocation=$_POST['TableLocation'];
      $ICNumber=$_POST['ICNumber'];


      echo "The following details have been entered: </br>";
      echo '<ol>';
      echo "<li>Date: $BookingDate </br>";
      echo "<li>Name: $CustName </br>";
      echo "<li>Contact: $CustContact </br>";
      echo "<li>Table Type: $TableType </br>";
      echo "<li>Table Location: $TableLocation </br>";
      echo "<li>IC Number: $ICNumber </br>";
      echo '<ul></br></br>';

    $BookingDate = new DateTime;      
    $mysqli = new mysqli("localhost", "root", null, "kiewcRMAD");
    $stmt = $mysqli->prepare("INSERT INTO `kiewcRMAD`.`TableBooking` (`BookingDate`, `CustName`, `CustContact`, `TableType`, `TableLocation`, `ICNumber`) VALUES ('?', '?', '?', '?', '?', '?')");
    $stmt->bind_param('ssisss', $BookingDate, $CustName, $CustContact, $TableType, $TableLocation, $ICNumber);
    $stmt->execute();
    $stmt->store_result();
    $stmt->close();



    }
  }



  ?>


<fieldset>
  <legend> Enter Booking Information in the form below:</legend>


  <p>
  <b>Date of Booking:</b>
  <input type="text" name="BookingDate" size="30" value="<?php if (isset ($_POST['BookingDate']))echo $BookingDate;?>"/>
  </p>



  <p>
  <b>Name:</b>
  <input type="text" name="CustName" size="30" maxlength="20" value="<?php if (isset ($_POST['CustName']))echo $CustName;?>"/>

  </p>

  <p>
  <b>Contact:</b>
  <input type="number" name="CustContact" size="15" maxlength="8" value="<?php if (isset ($_POST['CustContact']))echo $CustContact;?>"/>
  </p>

  <p>
  <b>Table Type:</b>
  <SELECT size="1" name="TableType">
    <OPTION value="">--Select--</OPTION>
    <OPTION value="Standing Table"<?php if ((isset ($_POST['TableType'])) && ($_POST['TableType'] == "Standing Table")) echo "selected='selected'";?>>Standing Table</OPTION>
    <OPTION value="Single Sofa" <?php if ((isset ($_POST['TableType'])) && ($_POST['TableType'] == "Single Sofa")) echo "selected='selected'";?>>Single Sofa</OPTION>
    <OPTION value="Double Sofa"<?php if ((isset ($_POST['TableType'])) && ($_POST['TableType'] == "Double Sofa")) echo "selected='selected'";?>>Double Sofa</OPTION>
    <OPTION value="VIP Table"<?php if ((isset ($_POST['TableType'])) && ($_POST['TableType'] == "VIP Table")) echo "selected='selected'";?>>VIP Table</OPTION>
  </SELECT>
  </p>


  <p>

  <b>Table Location:</b>
  <input type="radio" name="TableLocation" value="King's Lounge" <?php if ((isset ($_POST['TableLocation'])) && ($_POST['TableLocation'] == "King's Lounge")) echo "checked'";?>/> King's Lounge 
  <input type="radio" name="TableLocation" value="Queen's Loft" <?php if ((isset ($_POST['TableLocation'])) && ($_POST['TableLocation'] == "Queen's Loft")) echo "checked'";?>/> Queen's Loft

  </p>

  <p>
  <b>IC Number:</b>
  <input type="text" name="ICNumber" size="15" maxlength="10" value="<?php if (isset ($_POST['ICNumber']))echo $ICNumber;?>"/>
  </p>

  </fieldset>

  <input type="hidden" name="formSubmitted" value="TRUE" />

  <input type="submit" name="submit" value="Submit!" />
  <input type="reset" value="Reset" />


</form>

Since I'm not sure if you're getting any notifications for messages left in the comments area (or aware of that section), am submitting the following as an actual answer.
Let's see if you do get a notification for it.

You're using quotes around each ? placeholder, which in turn is acting as passing a literal string instead of using for its intended purpose, a placeholder.

Remove the quotes (?, ?, ?, ?, ?, ?)

Consult the manual:

Example from that page:

/* Prepared statement, stage 1: prepare */
if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

Having used error checking such as, and in place of $stmt->execute();

if(!$stmt->execute()){
  trigger_error("there was an error....".$mysqli->error, E_USER_WARNING);
}

would have signaled and shown you the syntax error.


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM