1

I cannot resolve why what works locally fails at the host server. It connects to the database, retrieves and displays data, but it fails to retrieve the data and include the form. Hopefully, I have included enough code.

First the data is retrieved and displayed:

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

Then, in the row the user wishes to edit, he clicks the edit button. Here's that 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>

Clicking the edit button triggers this php, which fails (not locally). It does not include the file (the path is correct; in the same folder).

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

If anyone has any ideas why this does not work, I welcome your insight!

6
  • Do yourself a favor, simply place a print_r($_POST) or var_dump($_POST) in the PHP page which receives the form submission. Fill out your form, submit and look closely at the data printed to the screen. Familiarize yourself with how form data is posted to scripts, including what gets passed and what doesn't. I have an inkling that you're not entering into the if statement. Commented Feb 25, 2016 at 19:57
  • change to require instead of include, and see if the script aborts at that point. you're just assuming it's loaded properly. Commented Feb 25, 2016 at 20:09
  • @JayBlanchard Thanks for your reply. I fear my description of my problem might be ambiguous. The only data the form posts is the ID of the item and the value of the action ("edit_accessories"). The php script only needs the item ID to retrieve the other fields and display them on the included form (which will not display at the host server, but works fine locally). Commented Feb 25, 2016 at 20:12
  • @MarcB. Thanks for your reply. It's a good idea, but using require instead of include does not make it work or generate an error message. Commented Feb 25, 2016 at 20:14
  • 1
    ah, but since the script didn't bail with a "failed to include" error, then the file is present, which means it WAS loaded. so now you have to look at what's in the file that'd be causing it to output nothing. Commented Feb 25, 2016 at 20:15

1 Answer 1

2

Just change the sentence:

FROM: '../includes/dbconnect.php';

TO: $_SERVER['DOCUMENT_ROOT'].'/includes/dbconnect.php';

In the server the path can't be write as '../' because there is a whole different server path configuration.

Sign up to request clarification or add additional context in comments.

2 Comments

I am delighted that you took the time to answer this "old" post. Your answer resolved the problem! Thank you very much!
You are welcome! Actually I got into this question discussion by accident... I don't work as a PHP programmer... Don't know how the people didn't answered straight away a so simple question :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.