Jquery Ajax将数据插入数据库
Jquery Ajax Inserting data into a database
我有一个包含三个表单的页面,这些表单上都是独立的变量。该页面允许用户输入详细信息,这些信息将被插入MySQL数据库中进行查看。我有脚本:
编辑:我也知道mySql_被弃用了,但为了这个例子,它运行得很好。
编辑2:我知道你可以注入,但目前这很无关紧要,我认为使用文本区域而不是简单输入是个问题。
编辑3:这只是一个打字错误。
$("#finishButton").click(function(e) { // store final value and execute script to insert into DB. On success, switch to success page
var commentsValid = $('#commentsDetailsForm').valid();
if (commentsValid) {
comments = document.getElementById('commentsInput').value;
e.preventDefault();
$.ajax({
type: "POST",
url: 'insert.php',
data: 'forenameInput=' + forename + '&surnameInput=' + surname + '&emailInput=' + email + '&telephoneInput=' + telephone + '&genderInput=' + gender + '&dobInput=' + dob + '&commentInput=' + comments,
success: function (data) {
if (data == "Error") {
$("#error").show();
$("#processing").hide();
} else {
window.location.href = "success.php";
}
}
});
} else {
}
});
这意味着将所有详细信息存储到数据库中。然而,就目前情况来看,它将除注释(最终变量)之外的所有详细信息存储在数据库中。我完成数据陈述时出错了吗?还有其他根本错误的地方吗?
PHP脚本:
<?php
// Connection Details
$servername = "localhost";
$username = "root";
$password = "user10";
$dbname = "test";
// Create connection
$conn = mysql_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_connect_error());
}
// Select database
mysql_select_db($dbname,$conn);
// Store posted data in variables
$forename = $_POST['forenameInput'];
$surname = $_POST['surnameInput'];
$email= $_POST['emailInput'];
$telephone = $_POST['telephoneInput'];
$dob = $_POST['dobInput'];
$gender = $_POST['genderInput'];
$comments = $_POST['commentsInput'];
//Change date of birth so it's storable in mysql database
$dobAlt = date('Y-m-d',strtotime($dob));
// Insert form information into database
$sqlQuery = "INSERT INTO test (firstName, lastName, email, telephone, gender, dob, comments) VALUES ('$forename','$surname','$email','$telephone','$gender','$dobAlt', '$comments')";
// Check if query worked
if (mysql_query($sqlQuery, $conn)) {
} else {
echo "Error: " . $sql . "<br>" . mysql_error($conn);
}
// Close db
mysql_close();
?>
html:
<form id = "commentsDetailsForm" name = "commentsDetailsForm" method = "post">
<label for "commentsInput" id = "labels"> Comments </label>
<br>
<textarea id = "commentsInput" rows= "2" name = "commentsInput" class = "input-block-level"></textarea>
<br>
<div id = "registrationButtonWrapper">
<button id = "finishButton" class = "insertDetailsFinal" name = "finish"> finish > </button>
</div>
</form>
你也可以看到它在http://chriswaszczuk.me/jobTest/(你必须填写表格才能查看数据库)。
你只有一个打字错误。
JS
'&commentInput=' + comments
-共输入-奇异
PHP
$comments = $_POST['commentsInput'];
-注释输入-复数
问题可能是该值包含url中不允许的字符。
您应该确保所有变量都正确编码:
'&commentInput=' + encodeURIComponent(comments)
这适用于所有变量。
除此之外,您还有一个sql注入问题。您应该切换到PDO或mysqli并使用准备好的语句。
相关文章:
- 使用formData使用ajax将图像插入数据库
- 如何访问日期并将其插入数据库?节点.JS&MongoDB
- 使用Mysqli、PHP和AJAX的聊天系统正在将空白消息插入数据库
- AJAX PHP 连接将行插入数据库
- 使用 pur Html 和 Javascript 将文件插入数据库
- 将表单数据插入数据库后无法重定向到 html 页面
- 将整数变量插入数据库
- 单击按钮可多次动态添加选择框、文本框和日期,并将这些值插入数据库
- 动态选择列表AJAX和插入数据库表通过张贴形式
- 无法使用ajax序列化功能将图像插入数据库
- 使用ajax进行内联编辑(将值插入数据库)
- 将捕获的图像插入数据库问题(Cordova/SQlite/JavaScript)
- 如何在 PHP 中将动态表值插入数据库
- 将数据插入数据库express/mongodb应用程序时出错
- 通过API将视频信息插入数据库
- 我想单击一个
- 并获取 id 以通过 POST 将其插入数据库,可以吗?
- 如何将翻译后的文本插入数据库 php
- PHP-插入数据库而不刷新页面(OOP)
- MongoDB在将文档插入数据库之前为空集合
- 将本地存储值插入数据库