我在測試 php 網(wǎng)站上的頁面上調(diào)用一個函數(shù)來顯示一個 html 表單來記錄要通過 ajax 提交到我的 SQL 數(shù)據(jù)庫的數(shù)據(jù),但由于某種原因沒有插入任何數(shù)據(jù)。我在包含的 header.php 中有所有必需的引導(dǎo)程序、js、ajax 鏈接,并且我有其他表單提交到測試站點上同一數(shù)據(jù)庫中的表,這些表單都沒有問題,因此數(shù)據(jù)庫連接似乎很好,這是為什么我如此努力地弄清楚為什么沒有插入數(shù)據(jù)。這是 MAMP 上的本地主機測試服務(wù)器,使用 MySQL 5、PHP 5。我的文件包含在調(diào)用時顯示 html 條目表單的代碼在 functions.php 中:<?php session_start(); $link = mysqli_connect("localhost", "user", "pass", "tables"); if (mysqli_connect_errno()) { print_r(mysqli_connect_error()); exit();}// other code ...function displayTextInput() { global $link; echo ' <div class="form"> <div class="form-group"> <textarea class="form-control" id="textDesc" placeholder="Description"></textarea> <textarea class="form-control" id="textType" placeholder="Type"></textarea> </div> <button id="postTextButton" class="btn btn-primary">Post Text</button> </div>';}?>我的 ajax 調(diào)用位于 footer.php 內(nèi)的腳本中:<script>$("#postTextButton").click(function() { $.ajax({ type: "POST", url: "actions.php?action=postText", data: {textDesc: $("#textDesc").val(), textType: $("#textType").val()}, success: function(result) { if (result == "1") { alert("ajax successful"); } else if (result != "") { alert("ajax unsuccessful"); } } }) })</script>最后我的 SQL 在一個 actions.php 中:if ($_GET['action'] == 'postText') { if (!$_POST['TextDesc']) { echo "Your text description is empty!"; } else { $textDesc = mysqli_real_escape_string($link, $_POST['textDesc']); $textType = mysqli_real_escape_string($link, $_POST['textType']); $userIDForTable = mysqli_real_escape_string($link, $_SESSION['id']);我可以在 actions.php else { } 部分中回顯語句,因此代碼肯定連接到那里,但我真的似乎無法弄清楚原因。我什至將上面的代碼鏈接到同一個數(shù)據(jù)庫中的不同 php 表并讓它工作,所以我無法理解為什么這不起作用。我也可以看到控制臺中沒有錯誤。任何幫助將不勝感激。
我的 php 沒有通過 ajax 請求將值插入到我的 sql 數(shù)據(jù)庫中
寶慕林4294392
2021-06-15 21:17:15