我無法解決爲什麼在本地工作的本地主機服務器失敗。它連接到數據庫,檢索並顯示數據,但無法檢索數據幷包含表單。希望我已經包含了足夠的代碼。php包括不在主機服務器上工作,但在本地(xampp)服務器上工作
首先檢索數據並顯示:
/*------------------- DISPLAY ACCESSORIES ------------------*/
if(isset($_GET['table']) && $_GET['table'] === "accessories")
{
$table = 'accessories';
include '../includes/dbconnect.php';
try {
$result = $db->query("SELECT * FROM $table");
while($row = $result->fetch(PDO::FETCH_ASSOC)){
$accessories[] = array(
'id' => $row['id'],
'buy_link' => $row['buy_link'],
'img' => $row['img'],
'item_number' => $row['item_number'],
'name' => $row['name'],
'description' => $row['description'],
'laser_series' => $row['laser_series'],
'laser_model' => $row['laser_model'],
'quantity' => $row['quantity'],
'price' => $row['price'],
);
}
}
catch (PDOException $e)
{
$error = 'Error fetching data.' . $e->getMessage();
include 'error.html.php';
exit();
}
try {
$sql2 = 'DESCRIBE accessories';
$s2= $db->prepare($sql2);
$s2->execute();
$table_fields = $s2->fetchAll(PDO::FETCH_COLUMN);
}
catch (PDOException $e)
{
$error = 'Error fetching data from database.';
include 'error.html.php';
exit();
}
// Close database connection
$db = null;
// Display data on included page
include 'display-accessories.html.php';
exit();
}
然後,用戶希望編輯的行中,他點擊編輯按鈕。這是HTML:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<input type="hidden" name="id" value="<?php htmlout($accessory['id']); ?>">
<button class="btn btn-default btn-sm" type="submit" name="action" value="edit_accessories">Edit</button>
</form>
點擊編輯按鈕觸發這個PHP,它失敗(不是本地)。它不包括文件(路徑是正確的;在同一個文件夾中)。
/*------------------- EDIT ACCESSORIES ------------------*/
if(isset($_POST['action']) && $_POST['action'] === "edit_accessories")
{
// Assign name of table being queried to variable
$table = 'accessories';
// Sanitize posted data
$id = sanitize($_POST['id']);
// Connect to database
include '../includes/dbconnect.php';
try {
$sql = "SELECT * FROM $table WHERE id = :id";
$s = $db->prepare($sql);
$s->bindValue(':id', $id);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error fetching data.' . $e->getMessage();
include 'error.html.php';
exit();
}
// Store single row result in $item associative array
$item = $s->fetch(PDO::FETCH_ASSOC);
// Close database connection
$db = null;
// Display row content in form
include 'edit-accessories-form.html.php';
exit();
}
如果有人有任何想法,爲什麼這不起作用,我歡迎您的洞察!
爲自己做個忙,只需在接收表單提交的PHP頁面中放置一個'print_r($ _ POST)'或'var_dump($ _ POST)'。填寫你的表格,提交併仔細查看打印到屏幕上的數據。熟悉表單數據如何發佈到腳本,包括什麼被傳遞,哪些沒有傳遞。我有一個暗示,你沒有進入'if'語句。 –
更改爲'require'而不是'include',並查看腳本是否在此時中止。你只是假設它正確加載。 –
@JayBlanchard感謝您的回覆。我擔心我對我的問題的描述可能不明確。表單帖子的唯一數據是項目的ID和操作的值(「edit_accessories」)。 PHP腳本只需要項目ID來檢索其他字段並將其顯示在包含的表單上(不會在主機服務器上顯示,但在本地工作正常)。 – JimB814