Update Problem

Ask about a PHP problem here.
Post Reply
toqi786
Posts: 9
Joined: Mon May 16, 2011 12:43 pm

Update Problem

Post by toqi786 »

Hey

I have a slight problem!

I have a form which is connected to a mysql database!

With this form, I wish to edit the content of the mysql database!

Each element in this form is pre-filled! [so this shows the connect file is working]

This is the code for my Edit page
<html>
<?php
include('connect.php');
$id = $_GET['id'];
$query = mysql_query("SELECT * FROM stocklist WHERE ID = '$id'");
$data = mysql_fetch_assoc($query);
?>
<head><title>Manage a Previous Vehicle - Admin Area - High Spec Prestige</title></head>
<body>
<form action="doEdit.php" method="post">
        <table>
        <tr>
                <td>
                <label for="titleL">Title</label>
            </td>
            <td>
            <input type="text" name="title" value="<?php echo $data['Title']; ?>"/>
            </td>
        </tr>
        <tr>
                <td>
                <label for="RegistrationL">Vehicle Registration</label>
            </td>
            <td>
            <input type="text" name="Registration" value="<?php echo $data['Registration']; ?>"/>
            </td>
        </tr>
        <tr>
                <td>
                <label for="YearL">Year/Reg</label>
            </td>
            <td>
            <input type="text" name="Year" value="<?php echo $data['Year']; ?>"/>
            </td>
        </tr>
        <tr>
                <td>
                <label for="BodyL">Body Type</label>
            </td>
            <td>
            <input type="text" name="Body" value="<?php echo $data['Body']; ?>"/>
            </td>
        </tr>
        <tr>
                <td>
                <label for="TransmissionL">Transmission</label>
            </td>
            <td>
            <input type="text" name="Transmission" value="<?php echo $data['Transmission']; ?>"/>
            </td>
        </tr>
        <tr>
                <td>
                <label for="FuelL">Fuel</label>
            </td>
            <td>
            <input type="text" name="Fuel" value="<?php echo $data['Fuel']; ?>"/>
            </td>
        </tr>
        <tr>
                <td>
                <label for="ColourL">Colour</label>
            </td>
            <td>
            <input type="text" name="Colour" value="<?php echo $data['Colour']; ?>"/>
            </td>
        </tr>
        <tr>
                <td>
                <label for="EngineL">Engine Size</label>
            </td>
            <td>
            <input type="text" name="Engine" value="<?php echo $data['Engine']; ?>"/>
            </td>
        </tr>
        <tr>
                <td>
                <label for="MileageL">Mileage</label>
            </td>
            <td>
            <input type="text" name="Mileage" value="<?php echo $data['Mileage']; ?>"/>
            </td>
        </tr>
        <tr>
                <td>
                <label for="DescriptionL">Description</label>
            </td>
            <td><textarea name="Description" cols="30" rows="7"><?php echo $data['Description']; ?></textarea>
            </td>
        </tr>
        <tr>
                <td>
                <label for="priceL">Price</label>
            </td>
            <td>
              <p>
                <input type="text" name="price" value="<?php echo $data['Price']; ?>"/>
            </p></td>
        </tr>
        </table>
        <table>
        <tr>
                        <td width="166.5">
            <label><input type="radio" name="Availability" value="1" checked>Available</label>
            </td>
                    <td width="166.5">
           <label><input type="radio" name="Availability" value="0">Sold</label></td>
        </tr>
<tr><td><input type="submit" name="submit" /></td></tr>
<tr><td><input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" /></td></tr>
    </table>
</form>
</body>
</html>
And this is the code for the forms action page i.e. doedit.php
<?php
include('connect.php');

if(isset($_POST['submit'])) {
        if(isset($_POST['Title'])) {
        $Title = $_POST['Title'];
                if(isset($_POST['Registration'])) {
                $Registration = $_POST['Registration'];
                        if(isset($_POST['Year'])) {
                        $Year = $_POST['Year'];
                                if(isset($_POST['Body'])) {
                                $Body = $_POST['Body'];
                                        if(isset($_POST['Transmission'])) {
                                        $Transmission = $_POST['Transmission'];
                                                if(isset($_POST['Fuel'])) {
                                                $Fuel = $_POST['Fuel'];
                                                        if(isset($_POST['Colour'])) {
                                                        $Colour = $_POST['Colour'];
                                                                if(isset($_POST['Engine'])) {
                                                                $Engine = $_POST['Engine'];
                                                                        if(isset($_POST['Mileage'])) {
                                                                        $Mileage = $_POST['Mileage'];
                                                                                if(isset($_POST['Description'])) {
                                                                                $Description = $_POST['Description'];
                                                                                        if(isset($_POST['Price'])) {
                                                                                        $Price = $_POST['Price'];
                                                                                        $Availability = $_POST['Availability'];
                                                                                        $ID = $_POST['id'];
                                                                                        $query = mysql_query("UPDATE stocklist SET Title = '$Title', Registration = '$Registration', Year = '$Year', Body = '$Body', Transmission = '$Transmission', Fuel = '$Fuel', Colour = '$Colour', Engine = '$Engine', Mileage = '$Mileage', Description = '$Description', Price = '$Price', Availability = '$Availability' WHERE ID = '$ID'") or die(mysql_error());
                                                                                        header("Location:manage.php");  
                                                                                        } else {
                                                                                                echo "Please ensure you have entered the Price!";
                                                                                                header("Location:edit.php?id=$ID");
                                                                                        }
                                                                                } else {
                                                                                        echo "Please ensure you have entered the Description!";
                                                                                        header("Location:edit.php?id=$ID");
                                                                                }
                                                                        } else {
                                                                                echo "Please ensure you have entered the Mileage!";
                                                                                header("Location:edit.php?id=$ID");
                                                                        }
                                                                } else {
                                                                        echo "Please ensure you have entered the Engine!";
                                                                        header("Location:edit.php?id=$ID");
                                                                }
                                                        } else {
                                                                echo "Please ensure you have entered the Colour!";
                                                                header("Location:edit.php?id=$ID");
                                                        }
                                                } else {
                                                        echo "Please ensure you have entered the Fuel!";
                                                        header("Location:edit.php?id=$ID");
                                                }
                                        } else {
                                                echo "Please ensure you have entered the Transmission!";
                                                header("Location:edit.php?id=$ID");
                                        }
                                } else {
                                        echo "Please ensure you have entered the Body Type!";
                                        header("Location:edit.php?id=$ID");
                                }
                        } else {
                                        echo "Please ensure you have entered the Year/Reg!";
                                        header("Location:edit.php?id=$ID");
                                }
                } else {
                echo "Please ensure you have entered the Vehicle Registration!";
                header("Location:edit.php?id=$ID");
        }
        } else {
                echo "Please ensure you have entered the Title!";
                header("Location:edit.php");
        }
}
?>
The problem i am facing is, when i click the submit button on the edit.php page, it will refresh the page, delete all the data with in the fields, but it still WILL NOT update the mysql database for that record!

Please help me out!
Thanx
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Update Problem

Post by jacek »

do you get redirected to the manage.php page ?

Also, that level of nesting is a bit insane !
Image
toqi786
Posts: 9
Joined: Mon May 16, 2011 12:43 pm

Re: Update Problem

Post by toqi786 »

no i dont get redirected to the manage.php page

it stays on edit.php, and does not have the pre filled fields anymore, the field are now empty

and about the nesting, it was the only way i could think of to make sure each field is filled in and if not return an error message. if you can help me on that also, it would be appreciated!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Update Problem

Post by jacek »

toqi786 wrote:and about the nesting, it was the only way i could think of to make sure each field is filled in and if not return an error message. if you can help me on that also, it would be appreciated!!
you can use the elseif syntax... example...
if (isset($_POST['thing']) == false){
    // error
}else if (isset($_POST['other']) === false){
    // other error
}else{
    // success, do update query
}
Adding as many blocks as you need.
toqi786 wrote:it stays on edit.php, and does not have the pre filled fields anymore, the field are now empty
if you add
error_reporting(E_ALL);
to the top of the page, do you get any useful info ?
Image
toqi786
Posts: 9
Joined: Mon May 16, 2011 12:43 pm

Re: Update Problem

Post by toqi786 »

jacek wrote:
if (isset($_POST['thing']) == false){
    // error
}else if (isset($_POST['other']) === false){
    // other error
}else{
    // success, do update query
}
error_reporting(E_ALL);
I have used the way you have told me, so my code looks like this now:
<?php
include('webhostconnect.php');

if(isset($_POST['Submit'])) {
	$ID = $_POST['id'];
	if (isset($_POST['Title']) == false){
		echo "Please ensure you have entered the Vehicle Title!";
		header("Location:edit.php?id=$ID");
	}else if (isset($_POST['Registration']) == false){
		echo "Please ensure you have entered the Vehicle Registration!";
		header("Location:edit.php?id=$ID");
	}else if (isset($_POST['Year']) == false){
		echo "Please ensure you have entered the Vehicle Year/Registration!";
		header("Location:edit.php?id=$ID");
	}else if (isset($_POST['Body']) == false){
		echo "Please ensure you have entered the Vehicle Body Type!";
		header("Location:edit.php?id=$ID");
	}else if (isset($_POST['Transmission']) == false){
		echo "Please ensure you have entered the Vehicle Transmission!";
		header("Location:edit.php?id=$ID");
	}else if (isset($_POST['Fuel']) == false){
		echo "Please ensure you have entered the Vehicle Fuel Type!";
		header("Location:edit.php?id=$ID");
	}else if (isset($_POST['Colour']) == false){
		echo "Please ensure you have entered the Vehicle Colour!";
		header("Location:edit.php?id=$ID");
	}else if (isset($_POST['Engine']) == false){
		echo "Please ensure you have entered the Vehicle Engine Size!";
		header("Location:edit.php?id=$ID");
	}else if (isset($_POST['Mileage']) == false){
		echo "Please ensure you have entered the Vehicle Mileage!";
		header("Location:edit.php?id=$ID");
	}else if (isset($_POST['Description']) == false){
		echo "Please ensure you have entered a Vehicle Description!";
		header("Location:edit.php?id=$ID");
	}else if (isset($_POST['Price']) == false){
		echo "Please ensure you have entered the Vehicle Price!";
		header("Location:edit.php?id=$ID");
	}else{
		$Title = $_POST['Title'];
		$Registration = $_POST['Registration'];
		$Year = $_POST['Year'];
		$Body = $_POST['Body'];
		$Transmission = $_POST['Transmission'];
		$Fuel = $_POST['Fuel'];
		$Colour = $_POST['Colour'];
		$Engine = $_POST['Engine'];
		$Mileage = $_POST['Mileage'];
		$Description = $_POST['Description'];
		$Price = $_POST['Price'];
		$Availability = $_POST['Availability'];

		$query = mysql_query("UPDATE stocklist SET Title = '$Title', Registration = '$Registration', Year = '$Year', Body = '$Body', Transmission = '$Transmission', Fuel = '$Fuel', Colour = '$Colour', Engine = '$Engine', Mileage = '$Mileage', Description = '$Description', Price = '$Price', Availability = '$Availability' WHERE ID = '$ID'") or die(mysql_error());
		header("Location:manage.php");
	}
}
?>
and also the error reporting didnt give me anything!

However i used print_r($_POST) and i came to realise i have named the input field 'title' but callin for the post of Title!

I hate it when you spend so much time on something, for something soo small!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Update Problem

Post by jacek »

toqi786 wrote:However i used print_r($_POST) and i came to realise i have named the input field 'title' but callin for the post of Title!
Ah, easy mistake to make, at least you spotted it. And your code is MUCH neater now :D
Image
toqi786
Posts: 9
Joined: Mon May 16, 2011 12:43 pm

Re: Update Problem

Post by toqi786 »

yea thnx
Post Reply