我不知道ajax是否可以这种方式工作,但是数据没有添加到我的mysql数据库中。我在Chrome浏览器中检查了网络标签,发现数据已转发。 我曾经尝试过这种方式
script.js:
$(".btn_ranking").click(function (e) {
e.preventDefault();
var name = localStorage.getItem('name');
var time = localStorage.getItem('timer_end');
$.ajax({
url: "php/file.php",
method: "POST",
data: { "name": name, "time": time }
})
});
file.php:
<?php
require_once "connect.php";
$polaczenie = @new mysqli($host, $db_user, $db_password, $db_name);
$name = $_POST['name'];
$time = $_POST['time'];
if ($polaczenie->connect_errno != 0) {
echo "Error: " . $polaczenie->connect_errno;
} else {
if ($rezultat = @$polaczenie->query("INSERT INTO ranking (id, name, time) VALUES (NULL, $name, $time)")) {
echo "ok";
}
}
?>
答案 0 :(得分:0)
首先,最好使用 PrepareStatement 传递参数,而不是直接将其写入SQL。
对于您当前的代码,请尝试更改为以下代码
StringBuilder sbOrg = new StringBuilder();
StringBuilder sbDub = new StringBuilder();
foreach (var item in org) {
if (sbOrg.Length > 0)
sbOrg.Append(",");
sbOrg.Append(item);
}
foreach (var item in doubled) {
if (sbDub.Length > 0)
sbDub.Append(",");
sbDub.Append(item);
}
orgResult = sbOrg.ToString();
doubledResult = sbDub.ToString();
答案 1 :(得分:0)
您应该尝试如下操作
$(".btn_ranking").click(function (e) {
e.preventDefault();
var name = localStorage.getItem('name');
var time = localStorage.getItem('timer_end');
$.ajax({
url: "php/file.php",
method: "POST",
data: { name: name, time: time } //Remove double quote
})
});