【发布时间】:2016-11-28 01:32:09
【问题描述】:
这个主题有很多问题,但我是初学者,无法一起回答。
我正在尝试通过 ajax 使用表单更新 MySQL 数据库 - 然后将更新后的数据库作为数组取回以在主页中使用(不刷新页面)。
我设法完成了第一部分,但没有完成第二部分 - 取回数组。
我的第一部分代码:
HTML:
$("#form").submit(function(){
event.preventDefault();
var form = new FormData();
form.append("id", $('#id').val());
form.append("src", $('#src').val());
form.append("title_en", $('#title_en').val());
form.append("title_he", $('#title_he').val());
form.append("exhibition_en", $('#exhibition_en').val());
form.append("exhibition_he", $('#exhibition_he').val());
form.append("subjects_en", $('#subjects_en').val());
form.append("subjects_he", $('#subjects_he').val());
form.append("keywords_en", $('#keywords_en').val());
form.append("keywords_he", $('#keywords_he').val());
form.append("height", $('#height').val());
form.append("width", $('#width').val());
form.append("sold", $('#sold').val());
var settings = {
"async": true,
"crossDomain": true,
"url": "update.php",
"method": "POST",
"dataType": 'json',
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).success(function(data) {
});
});
PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "chana_goldberg";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = $_POST['id'];
$src = $_POST['src'];
$title_en = $_POST['title_en'];
$title_he = $_POST['title_he'];
$exhibition_en = $_POST['exhibition_en'];
$exhibition_he = $_POST['exhibition_he'];
$subjects_en = $_POST['subjects_en'];
$subjects_he = $_POST['subjects_he'];
$keywords_en = $_POST['keywords_en'];
$keywords_he = $_POST['keywords_he'];
$height = $_POST['height'];
$width = $_POST['width'];
$sold = $_POST['sold'];
$sql = "UPDATE paintings_catalog SET title_en='$title_en', title_he='$title_he', exhibition_en='$exhibition_en', exhibition_he='$exhibition_he', subjects_en='$subjects_en', subjects_he='$subjects_he', keywords_en='$keywords_en', keywords_he='$keywords_he', height='$height', width='$width', sold='$sold' WHERE id='$id'";
$conn->query($sql);?>
【问题讨论】:
-
你可能需要php支持的json(JavaScript Object Notation)。请参阅:php.net/manual/en/ref.json.php 和 w3schools.com/js/js_json.asp
-
您的代码易受 SQL 注入攻击。请使用准备好的语句,而不是在您通过请求收到的 SQL 中嵌入字符串。
标签: php jquery mysql arrays ajax