gpt4 book ai didi

php - Bootstrap 模式表单中的数据不会使用 php 传输到 MySQL 表

转载 作者:行者123 更新时间:2023-11-29 12:33:35 24 4
gpt4 key购买 nike

我已经通过查看以前的问题答案来解决这个问题有一段时间了,但仍然不起作用。

当通过 PHP 提交时,我试图用 Bootstrap Modal 表单中的数据更新 MySQL 表。下面的 php 代码在以前的网站(不使用 bootstrap)中使用时可以工作。

我收到的问题是提交时;该表单通过了 JavaScript 验证,然后打开 register.php 文件。当我查看 MySQL 表时,数据尚未传输,网页只是空白,没有内容(网址显示为 http://localhost/BootstrapEx/php/register.php)。

请有人帮忙解释为什么使用 Twitter Bootstrap 时表单中的数据没有传输到 MySQL 以及解决此问题的方法?

我知道我必须更改/更新 php 文件中的安全性、验证和错误页面,但在这个阶段我想要它做的就是将数据加载到 MySQL 表中。我临时创建了Thankyou.html、SystemError.html 和RegError.html,正如您将在php 代码中看到的那样,只是为了查看这些页面是否打开。

请找到下面的代码:

Html:仅模态表单部分

<div class="modal fade" id ="Register" role ="dialog">
<div class="modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<h4>Registration Screen</h4>
</div>
<div class ="modal-body">




<form name="myForm" role="form" action="php/register.php" method ="post" onsubmit="return validateForm()">


<fieldset>
<div class="form-group">
<label for="fname">First Name</label><span>*</span>
<input type="FirstName" class="form-control" id="fname" placeholder="Enter your first name" name="fname">
</div>
<div class="form-group">
<label for="lname">Last Name</label><span>*</span>
<input type="Surname" class="form-control" id="lname" placeholder="Enter your last name or surname" name="lname">
</div>
<div class="form-group">
<label for="email">Email address</label><span>*</span>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
</div>
<div class="form-group">
<label for="psword1">Password</label><span>*</span>
<input type="password" class="form-control" id="psword1" placeholder="Password" name="psword1">
</div>
<div class="form-group">
<label for="psword2">Confirm Password</label><span>*</span>
<input type="password" class="form-control" id="psword2" placeholder="Confirm Password" name="psword2">
</div>

<div class = "form-group">
<a class = "btn btn-default" data-dismiss = "modal">Close</a>
<button class = "btn btn-primary" type="submit" name="submit" value="Yes">Register</button>
</fieldset>
</div>

</div>

</div>

</div>

</div>
</form>

register.php 文件(请原谅错误部分,这些内容将在数据提交到表中后更新)。

<?php
if(isset($_POST['submit']))
{
require ('php/mysqli_connect.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$errors = array(); // Initialize an error array.
// Check for a first name:
if (empty($_POST['fname']))
{
$errors[] = 'You forgot to enter your first name.';
} else
{
$fn = mysqli_real_escape_string($dbcon, trim($_POST['fname']));
}
// Check for a last name:
if (empty($_POST['lname']))
{
$errors[] = 'You forgot to enter your last name.';
} else
{
$ln = mysqli_real_escape_string($dbcon, trim($_POST['lname']));
}
// Check for an email address:
if (empty($_POST['email']))
{
$errors[] = 'You forgot to enter your email address.';
} else
{
$e = mysqli_real_escape_string($dbcon, trim($_POST['email']));
}
// Check for a password and match against the confirmed password:
if (!empty($_POST['psword1']))
{
if ($_POST['psword1'] != $_POST['psword2'])
{
$errors[] = 'Your two passwords did not match.';
} else
{
$p = mysqli_real_escape_string($dbcon, trim($_POST['psword1']));
}
} else
{
$errors[] = 'You forgot to enter your password.';
}

if (empty($errors))
{ // If everything's OK.
// Register the user in the database...

// Make the query:

$q = "INSERT INTO users (user_id, fname, lname, email, psword, registration_date) VALUES (' ', '$fn', '$ln', '$e', SHA1('$p'), NOW() )";
$result = @mysqli_query ($dbcon, $q); // Run the query.
if ($result)
{ // If it ran OK

header("Location:http://localhost/BootstrapEx/Thankyou.html");

echo '<p>Fields Loaded</p>';
exit();

} else
{ // If it did not run OK
// Error message:

header("Location:http://localhost/BootstrapEx/SystemError.html");
echo '<h2>System Error</h2>
<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
//Debugging message:
echo '<p>' . mysqli_error($dbcon) . '<br><br>Query: ' . $q . '</p>';
} // End of if ($result)
mysqli_close($dbcon); // Close the database connection.

exit();
} else
{ // Report the errors

header("Location:http://localhost/BootstrapEx/RegError.html");
echo '<h2>Error!</h2>
<p class="error">The following error(s) occurred:<br>';
foreach ($errors as $msg) { // Echo each error
echo " - $msg<br>\n";
}
echo '</p><h3>Please try again.</h3><p><br></p>';
}// End of if (empty($errors))
} // End of the main Submit conditional
}
?>

MySQL php 连接文件(用户名、密码等详细信息已更改)

<?php
//This file provides the information for accessing the database and connecting to
//mysql. It also sets the language coding to utf-8.


DEFINE ('DB_USER', '****')
DEFINE ('DB_PASSWORD', '****')
DEFINE ('DB_HOST', 'localhost')
DEFINE ('DB_NAME', '****')

$dbcon = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die ('Could not connect to MySQL:' .mysqli_connect_error());
language encoding
mysqli_set_charset($dbcon, 'utf8');

?>

Javascript 验证

function validateForm() {

// First name validation
var w = document.forms["myForm"]["fname"].value;
var x = document.forms["myForm"]["lname"].value;
var y = document.forms["myForm"]["email"].value;
var z = document.forms["myForm"]["psword1"].value;
var b = document.forms["myForm"]["psword2"].value;
var atpos = y.indexOf("@");
var dotpos = y.lastIndexOf(".");

if (w == null || w == "") {
alert("First name must be filled out");
return false;
}

else if (/[^a-zA-z'-]/.test(w)) {
alert("First Name not completed, please only use letters & spaces with either (') or (-).");
return false;
}

// Last name validation

else if (x == null || x == "") {
alert("Last name must be filled out");
return false;
}

else if (/[^a-zA-z'-]/.test(w)) {
alert("Last name not completed, please only use letters & spaces with either (') or (-).");
return false;
}

// Email validation

else if (y == null || y == "") {
alert("Email address must be completed");
return false;
}


else if (atpos< 1 || dotpos<atpos+2 || dotpos+2>=y.length) {
alert("Not a valid e-mail address");
return false;
}

// Password Validation

else if (z == null || z == "") {
alert("Password must be entered");
return false;
}

else if (z.length < 7 || !/[a-z]/.test(z) || !/[A-Z]/.test(z) || !/[0-9]/.test(z)) {
alert("Password must be a minimum of 8 characters, with at least 1 number, 1 lower case and 1 upper case letter.");
return false;
}

else if (z !== b) {
alert("Passwords do not match.");
return false;
}


}

任何帮助将不胜感激。

非常感谢,

无望的编码员

最佳答案

连接脚本中的语法错误:

$dbcon = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die ('Could not connect to MySQL:' .mysqli_connect_error());
language encoding
^^^^^^^^^^^^^^^^^

这不是有效的 PHP。由于您只是得到一个空白页,因此您可能已经关闭了 display_errors 和 error_reporting 。它们在开发/调试时永远不应该关闭。这几乎和使用 @ 抑制运算符一样糟糕 - 相当于 Handlebars 指塞进耳朵里然后说“lalalalalala 听不到你说话”。

关于php - Bootstrap 模式表单中的数据不会使用 php 传输到 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27114310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com