嗨,我正在尝试使用php从ajax向mysql插入数据,这是我的代码,任何人都可以帮忙。
我得到的唯一错误是未插入数据,在我的mysql中未传递值。
我的ajax哪里出了问题?
或当我传递值时在我的PHP中。
index.php
<?php include "bd.php"; ?>
<!DOCTYPE html>
<html>
<head>
<title>Submit Form Using AJAX and jQuery</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script
src="https://code.jquery.com/jquery-2.2.4.js"
integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="
crossorigin="anonymous"></script>
<link href="main.css" rel="stylesheet">
</head>
<body>
<div class="container ">
<form class="form-signin text-center col-md-6" method="post" action="">
<h2 class="form-signin-heading">Please Insert value in</h2>
<div class="form-group">
<label for="name" class="sr-only">Name:</label>
<input type="text" id="name" name="name" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<label for="last_name" class="sr-only">Last name</label>
<input type="text" id="last_name" name="last_name" class="form-control" placeholder="Last Name" >
</div>
<div class="form-group">
<label for="email" class="sr-only">email:</label>
<input type="text" id="email" name="email" class="form-control" placeholder="Email" >
</div>
<div class="form-group">
<label for="phone" class="sr-only">Phone:</label>
<input type="text" id="phone" name="phone" class="form-control" placeholder="Phone" >
</div>
<div class="form-group">
<label for="bridge">Select list:</label>
<select class="form-control" id="bridge" name="bridge">
<option>None</option>
<option>eAgent</option>
<option>iArts</option>
<option>Orbit</option>
<option>G&G</option>
<option>EstateWeb</option>
<option>Globalc</option>
</select>
</div>
<div class="form-group">
<label for="comments">Comment:</label>
<textarea class="form-control" rows="5" id="comments" name="comments"></textarea>
</div>
<button type="button" id="submit" class="btn btn-lg btn-primary btn-block" >Register</button>
</form>
<div class="col-md-6">
<h2>Here is tha form with ajax.</h2>
</div>
</div> <!-- /container -->
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
var name = $("#name").serialize();
var last_name = $("#last_name").serialize();
var email = $("#email").serialize();
var phone = $("#phone").serialize();
var bridge = $("#bridge").serialize();
var comments = $("#comments").serialize();
$.ajax({
type : "POST",
url : "ajax.php",
data : {'name': name,'email': email ,'last_name':last_name,'phone':phone,'bridge': bridge, 'comments': comments},
success : function(result) {
alert(result);
}
});
});
});
</script>
</body>
</html>
现在我的php文件已完成连接。
<?php
/**
* Created by PhpStorm.
* User: erevos13
* Date: 26/6/2017
* Time: 11:13 μμ
*/
//is the databases connection
include "bd.php";
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['name'])) {
$name = $_POST['name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$bridge = $_POST['bridge'];
$comments = $_POST['comments'];
$sql = "INSERT INTO info (`id` , `name`, `last_name`, `email `, `phone`, `bridge` , `comments` ) VALUES ( '' , '" . $name . "', '" . $last_name . "','" . $email . "', '" . $phone . "' , '" . $bridge . "', '" . $comments . "')";
$query = mysqli_query($connection, $sql);
if ($query) {
echo "data insert successfully";
} else {
echo "data is not insert";
}
}
我得到的唯一错误是“数据未插入”。
最佳答案
您应该使用console.log()
函数检查javascript代码中的数据。
并在here上阅读有关serialize
函数的更多信息。
您的JavaScript代码应类似于:
$.ajax({
type : "POST",
url : "ajax.php",
data : $("form").serialize();
关于php - 从ajax和php插入mysql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44773906/