簡體   English   中英

SQL + AJAX + PHP POST請求

[英]SQL +AJAX + PHP POST REQUEST

我試圖使用PHP刪除SQL數據庫中的行。 該行由用戶鍵入的產品名稱決定。

我創建了一個簡單的表單來刪除已經輸入的產品:

<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> &nbsp;

        <p>Enter a name of product <input type='text' id='name' name =     'name'/></p>

        <br> &nbsp;

    <input name="submit" id ="submit" type="button" value="Find Product"/>

        </form>
        </fieldset>

        <div id="fetchProduct">
        </div>

    </section>
</article>

說我已經在另一種表單上輸入了數據,它進入了數據庫,但是當我嘗試刪除它時,我得到了未定義索引:嘗試刪除時名稱錯誤。

這是即時消息用於刪除產品的腳本:

<?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>

在此行中, <p>不是自動關閉標簽,因此

<p>Enter a name of product <input type='text' id='name' name = 'name'></p>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM