[英]how to use ajax in wordpress to show database columns using php and Json
[英]How to use AJAX / JSON to add details to mySQL database using PHP?
在這里,我嘗試將兩個variables
發送到我的php
文件:
我在PHP中不斷收到錯誤mysqli_query() expects parameter 1 to be mysqli and undefined indexes
兩個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
mysqlconnect.php中的mysql_connect
返回一個MySQL鏈接標識符,但不返回主php文件中mysqli_query
所需的Mysqli對象。 您應該同時使用Mysqli這兩個文件。
您必須使用mysqli_connect(“ myhost”,“ myuser”,“ mypassw”,“ mybd”)代替mysql_connect和mysqli_select_db(connection,dbname); 代替mysql_select_db();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.