目前空白行正在输入我的mySQL数据库。未定义的索引是我在PHP中为Name和Address获取的。我正在使用javaScript代码作为函数的一部分。用户将激活此功能,此添加到数据库将在幕后无缝发生。
如何获取输入mySQL的值。
PHP代码,
<?php
$connection = mysqli_connect("localhost", "user", "pass", "db");
$name = $_POST['Name'];
$address = $_POST['Address'];
mysqli_query($connection,"INSERT INTO offerSelected (Id, Url) VALUES ('".$name."','".$address."')");
?>
的JavaScript
var name = "John";
var address = "UK";
var sendInfo = {
Name: name,
Address: address
};
var params = JSON.stringify(sendInfo);
alert(params);
var httpSend = new XMLHttpRequest();
var php = "http://server/~name/folder/insertOffer.php";
httpSend.open("POST", php, true);
httpSend.onreadystatechange = function()
{
if(httpSend.readyState == 4 && httpSend.status == 200) {
alert("sent");
}
}
httpSend.send(params);
已更新
var params = 'Name=' + name + '&Address=' + address;
alert(params);
var httpSend = new XMLHttpRequest();
var php = "http://server/~name/folder/insertOffer.php";
httpSend.open("POST", php, true);
httpSend.onreadystatechange = function()
{
if(httpSend.readyState == 4 && httpSend.status == 200) {
alert("h");
}
}
httpSend.send(params);
答案 0 :(得分:1)
除了评论中提到的SQL注入问题之外,代码的问题是JSON.stringify
。 POST参数使用键值格式,就像GET请求一样,这意味着你的参数应该如下所示:
Name=name&Address=address
而不是JSON对象。您还必须在httpSend.open
之后添加此行代码,以告知服务器您正在发送密钥/值:
httpSend.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");