PHP 包括不工作在主机服务器上,但在本地 (XAMPP) 服务器上工作


php include not working on host server but works on local (xampp) server

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

首先检索并显示数据:

/*-------------------  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,该 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();
}

如果有人有任何想法为什么这不起作用,我欢迎您的见解!

只需更改句子:

来自: '../include/dbconnect.php';

至:_SERVER美元["DOCUMENT_ROOT"]。/include/dbconnect.php';

在服务器中,路径不能写为"../' 因为有一个完全不同的服务器路径配置。