[英]PHP Form not posting to a MySQL database
我正在为我的雇主创建一个新的预订系统,在其中填写表格,数据输入预先建立的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天。 感谢您提供所有答案。
include('booking_db.php');
这一行可能有问题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!";
}
提交表单后,请检查您得到什么。
将您的插入查询替换为:
INSERT INTO Future
(`name`,`out`,`in`) VALUES ('".$name."', '".$out."', '".$in."')
和if (isset($_POST['submitted']))
和if (isset($_POST['Confirm Booking']))
因为您必须将提交按钮的值放在POST中
原来,我的问题是
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);
}
并更改了一些变量以与此匹配,然后它开始工作并发布到我的数据库中。 谢谢所有回答的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.