SQL+AJAX+PHP POST请求
我正在尝试使用PHP删除SQL数据库中的一行。 行由用户键入的产品的名称决定 我创建了一个简单的表单来删除已输入的产品:SQL+AJAX+PHP POST请求,php,html,ajax,mysqli,Php,Html,Ajax,Mysqli,我正在尝试使用PHP删除SQL数据库中的一行。 行由用户键入的产品的名称决定 我创建了一个简单的表单来删除已输入的产品: <article> <section> <fieldset><legend><span>Would you like to add a Product?</span> </legend> <form method="POST" id =
<article>
<section>
<fieldset><legend><span>Would you like to add a Product?</span> </legend>
<form method="POST" id = "myForm" name="myForm" onsubmit="return false;">
<br>
<p>Enter a name of product <input type='text' id='name' name = 'name'/></p>
<br>
<input name="submit" id ="submit" type="button" value="Find Product"/>
</form>
</fieldset>
<div id="fetchProduct">
</div>
</section>
</article>
假设我已经在另一个表单上输入了数据,它进入了数据库,但是当我尝试删除它时,我在尝试删除时得到了未定义的索引:name
以下是im用于删除产品的脚本:
<?php
include("database/connect_database.php");
$name = $_POST['name'];
$query = "DELETE FROM products WHERE product_name = '$name' ";
$result = $database->query($query) OR die ("Failed query $query");
echo $database->error."<p>";
if($result){
echo "Record deleted";
}
else {
echo "Product not found!";
}
?>
我还使用AJAX传递名称:
fetch = function () {
// declare the two variables that will be used
var xhr, target, changeListener;
// find the element that should be updated
target = document.getElementById("fetchProduct");
var name = document.getElementById("name").value;
var variable = "name="+name;
// create a request object
xhr = new XMLHttpRequest();
changeListener = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
target.innerHTML = xhr.responseText;
} else {
target.innerHTML = "<p>Something went wrong.</p>";
}
};
xhr.open("POST", "deleteProductSQL.php", true);
xhr.onreadystatechange = changeListener;
xhr.send(variable);
};
pageLoaded = function () {
var fetchButton = document.getElementById("submit");
if (fetchButton) {
fetchButton.addEventListener("click", fetch);
}
};
window.onload = pageLoaded;
有谁能指出我在这里犯了什么严重错误,因为我无法确定为什么它不允许我删除行,为什么我收到了未定义的错误
感谢您抽出时间将您的PHP代码放入isset
if (isset($_POST['name']){
//code here
}
也
在这一行中,不是一个自动关闭标记,因此
<p>Enter a name of product <input type='text' id='name' name = 'name'></p>
可爱的脆弱。享受你的服务器pwn3d。我试过了,错误信息消失了,但它没有传递任何信息,值是空的?感谢您的帮助事实上,我已经从jQuery学习了AJAX,所以我无法帮助您编写JS代码。否则,您的PHP看起来相当完美。
<p>Enter a name of product <input type='text' id='name' name = 'name'></p>