无法将东西放入数据库

时间:2016-06-29 05:33:21

标签: php html mysql session phpmyadmin

我正在尝试将表单结果放入数据库但它无法正常工作。似乎没有错误发生,我的代码出了什么问题? 这是我的第一页预订表格:     

$booking_sql = "SELECT * FROM calendar, missions WHERE calendar_id = '%d' AND calendar.missions_id = missions.missions_id" ;
$_SESSION['booking']['calendar'];
$booking_query = mysqli_query($dbconn, $booking_sql) or die(mysqli_error());
$rsBooking = mysqli_fetch_assoc($booking_query);
?>
 <form action="confirm.php" method="post" name="fmNumCon" id="fmNumCon">
        <div class="row"><span class="label"><strong class="full"></strong></span> <span class="element"><h4><?php echo $rsBooking['missions_name'].', '.$rsBooking['calendar_date']; ?></h4></span></div>
        <div class="row"><span class="label"><h4>*Number of people:</h4></span><span class="element">
          <select name="number" id="number" onchange="getNumber()">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
          </select>
          </span></div>
       <!--  <div class="row"><span class="label"</span><span class="element"><h4>$NZDisplay cost per person.00</h4><input type="hidden" name="price" id ="price" value="" /></span></div>
        <div class="row"><span class="label">&nbsp;</span><span class="element">
          <a id="update" href="booking.php?mode=update">Update cost</a>
        </span></div> -->
        <div class="row"><strong class="full"><h4>Conatact details:</h4></strong>&nbsp;</div>
        <div class="row">
          <input name="name" type="text" id="name" value="<?= isset($name) ? $name : ''?>" placeholder="name"/>
         </div>




        <div class="row">
          <input name="$phone" type="text" id="phone" placeholder="phone" value="<?= isset($phone) ? $phone : ''?>"/>
          </div>

        <div class="row">
          <input name="email" type="text" id="email" placeholder="email" value="<?= isset($email) ? $email : ''?>"/>
          </div>
        <div class="row">
          <input type="reset" name="Reset" value="Reset" />

          <input type="submit" name="Submit" value="Continue" />
          </div>
      </form>

以下是确认页面:     

$number = $_SESSION['booking']['number'];
$phone = $_POST['phone'];
$email = $_POST['email'];



require_once('includes/dbconn.php');

$booking_sql = sprintf("SELECT * FROM calendar, missions WHERE calendar_id = '%d' AND calendar.missions_id = missions.missions_id", $_SESSION['booking']['calendar']);
$booking_query = mysqli_query($dbconn, $booking_sql) or die(mysqli_error());
$rsBooking = mysqli_fetch_assoc($booking_query);
?>
 <form action="thanks.php" method="post" name="fmConfirm" id="fmConfirm" display="hidden">
        <div class="row"><?php echo $rsBooking['missions_name']; ?></div>
        <div class="row"><input type="hidden" name="number" value ="<?php echo $number?>"></input></div>

        <div class="row"><input type="hidden" name="name" value ="<?php echo $name?>"></input></div>



        <div class="row"><input type="hidden" name="phone" value ="<?php echo $phone?>"></input></div>

        <div class="row"><input type="hidden" name="email" value ="<?php echo $email?>"></input></div>
    <div class="row"><span class="label">&nbsp;</span> <span class="element"><a href="booking.php?calendar_id=<?php echo $_SESSION['booking']['calendar']; ?>&number=<?php echo $_SESSION['booking']['number']; if (!$_COOKIE['PHPSESSID']) { echo '&'.SID; } ?>">Edit details</a></span></div>
        <div class="row"><span class="label">&nbsp;</span><span class="element">
          <input type="submit" name="Submit" value="Finsih" />
          </span></div>
      </form>

这是我的感谢页面:

<?php require_once('includes/dbconn.php'); ?>
<?php session_start();
$date = date('Y-m-d');
// $calendar = $_POST['calendar_date'];
$name = $_POST['name'];
$number = $_POST['number'];
$phone = $_POST['phone'];
$email = $_POST['email'];
// $booking_sql = "INSERT into bookings (calendar_id, booking_name, 
// booking_number, booking_phone, 
// booking_email, booking_date) VALUES ('$calendar','$name', 'number', 'phone', 'email')";
$booking_sql = "INSERT INTO bookings (booking_name, 
booking_number, booking_phone, 
booking_email, booking_date)
        VALUES ('$name','$number','$phone','$date')";
    $dbconn->query($booking_sql);


// $sql = "INSERT INTO bookings (booking_fname, booking_lname, booking_email, user_id, date_id)
//         VALUES ('$fName','$lName','$email', '$userid', '$dateid')";
//     $dbc->query($sql);


$booking_query = mysqli_query($dbconn, $booking_sql);
$_SESSION = array();
// session_destroy();
// if(!isset($_COOKIE['active'])) {
//   setcookie('active', 'no', time() + 31536000);
// }
?>
<h2>Booking complete </h2>
      <p>&nbsp;</p>
      <p>Thank you for choosing <strong>Mountain Bike Missions</strong>.</p>
      <p>To check your booking immediately, you can log in with your email address <a href="login.php">here</a>. At any another time, please use the <strong>Check booking</strong> link in the <strong>Booking</strong> section of the site.</p>

1 个答案:

答案 0 :(得分:1)

我稍微调整了你的thanks.php。

  • 在您使用mysqli的过程语法的其他脚本中,在thanks.php中,您切换到了对象语法

  • 删除了所有这些注释行

  • session_start()应该始终位于程序之上(在ini-directives之后),如果你在一个包含文件中有一些意外输出它会导致致命错误,否则

  • 我使用准备好的状态来处理从用户那里获得的数据,这样,如果输入中有特殊字符,则不能破坏查询的格式

  • 启用了display_errors,以便显示您的错误(当您完成测试后,您可以删除这些行,您不希望在高效时向用户显示技术错误消息)

  • 使用trigger_error来捕获数据库抛出的错误,将它们视为php错误

代码如下所示:

<?php 
/*
* enable display_errors
*/
ini_set('display_errors', 1);
error_reporting(-1);

/*
* start session, construct connection to db
*/
session_start();
require_once('includes/dbconn.php');

/*
* define your variables
*/
$date = date('Y-m-d');
$name = $_POST['name'];
$number = $_POST['number'];
$phone = $_POST['phone'];
$email = $_POST['email'];

/*
* define your sql-statemnt
* send the statement to the database "preparation"-process
* check if the database had any troubles by asking it for errors
*/
$booking_sql = "INSERT INTO bookings (booking_name, booking_number, booking_phone, booking_email, booking_date) VALUES (?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($dbconn ,$booking_sql);
if(mysqli_errno($dbconn) !== 0){
    trigger_error(mysqli_error($dbconn));
}

/*
* bind your variables to the prepared statement
* execute the statement
* again, ask if the database had any troubles
*/
mysqli_stmt_bind_param($stmt, "sssss", $name, $number, $phone, $email, $date);
mysqli_stmt_execute($stmt);
if(mysqli_errno($dbconn) !== 0){
    trigger_error(mysqli_error($dbconn));
}

$_SESSION = array();
?>
<h2>Booking complete </h2>