Updating Record

Hi,

I am having problems with this page for updating DB records. I get no errors and does not update the record when I click update. I’m guessing it’s something simple that I keep missing. I would be very grateful for any help anyone can give.

[php]

<?php { require ("header.php"); require ("connect.php"); $pid=$_GET['id']; if (!$_POST['part_number'] ) { die(' </table'); } $number=$_POST['part_number']; $description=$_POST['part_description']; $datasheet=$_POST['part_number']; $a=$_POST['part_a']; $b=$_POST['part_b']; $c=$_POST['part_c']; $d=$_POST['part_d']; $e=$_POST['part_e']; $f=$_POST['part_f']; $g=$_POST['part_g']; $p=$_POST['part_p']; $shape=$_POST['part_shape']; $cabledia=$_POST['part_cabledia']; $imped25=$_POST['part_imped25']; $imped100=$_POST['part_imped100']; $cat=$_POST['part_cat']; $family=$_POST['part_family']; $manufacturer=$_POST['part_manufacturer']; $website=$_POST['part_web']; $website2=$_POST['part_web2']; $rosh=$_POST['part_rohs']; $image=$_POST['part_number']; $query="UPDATE * parts SET part_number='$number', part_description = '$description', part_datasheet = '$datasheet', part_a = '$a', part_b = '$b', part_c = '$c', part_d = '$d', part_e = '$e', part_f = '$f', part_g = '$g', part_p = '$p', part_shape = '$shape', part_cabledia = '$cabledia', part_imped25 = '$imped25', part_imped100 = '$imped100', part_cat = '$cat', part_family = '$family', part_manufacturer = '$manufacturer', Part_website = '$website', part_website2 = '$website2', part_rosh = '$rosh', part_image = '$image' WHERE part_id = '$pid'"; mysql_query($query); // if(!$result) // { // die('Could not connect: ' . mysql_error()); // } echo '
You did not complete all of the required fields

Back to Update Part
Successful

Back to Update Part
'; require ("footer.php"); } ?>

[/php]

For now the only problem i see with your script is the wild card character * in front of your update command. This character is used for selecting all items from a table. For the update command though, you have to set the value you need to update at each column and use a where clause to specify the row you want the changes to affect. If you are making use of this character because you want the entire table to be updated, you can do this instead with a loop.

Hi,

Thanks for your reply. I tried a few things but still not having any luck. How to you mean update each column?

Remove the * in front of your update keyword stored in $query and try again. It should work

Yes, remove the “*” !

Also, you should be able to find out the error if you put your error display back into your query.

Something like this:
[php]
$result = mysql_query($query) or die('Query Error: ’ . mysql_error());
[/php]

Then, you can use the $result to see how many rows were affected or whatever you want to do.
Most programmers place the “or die” on each part of database access such as “connection”, “DBtable”
and “Queries”. In this way if any part dies, you know about it. Hope that helps…

Try the following…

[php]$sql=“UPDATE parts SET
part_number=$number,
part_description = ‘$description’,
part_datasheet = ‘$datasheet’,
part_a = ‘$a’,
part_b = ‘$b’,
part_c = ‘$c’,
part_d = ‘$d’,
part_e = ‘$e’,
part_f = ‘$f’,
part_g = ‘$g’,
part_p = ‘$p’,
part_shape = ‘$shape’,
part_cabledia = ‘$cabledia’,
part_imped25 = ‘$imped25’,
part_imped100 = ‘$imped100’,
part_cat = ‘$cat’,
part_family = ‘$family’,
part_manufacturer = ‘$manufacturer’,
Part_website = ‘$website’,
part_website2 = ‘$website2’,
part_rosh = ‘$rosh’,
part_image = ‘$image’
WHERE part_id = $pid”;[/php]

There are a few subtle changes yo umight not see. First the “*” has to go (I am sure you got that from the classic “SELECT * FROM …” - in UPDATE you are just telling it to update a specific field.

The next changes are the quotes. The nice thing about PHP (or bad depending on perspective) is it is a lazy language. I do not know how your DB is set up initially, so I took a guess. But if you have your part_number and part_id set as"int"(or integer) you should not have a single quote. so rule of thumb: varchar, text etc… DO QUOTE the variable, otherwise DO NOT QUOTE the variable.

It is also good practice to have all fields set with a “reverse” quote (upper left key on below “esc” key).

Another good practice, as Ernie pointed out, is to always include a error trap on your queries. In your example I do something like this.

[php]$query = mysql_query($sql) or die(“Bad Query String:
$sql
”.mysql_error());[/php]

This is the reason I break it up into 2 sections. MySQL is horrible at telling you what is wrong, so sometimes seeing your sql string in action, will make a light go on.

Hope this helps.

ps
final note, for readability, it would not hurt to format your sql strings as shown above. This way when you go back, your not scrolling all over your page, and you can quickly see what you are doing.

Thank you all for the help and tips. Got it working now. :slight_smile:

what did you do, so that others that stumble by with the same issue, can benefit?

Sponsor our Newsletter | Privacy Policy | Terms of Service