Update not updating - no error

Hi again

I am using the book:

Yank, Kevin (2009). Build Your Own Database Driven Web Site Using PHP & MySQL 4th Edition (Kindle Locations 3745-3746). SitePoint Pty Ltd. Kindle Edition.

to learn PHP.

I have adapted the Content Management System (Chapter 7) for a Web site I am working on. It gives the user the ability to add, edit, or delete the information in the database.

I can add or delete. When I press the Edit button, the form fills in the correct information in the correct fields (except boat_description which is a textarea and has no value).

However, when I change something and press Update Boat - nothing updates and I get no error messages.

Could somebody please help?

Here is the start page w/ php:


<?php
print_r($_POST);
ob_start();
if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
        array_map('stripslashes_deep', $value) :
        stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}

if (isset($_GET['add'])) {
    $pagetitle = 'New Boat';
    $action = 'addform';
    $type = '';
    $make = '';
    $BoatID = '';
    $year = '';
    $price = '';
    $city = '';
    $state = '';
    $details = '';
    $item = '';
    $boat_description = '';
    $picture = '';
    $button = 'Add boat';
    include 'form.html.php';
    exit();
}
if (isset($_GET['addform'])) {
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

    if (isset($_POST['type'])) {
        $target = "../Images/";
        $target = $target . basename($_FILES['picture']['name']);
        $type = mysqli_real_escape_string($link, $_POST['type']);
        $BoatID = mysqli_real_escape_string($link, $_POST['BoatID']);
        $make = mysqli_real_escape_string($link, $_POST['make']);
        $year = mysqli_real_escape_string($link, $_POST['year']);
        $price = mysqli_real_escape_string($link, $_POST['price']);
        $city = mysqli_real_escape_string($link, $_POST['city']);
        $state = mysqli_real_escape_string($link, $_POST['state']);
        $details = mysqli_real_escape_string($link, $_POST['details']);
        $item = mysqli_real_escape_string($link, $_POST['item']);
        $boat_description = mysqli_real_escape_string($link, $_POST['boat_description']);
        $picture = mysqli_real_escape_string($link, ($_FILES['picture']['name']));
        $pictarget = '<img src="Images/' . $picture . '"/>';
        switch ($type) {
            case 'Sailboat':
                $Cid = 1;
                break;
            case 'Powerboat':
                $Cid = 2;
                break;
            case 'Other':
                $Cid = 3;
                break;
            default: $Cid = 3;
        }
        $sql = "INSERT INTO boats SET  CategoryID='$Cid',

BoatType='$type', Make='$make',
    BoatYear='$year', Price='$price', City='$city',

State='$state', Details='$details', Item='$item',

BoatDescription='$boat_description',
    Picture='$pictarget'";
        if (move_uploaded_file($_FILES['picture']['tmp_name'], $target)) {
            // Tells you if its all ok
            echo "The file " . basename($_FILES['uploadedfile']['name']) . " has been uploaded, and your information has been added to the directory";
        }else {
            // Gives and error if its not
            echo "Sorry, there was a problem uploading your file.";
        }
        if (!mysqli_query($link, $sql)) {
            $error = 'Error adding submitted boat: ' . mysqli_error($link);
            include 'error.html.php';
            exit();
        }

        header('Location: .');
        exit();
    }
}
    if (isset($_POST['action']) and $_POST['action'] == 'Edit')
    {
       include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
        
        $BoatID = mysqli_real_escape_string($link, $_POST['BoatID']);
        $sql = "SELECT BoatID, Make, BoatType, BoatYear, Price, City, State, Details, Item, BoatDescription, Picture FROM boats WHERE BoatID='$BoatID'";
        $result = mysqli_query($link, $sql);
        if (!$result) {
            $error = 'Error fetching boat details.';
            include 'error.html.php';
            exit();
        }
        $row = mysqli_fetch_array($result);
    $BoatID = $row['BoatID'];
        $pagetitle = 'Edit Boat';
        $action = 'editform';
    $type = $row['BoatType'];
        $make = $row['Make'];
        $year = $row['BoatYear'];
        $price = $row['Price'];
        $city = $row['City'];
        $state = $row['State'];
        $details = $row['Details'];
        $item = $row['Item'];
    $boat_description = $row['BoatDescription'];
        $picture = $row['Picture'];
        $button = 'Update boat';

        include 'form.html.php';
        exit();
    } 
    if (isset($_GET['editform']))
{
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

    $BoatID = mysqli_real_escape_string($link, $_POST['BoatID']);
        $target = '../Images/';
        $target = $target . basename($_FILES['picture']['name']);
        $type = mysqli_real_escape_string($link, $_POST['type']);
        
        $make = mysqli_real_escape_string($link, $_POST['make']);
        $year = mysqli_real_escape_string($link, $_POST['year']);
        $price = mysqli_real_escape_string($link, $_POST['price']);
        $city = mysqli_real_escape_string($link, $_POST['city']);
        $state = mysqli_real_escape_string($link, $_POST['state']);
        $details = mysqli_real_escape_string($link, $_POST['details']);
        $item = mysqli_real_escape_string($link, $_POST['item']);
        $boat_description = mysqli_real_escape_string($link, $_POST['boat_description']);
        $picture = mysqli_real_escape_string($link, ($_FILES['picture']['name']));
        $pictarget = '<img src="Images/' . $picture . '"/>';
        switch ($type) {
            case 'Sailboat':
                $Cid = 1;
                break;
            case 'Powerboat':
                $Cid = 2;
                break;
            case 'Other':
                $Cid = 3;
                break;
            default: $Cid = 3;
        }
        $sql = "UPDATE boats SET 
        CategoryID='$Cid',
        BoatType='$type', 
        Make='$make',
        BoatYear='$year', 
        Price='$price', 
        City='$city',
        State='$state', 
        Details='$details', 
        Item='$item',
        BoatDescription='$boat_description',
        Picture='$pictarget'
    WHERE   BoatID='$BoatID'";
        if (!mysqli_query($link, $sql)) {
            $error = 'Error updating submitted boat.';
            include 'error.html.php';
            exit();
        }

        header('Location: .');
        exit();
   
}
    if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
        $BoatID = mysqli_real_escape_string($link, $_POST['BoatID']);
        $sql = "DELETE FROM boats WHERE BoatID='$BoatID'";
        if (!mysqli_query($link, $sql)) {
            $error = 'Error deleting boat: ' . mysqli_error($link);
            include 'error.html.php';
            exit();
        }

        header('Location: .');
        exit();
    }
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
    $result = mysqli_query($link, 'SELECT BoatID, Make, BoatType, BoatYear, Price, Item, Details, Picture FROM boats');
    if (!$result) {
        $error = 'Error fetching boats: ' . mysqli_error($link);
        include 'error.html.php';
        exit();
    }while ($row = mysqli_fetch_array($result)) {
        $boats[] = array('BoatID' => $row['BoatID'], 'text' => $row['BoatType'] . " - " . $row['Make'] . " - " . $row['BoatYear'] . " - " . $row['Price'] . " - " . $row['Details']);
    }

    include 'boats.html.php';
ob_end_flush();
    ?>

Here is the form.html.php


<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "HTTP://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="HTTP://www.w3.org/1999/HTML" xml:lang="en" lang="en">
<head>
<title><?php htmlout($pagetitle); ?></title> 
<meta HTTP-equiv="content-type"
                content="text/html; charset=utf-8"/>

</head>
<body>
<form action="?<?php htmlout($action); ?>"  enctype="multipart/form-data" method="post" >
  <table cellpadding="3" bgcolor="#CCD6EB" >
    <tr>
      <th colspan="4" align="center"></th>
    </tr>
    <tr>
      <td><label>Boat Type</label></td>
      <td><select name="type" id="type">
          <option>Sailboat</option>
          <option>Powerboat</option>
          <option>Other</option>
        </select></td>
      <td><label>Make</label></td>
      <td><input type="text" name="make" id="make" value="<?php htmlout($make); ?>" size="60" /></td>
    </tr>
    <tr>
      <td><label>Boat Year</label></td>
      <td><input type="text" name="year" id="year" value="<?php htmlout($year); ?>" size="5" /></td>
      <td><label>Price</label></td>
      <td><input type="text" name="price" id="price" value="<?php htmlout($price); ?>" size="10" /></td>
    </tr>
    <tr>
      <td><label>City</label></td>
      <td><input type="text" name="city" id="city" value="<?php htmlout($city); ?>" size="40" /></td>
      <td><label>State</label></td>
      <td><input type="text" name="state" id="state" value="<?php htmlout($state); ?>" size="2" /></td>
    </tr>
    <tr>
      <td><label>Details</label></td>
      <td><input type="text" name="details" id="details" value="<?php htmlout($details); ?>" size="30" /></td>
      <td><label>Item</label></td>
      <td><input type="text" name="item" id="item" value="<?php htmlout($item); ?>" size="11" /></td>
    </tr>
    <tr>
      <td><label>Boat Description</label></td>
      <td><textarea name="boat_description" id="boat_description" <?php echo $boat_description; ?>" cols="30" rows="6"></textarea></td>
      
      <td><label for="picture">Image</label></td>
      <td><input type="file" name="picture" id="picture" value="<?php htmlout($picture); ?>" /></td>
    </tr>
    <tr>
      <td>&nbsp;&nbsp;</td>
      <td align="right"><input type="submit" value="<?php htmlout($button); ?>"/>
      <td align="left"><input name="reset" type="reset" value="Reset" /></td>
      <td>&nbsp;&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>

And here is the set-up for the list of boats: boats.html.php:

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?> 

 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>List of Boats</title>
        <meta http-equiv="content-type"
                content="text/html; charset=utf-8"/>
                <style type="text/css">
a { color:#0000CC;
font-family:Arial, Helvetica, sans-serif;
font-size:1.2em}
p {
color:#000033;
font-family:Arial, Helvetica, sans-serif;
font-size:1.5em;}
body {
background-color:#CCCCF5;}
h1 {
font-family:Arial, Helvetica, sans-serif;
color:#000066;
text-align:center;}
p.center {text-align:center;}
</style>
    </head>
    <body>



        <p><a href="?add">Add new boat</a></p>
        <p><a href="../UBoDMgmtSys.php">Return to UBoD Management System</a></p>
        <p class="center">Here are all the boats in the database:


Please help!

The last set did not show all the way,

Here it is:

<body>



        <p><a href="?add">Add new boat</a></p>
        <p><a href="../UBoDMgmtSys.php">Return to UBoD Management System</a></p>
		<p class="center">Here are all the boats in the database:</p>
        <?php foreach ($boats as $boat): ?><form action="" method="post">
				
					<p>
						<?php echo htmlspecialchars($boat['text'], ENT_QUOTES,
								'UTF-8'); ?>
						<input type="hidden"  name="BoatID" value="<?php
								echo $boat['BoatID']; ?>"/>
                                                
                                                
						<input type="submit" name="action" value="Edit"/>
							<input type="submit" name="action" value="Delete"/>
					</p>
				
</form> <?php endforeach; ?>
</body>

There is a small error with the textarea which is why you’re not seeing the stored data; it should look like this:


<td><label>Boat Description</label></td>
<td><textarea name="boat_description" id="boat_description" cols="30" rows="6"><?php echo $boat_description; ?></textarea></td>

centrex,

Thank you, that corrected that. That is very good to know!

Now I only need to figure out why my info isn’t updating.

It looks like the boat’s ID is not being passed to the form; that’s why you’re not seeing anything get updated.

Add a hidden id field and it should work as expected:


<input type="hidden" name="BoatID" value="<?php htmlout($BoatID); ?>" />

centrex,

Thank you so much! I have been staring at this for days. I missed that. Now it works.