Well having thought I'd got over all the hurdles of this damn database with my previous post, it seems I hadn't quite suffered enough, and have been thrown another challenge.

I have a piece of code which fills a form in with the current details of a car record.

Form
----

<form action="updatedCar.php" method="post">
        <input type="hidden" name="ud_stockNumber" value="<? echo $scn; ?>">
            Category: <input type="text" name="ud_Category" value="<? echo $Category; ?>"> 
        Must be either 'Classic', 'Modern', or 'Range_Rover'<br />
            Make: <input type="text" name="ud_Make" value="<? echo $Make; ?>"><br />
            Model: <input type="text" name="ud_Model" value="<? echo $Model; ?>"><br />
            Reg Year: <input type="text" name="ud_Reg_Year" value="<? echo $Reg_Year; ?>"><br />
            Reg Letter: <input type="text" name="ud_Reg_Letter" value="<? echo $Reg_Letter; ?>"><br />
            Mileage: <input type="text" name="ud_Mileage" value="<? echo $Mileage; ?>"><br />
            Price: <input type="text" name="ud_Price" value="<? echo $Price; ?>"><br />
            Price Alternate: <input type="text" name="ud_Price_Alternate" value="<? echo $Price_Alternate; ?>"><br />
            Description: <textarea id="TextArea1" cols="60" rows="4" name="ud_Description"><? echo $Description; ?></textarea><br />
            Image 1: <input type="text" name="ud_Image1" value="<? echo $Image1; ?>"><br />
            Image 2: <input type="text" name="ud_Image2" value="<? echo $Image2; ?>"><br />
            Image 3: <input type="text" name="ud_Image3" value="<? echo $Image3; ?>"><br />
            Image 4: <input type="text" name="ud_Image4" value="<? echo $Image4; ?>"><br />
        <input type="Submit" value="Update">
    </form>

The idea is that the user edits anything they like within the form, and clicks Update.

This then runs updatedCar.php

<?php
    
        $ud_stockNumber=$_GET['ud_stockNumber'];
        $ud_Category=$_POST['ud_Category'];
        $ud_Make=$_POST['ud_Make'];
        $ud_Model=$_POST['ud_Model'];
        $ud_Reg_Year=$_POST['ud_Reg_Year'];
        $ud_Reg_Letter=$_POST['ud_Reg_Letter'];
        $ud_Mileage=$_POST['ud_Mileage'];
        $ud_Price=$_POST['ud_Price'];
        $ud_Price_Alternate=$_POST['ud_Price_Alternate'];
        $ud_Description=$_POST['ud_Description'];
        $ud_Image1=$_POST['ud_Image1'];
        $ud_Image2=$_POST['ud_Image2'];
        $ud_Image3=$_POST['ud_Image3'];
        $ud_Image4=$_POST['ud_Image4'];
             
        $username="usrname";
        $password="pswrd";
        $database="dbName";
        
        mysql_connect(localhost,$username,$password);
        @mysql_select_db($database) or die( "Unable to select database");

        $query="UPDATE cars SET Category='$ud_Category', Make='$ud_Make', Model='$ud_Model', Reg_Year='$ud_Reg_Year', Reg_Letter='$ud_Reg_Letter', Mileage='$ud_Mileage', Price='$ud_Price', Price_Alternate='$ud_Price_Alternate', Description='$ud_Description', Image1='$ud_Image1', Image2='$ud_Image2', Image3='$ud_Image3', Image4='$ud_Image4' WHERE ID='$ud_stockNumber'";
        mysql_query($query);
        echo "Record Updated";

        mysql_close();

       

    ?>

But the database doesn't change.

Now from my reading of the tutorials, I can't find any differences between the sample code and my code, other than variable names.

Again, I think this could be a case of staring at code for too long...

Any help will be greatly appreciated.

Ed

Recommended Answers

All 15 Replies

hi,
change database like this one

Category='$ud_Category'

to

Category='".$ud_Category."'

just to make it perfect and just see whether parameters are posting are not put

print_r($_POST)

after submit.
post here if any doubts

Have made the change you suggested ( '".$ud_Category."' etc.)

As for the print_r($_POST)...

Well if I'm reading the print output correctly (and having worked with php for oh, at least a week) I doubt it ;-)

Record UpdatedArray ( [ud_stockNumber] => 5 [ud_Category] => Range_Rover [ud_Make] => Range Rover [ud_Model] => Sport [ud_Reg_Year] => 2005 [ud_Reg_Letter] => Q [ud_Mileage] => 15000 [ud_Price] => [ud_Price_Alternate] => Coming Soon [ud_Description] => Sports version of the Range Rover [ud_Image1] => car.jpg [ud_Image2] => blank.jpg [ud_Image3] => blank.jpg [ud_Image4] => blank.jpg )

I'd say that meant that the record was being updated.
For reference, the original had a 'V' as the Reg Letter, I changed it to a 'Q'.

But the database doesn't change.

execute this query

$query="UPDATE cars SET `Category`='".$ud_Category."', `Make`='".$ud_Make."', `Model`='".$ud_Model."', `Reg_Year`='".$ud_Reg_Year."', `Reg_Letter`='".$ud_Reg_Letter."', `Mileage`='".$ud_Mileage."', `Price`='".$ud_Price."', `Price_Alternate`='".$ud_Price_Alternate."', `Description`='".$ud_Description."', `Image1`='".$ud_Image1."', `Image2`='".$ud_Image2."', `Image3`='".$ud_Image3."', `Image4`='".$ud_Image4."' WHERE `ID`='".$ud_stockNumber."'";

if you dont get it echo it and paste it to mysql and see what the error is. let us know what mysql says

$ud_stockNumber=$_GET;
here you are using get method but you sent values in post method so when you are trying to update it is not updated because $ud_stockNumber contains null value.
so take $ud_stockNumber=$_POST;

Not sure I quite follow you there.

The query you say to execute is the query that is set to execute already, and I'm not sure what you then want me to echo.

I tried echoing that whole query but just get a parse error.

Ah, cheers, your second post worked.
Changed it from _GET to _POST

I'd been using _GET with a page earlier which retrieved a value sent with the URL.

Cheers for your help.

i test your code no error's on this:

$query="UPDATE cars SET Category='$ud_Category', Make='$ud_Make', Model='$ud_Model', Reg_Year='$ud_Reg_Year', Reg_Letter='$ud_Reg_Letter', Mileage='$ud_Mileage', Price='$ud_Price', Price_Alternate='$ud_Price_Alternate', Description='$ud_Description', Image1='$ud_Image1', Image2='$ud_Image2', Image3='$ud_Image3', Image4='$ud_Image4' WHERE ID='$ud_stockNumber'";


Can you change this to POST in updatedCar.php.
$ud_stockNumber=$_POST['ud_stockNumber'];

see your hidden field my friend.

I take it back!
Everything can be updated, except for the Price :S

Updated code, its almost identical to the previous code though.

The Form
-----------

<form action="updatedCar.php" method="post">
        <input type="hidden" name="ud_stockNumber" value="<? echo $scn; ?>">
            Category: <input type="text" name="ud_Category" value="<? echo $Category; ?>"> 
        Must be either &#39;Classic&#39;, &#39;Modern&#39;, or &#39;Range_Rover&#39;<br />
            Make: <input type="text" name="ud_Make" value="<? echo $Make; ?>"><br />
            Model: <input type="text" name="ud_Model" value="<? echo $Model; ?>"><br />
            Reg Year: <input type="text" name="ud_Reg_Year" value="<? echo $Reg_Year; ?>"><br />
            Reg Letter: <input type="text" name="ud_Reg_Letter" value="<? echo $Reg_Letter; ?>"><br />
            Mileage: <input type="text" name="ud_Mileage" value="<? echo $Mileage; ?>"><br />
            Price: <input type="text" name="ud_Price" value="<? echo $Price; ?>"><br />
            Price Alternate: <input type="text" name="ud_Price_Alternate" value="<? echo $Price_Alternate; ?>"><br />
            Description: <textarea id="TextArea1" cols="60" rows="4" name="ud_Description"><? echo $Description; ?></textarea><br />
            Image 1: <input type="text" name="ud_Image1" value="<? echo $Image1; ?>"><br />
            Image 2: <input type="text" name="ud_Image2" value="<? echo $Image2; ?>"><br />
            Image 3: <input type="text" name="ud_Image3" value="<? echo $Image3; ?>"><br />
            Image 4: <input type="text" name="ud_Image4" value="<? echo $Image4; ?>"><br />
        <input type="Submit" value="Update">
    </form>

updatedCar.php

<?php
    
        $ud_stockNumber=$_POST['ud_stockNumber'];
        $ud_Category=$_POST['ud_Category'];
        $ud_Make=$_POST['ud_Make'];
        $ud_Model=$_POST['ud_Model'];
        $ud_Reg_Year=$_POST['ud_Reg_Year'];
        $ud_Reg_Letter=$_POST['ud_Reg_Letter'];
        $ud_Mileage=$_POST['ud_Mileage'];
        $ud_Price=$_POST['ud_Price'];
        $ud_Price_Alternate=$_POST['ud_Price_Alternate'];
        $ud_Description=$_POST['ud_Description'];
        $ud_Image1=$_POST['ud_Image1'];
        $ud_Image2=$_POST['ud_Image2'];
        $ud_Image3=$_POST['ud_Image3'];
        $ud_Image4=$_POST['ud_Image4'];
             
        $username="edmundr1_peterhu";
        $password="austin1300";
        $database="edmundr1_carsForSale";
        
        mysql_connect(localhost,$username,$password);
        @mysql_select_db($database) or die( "Unable to select database");

        $query="UPDATE cars SET Category='".$ud_Category."', Make='".$ud_Make."', Model='".$ud_Model."', Reg_Year='".$ud_Reg_Year."', Reg_Letter='".$ud_Reg_Letter."', Mileage='".$ud_Mileage."', Price='".$ud_Price."', Price_Alternate='".$ud_Price_Alternate."', Description='".$ud_Description."', Image1='".$ud_Image1."', Image2='".$ud_Image2."', Image3='".$ud_Image3."', Image4='".$ud_Image4."' WHERE ID='".$ud_stockNumber."'";
        mysql_query($query);
        echo "Record Updated";

        mysql_close();

       
       

    ?>

check once bd field names and it's types, field names correctly in the update query check once.

check once bd field names and it's types, field names correctly in the update query check once.

Sorry I have read through that several times and still can't quite understand what you are suggesting, other than checking that the field types are correct, which as far as I can tell they are, there are no errors that I can see in my naming between the two pages.

It appears to only be when I try to set it to a NULL value (when Price_Alternate takes over instead of an actual price)
The table keeps setting the price to 0.

Is there a way round this?


Cheers.

change your database structure in your amount 
price decimal(12,2)
   instead of
price int(8)


control your price if they type comma.
$ud_priceX = str_replace(",","",$ud_price); 

$ud_priceX put this in your $query.

Cheers, I shall try it out tomorrow after uni.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.