无法使用php表单将数据插入MySQL,数据库已连接

时间:2015-03-04 14:13:36

标签: php mysql

我正在完成学校作业和我正在处理的特定页面要求我使用php将数据输入MySQL。数据库是连接的,除了变量名之外,我还引用了上一个类的代码,它几乎是相同的。我已经尝试了很多方法,但不知怎的,我无法插入数据。除非启动并运行,否则无法在我的检索页面上工作..请帮忙!

<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>

1 个答案:

答案 0 :(得分:2)

由于我不确定您是否收到了评论区域中留下的消息的任何通知(或了解该部分),因此我将提交以下内容作为实际答案。
让我们看看你是否收到了通知。

您在每个?占位符周围使用引号,而占位符又用作传递文字字符串而不是用于其预期用途的占位符。

删除引号(?, ?, ?, ?, ?, ?)

参考手册:

该页面的示例:

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

使用了错误检查,例如,代替$stmt->execute();

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

会发出信号并向您显示语法错误。


error reporting添加到文件的顶部,这有助于查找错误。

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

// rest of your code

旁注:错误报告应仅在暂存时完成,而不是生产。