我正在为我的雇主创建一个新的预订系统,其中填写一份表格,数据进入预建的MySQL数据库。
我真的不确定我做错了什么。最初数据不会发布到数据库中,但表单似乎已提交。现在,表单只是提交到白页。我将提交下面的完整页面代码,因为那里没有令人信服的数据,希望有人能够提供帮助。
<head>
<title> Moat Laptop Bookinge </title>
<?php
if (isset($_POST['submitted'])) {
include('booking_db.php');
$name = $_POST['name'];
$out = $_POST['out'];
$in = $_POST['in'];
$sqlinsert = "INSERT INTO Future (name, out, 'in') VALUES ('$name', '$out', '$in')";
if (!mysqli_query ($dbcon, $sqlinsert)) {
die('error inserting new record');
}
$newrecord = "Laptop has been successfully Booked!";
}
?>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("#datepicker").datepicker();
});
</script>
<script>
$(document).ready(function() {
$("#datepicker2").datepicker();
});
</script>
</head>
<body style="background-height: 100%;background-width: 100%;background: #141E30;background: -webkit-linear-gradient(to left, #141E30 , #243B55);background: linear-gradient(to left, #141E30 , #243B55);">
<div id="logo" style="font-family: Tw Cen MT; font-weight: Bold; position: fixed; color: white; left: 650px;top: 35px; font-size: 80px;text-shadow: 3px 3px #c7c7c7;">
Book a Laptop
</div>
<div id="content_box" style="background-color: white;position: fixed; left: 450px;top: 135px; width:60%; height: 70%; border-radius: 3px;">
<center>
<form method="post" action="book.php" style="font-family: Bodoni MT;">
<input type="hidden" name="submitted" value="true" />
<br />
<br />
<b><legend>First Name and First Letter of Surname</legend></b>
<input type="text" name="name" value="Ex. James T" />
<br/>
<br />
<b><legend>When will you need to collect the device?</legend></b>
<input id="datepicker2" name="out" />
<br/>
<br />
<b><legend>When will you return the device?</legend></b>
<input id="datepicker" name="in" />
<br />
<input type="submit" value="Confirm Booking" />
</center>
<?php
echo $newrecord
?>
</div>
</body>
如果您需要更多信息,请随时提出。
修改 这个问题已经解决,我无法标记答案,因为这是我的回答,我必须等待2天。感谢你们所有答案。
答案 0 :(得分:0)
行include('booking_db.php');
可能存在问题。您应该在页面顶部提到error_reporting(E_ALL);
,然后尝试调试:
error_reporting(E_ALL);
if (isset($_POST['submitted'])) {
var_dump(file_exists('booking_db.php')); //check if you get true or false
require 'booking_db.php'; // Change include to required
echo "Test";
exit;
$name = $_POST['name'];
echo $name; // Check
$out = $_POST['out'];
$in = $_POST['in'];
$sqlinsert = "INSERT INTO Future (name, out, 'in') VALUES ('$name', '$out', '$in')";
var_dump($dbcon); // check
if (!mysqli_query ($dbcon, $sqlinsert)) {
die('error inserting new record');
}
$newrecord = "Laptop has been successfully Booked!";
}
请在表单提交后检查您的内容。
答案 1 :(得分:0)
将插入查询替换为:
INSERT INTO Future
(`name`,`out`,`in`) VALUES ('".$name."', '".$out."', '".$in."')
和
if (isset($_POST['submitted']))
与if (isset($_POST['Confirm Booking']))
因为您必须在POST中提交提交按钮的值
答案 2 :(得分:0)
事实证明,我的问题是
if (!mysqli_query ($dbcon, $sqlinsert)) {
die('error inserting new record');
}
$newrecord = "Laptop has been successfully Booked!";
}
我将此更改为
if (mysqli_query ($link, $sqlinsert)) {
echo "";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
并更改了一些变量以与之匹配,它开始工作并发布到我的数据库。感谢所有回答的人。