[英]PHP Form not posting to a MySQL database
I'm creating a new booking system for my employer, in which a form is filled in and data enters a pre-built MySQL database. 我正在为我的雇主创建一个新的预订系统,在其中填写表格,数据输入预先建立的MySQL数据库。
I'm honestly unsure as to what I am doing wrong. 老实说,我不确定自己在做什么错。 Originally the data would not post into the database, but the form would appear to have submitted.
最初,数据不会发布到数据库中,但是表单似乎已经提交了。 Now, the form just submits to a white page.
现在,表单仅提交到白页。 I will submit the full page code below as there's no comprimising data there, and hopefully somebody will be able to help.
我将在下面提交完整的页面代码,因为那里没有妥协的数据,希望有人能够提供帮助。
<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>
If you need any more information, within reason, feel free to ask. 如果您需要更多信息,请在合理范围内询问。
EDIT This issue has been resolved, I cannot mark the answer as it was my answer and I have to wait 2 days. 编辑此问题已解决,我无法将答案标记为答案,因此我必须等待2天。 THank you for all of the answers.
感谢您提供所有答案。
There could be a problem in the line include('booking_db.php');
include('booking_db.php');
这一行可能有问题include('booking_db.php');
. 。 You should mention
error_reporting(E_ALL);
您应该提到
error_reporting(E_ALL);
at the top of the page, and try debugging: 在页面顶部,然后尝试调试:
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!";
}
Please check what you're getting after form submit. 提交表单后,请检查您得到什么。
Replace your insert query with : 将您的插入查询替换为:
INSERT INTO Future
(`name`,`out`,`in`) VALUES ('".$name."', '".$out."', '".$in."')
and if (isset($_POST['submitted']))
with if (isset($_POST['Confirm Booking']))
和
if (isset($_POST['submitted']))
和if (isset($_POST['Confirm Booking']))
because you have to put your value of submit button in POST 因为您必须将提交按钮的值放在POST中
Turns out, my issue was with 原来,我的问题是
if (!mysqli_query ($dbcon, $sqlinsert)) {
die('error inserting new record');
}
$newrecord = "Laptop has been successfully Booked!";
}
I changed this to 我将其更改为
if (mysqli_query ($link, $sqlinsert)) {
echo "";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
And changed a few variables to match up with this, and it started working and posted to my DB. 并更改了一些变量以与此匹配,然后它开始工作并发布到我的数据库中。 Thank you to anyone who answered.
谢谢所有回答的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.