我无法解决为什么在本地工作的本地主机服务器失败。它连接到数据库,检索并显示数据,但无法检索数据并包含表单。希望我已经包含了足够的代码。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