无法将东西放入数据库

Posted

技术标签:

【中文标题】无法将东西放入数据库【英文标题】:Cannot put stuff into the database 【发布时间】:2016-10-31 15:51:04 【问题描述】:

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

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

【问题讨论】:

你的Thanks.php中没有定义$booking_sql 我如何定义它?我仍处于了解 php 的早期阶段,因此不胜感激。 【参考方案1】:

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

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

删除了所有这些注释行

session_start() 应始终位于程序的顶部(在 ini 指令之后),如果您在包含的文件中有一些意外输出,则会导致致命错误,否则会导致致命错误

我使用准备好的语句来处理从用户那里获得的数据,这样如果输入中有特殊字符,查询的格式就不会被破坏

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

【讨论】:

@jeff 如果对您有帮助,请不要忘记接受他的回答! :)

以上是关于无法将东西放入数据库的主要内容,如果未能解决你的问题,请参考以下文章

Spark:数据无法放入内存,我想避免将其写入磁盘,我可以对数据切片执行迭代以仅使用内存吗?

将 JSON 数据从 Request 转换为 Pandas DataFrame

如何将sql数据放入php表中

输出中的数据表 json 问题:无法设置未定义的属性“nTf”

SQLITE3 - 使用循环将数据放入列中

neo4j - py2neo - 无法在创建时将参数放入关系中