这里我正试图将两个variables
发送到我的php
文件:
我的PHP中不断出现错误。。两个variables
的mysqli_query() expects parameter 1 to be mysqli and undefined indexes
这就像它们没有被正确发送或接收一样。
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) {
}
}
httpSend.send(params);
PHP文件:将variables
添加到数据库
<?php
include("mysqlconnect.php");
$name = $_POST['Name'];
$address = $_POST['Address'];
mysqli_query($connection,"INSERT INTO offerSelected (Id, Url) VALUES ('".$name."','".$address."')");
?>
mysqlconnect.php等于:
<?php
$connection = mysql_connect("localhost", "user", "pass");
if(!$connection){
die('Could not connect to server: ' . mysql_error());
}
mysql_select_db("table", $connection);
?>
更新版本
<?php
$connection = mysqli_connect("localhost", "user", "pass", "table");
$stmt = $connection->prepare('INSERT INTO offerSelected (Id, Url) VALUES (?, ?)');
$stmt->bind_param('ss', $name, $address);
$stmt->execute();
?>
JavaScript
var xmlhttp1 = new XMLHttpRequest();
var name = "John";
var address = "UK";
var params = 'Name=' + name + '&Address=' + address;
var php_url = "http://server/~name/folder/insertOffer.php";
xmlhttp1.open('POST', php_url, true);
xmlhttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp1.onreadystatechange = function() {
if (xmlhttp1.readyState == 4 && xmlhttp1.status == 200) {
var response1 = xmlhttp1.responseText;
response1 = JSON.parse(response1);
alert(response1);
console.log(response1);
alert('Check the browser console');
}
}
xmlhttp1.send(params);
我的响应警报根本没有激活。
您使用mysql_connect,这对mysqli来说是错误的;)看看这里:mysqli
mysql_connect
返回MySQL链接标识符,但不返回主php文件中mysqli_query
所需的Mysqli对象。您应该同时使用Mysqli这两个文件。
您必须使用mysqli_connect("myhost"、"myuser"、"my passw"、"mybd")来代替mysql_connect和mysqli_select_db(connection,dbname);而不是mysql_select_db();