我正在尝试ajax方法,以使用php将数据插入数据库。但是只有警报消息有效,并且数据没有插入数据库
我的表单:
function register() {
var team = document.getElementById("team").value;
var m1 = document.getElementById("m1").value;
var m2 = document.getElementById("m2").value;
var m3 = document.getElementById("m3").value;
var m4 = document.getElementById("m4").value;
var email = document.getElementById("email").value;
var number = document.getElementById("number").value;
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'team=' + team + '&m1=' + m1 + '&m2=' + m2 + '&m3' + m3 + '&m4' + m4 + '&email' + email + '&number' + number;
if (team == '' || m1 == '' || m2 == '' || m3 == '' || m4 == '' || email == '' || number == '') {
alert("Please Fill All Fields");
} else {
// AJAX code to submit form.
$.ajax({
type: "POST",
url: "workreg.php",
data: dataString,
cache: false,
success: function(html) {
alert(html);
}
});
}
return false;
调用功能:
<?php
// Fetching Values From URL
$team=$_POST['team'];
$m1=$_POST['m1'];
$m2=$_POST['m2'];
$m3=$_POST['m3'];
$m4=$_POST['m4'];
$email=$_POST['m4'];
$number=$_POST['m4'];
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server..
$db = mysql_select_db("event", $connection); // Selecting Database
if (isset($_POST['team'])) {
$query = mysql_query("insert into workshop values ('$team', '$m1', '$m2','$m3','$m4','$email','$number')"); //Insert Query
echo "Form Submitted succesfully";
}
mysql_close($connection); // Connection Closed
}
这是php:
{{1}}
?>
每当我单击注册时,它只是显示警报消息,但我检查了数据库,未插入任何值
答案 0 :(得分:0)
您的帖子请求中缺少Content-Type。我只使用PostgreSQL,但是Content-Type是使发帖请求正常工作所必需的,也许您应该检查一下。 还要检查是否设置为在连接时自动提交到数据库,否则数据不会添加到数据库。
答案 1 :(得分:0)
$('#register').submit(event => {
event.preventDefault();
let team = $('#team').val();
let m1 = $('#m1').val();
let m2 = $('#m2').val();
let m3 = $('#m3').val();
let m4 = $('#m4').val();
let email = $('#email').val();
let number = $('#number').val();
$.ajax({
url: "workreg.php",
method: "POST",
data: { team, m1, m2, m3, m4, email, number }
.done(data => {
console.log(data)
})
.fail(err => console.error(err))
})
})
答案 2 :(得分:0)
实际上,您只是在检查发布变量test
是否存在。您不检查数据库查询是否成功执行。在$query
if(!$query){
echo "Form Submitted succesfully"
} else {
die('Invalid query: ' . mysql_error()); // show the error
}
mysql
是不推荐使用的功能,所以我正在使用mysqli
,这对您来说也更好。永远不要相信用户的输入,所以我也在使用prepare语句。 您应该始终在寻找更新的视频和文章。
$connection = mysqli_connect("localhost", "root", "","event"); // Establishing Connection with Server..
if (isset($_POST['team'])) {
$query = "insert into workshop values (?, ?, ?,?,?,?,?)"; //Never trust user's input so use prepare
$stmt = mysqli_prepare($connection ,$query) ;
mysqli_stmt_bind_param($stmt,'ssssssi',$team,$m1,$m2,$m3,$m4,$email,$number) ;
mysqli_stmt_execute($stmt);
if( mysqli_stmt_affected_rows($stmt) === 1 ) { //it will be int 1 if one row is inserted
echo "Form Submitted succesfully" ;
}else {
echo mysqli_error($connection)
}
}
mysqli_close($connection); // Connection Closed
一些未来的来源
How can I prevent SQL injection in PHP?
https://phpdelusions.net/pdo(关于PDO,但您会有所想法的。)
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php