Old 14-05-2007, 05:38   #1 (permalink)
pagey
Resident Cheese Expert
 
pagey's Avatar
 
Join Date: Aug 2006
Location: South Wales
Posts: 424
Update SQL not working with PHP

Hi there, I've been working on a PHP/MySQL website for a while. I thought everything was working originally, but now it seems my update has stopped working. It doesn't throw up an error, but the information is just the same as before.

The site is here. Not sure how much you can tell from it.

The actual code handling the update bits are as follows:

editentry1.php

PHP Code:
$id     = (int) $_GET['id']; //~ make the id a number
$query  = "SELECT * FROM details WHERE id=". $id; //~ no quotes because its a number

$result = @mysql_query($query) or die ( mysql_error()."<br><b>$query</b>" );

if ( mysql_num_rows($result) != 1 ) { //~ If there isn't 1 row returned, error message
    echo mysql_num_rows($result);  //echo '<p class="text">Error</p>';
    } else { //~ success, get the details
     
        
    $row = mysql_fetch_array($result);
    //~  array for radios
    $ud_title = array ('Mr', 'Mrs', 'Miss', 'Ms');  

$salutation_form = '';
foreach ($ud_title as $title_id => $title_salutation) {
//~ check if the value equals the salutation being printed out
$checked = ($row['Title'] == $title_salutation) ? ' checked="checked"' : '';
$salutation_form .= '<input type="radio" name="ud_title" value="'. $title_salutation .'"'. $checked  .' />'. $title_salutation ."\n";
}
?>


<table align="center" border="0">
<form method="post" action="update.php" onSubmit="return validateForm(edit);">
<input type="hidden" name="ud_id" value="<?php echo $row['id'?>" />

<tr><td class="text">Title: </td><td> <?php echo $salutation_form ?></td></tr>

<tr><td class="text">Forename(s): </td><td><input type="text" size="20" maxlength="40" name="ud_forename" value="<?php echo $row['Forename'?>"></td></tr>
<tr><td class="text">Surname: </td><td><input type="text" size="20" maxlength="40" name="ud_surname" value="<?php echo $row['Surname'?>"></td></tr>
<tr><td class="text">Address: </td><td><input type="text" size="50" maxlength="50" name="ud_address" value="<?php echo $row['Address'?>"></td></tr>
<tr><td class="text">Telephone Number: </td><td><input type="text" size="40" maxlength="40" name="ud_tel" value="<?php echo $row['Tel'?>"></td></tr>
<tr><td class="text">Mobile Telephone Number: </td><td><input type="text" size="40" maxlength="40" name="ud_mob_tel" value="<?php echo $row['Mob_Tel'?>"></td></tr>
<tr><td class="text">E-Mail Address: </td><td><input type="text" size="40" maxlength="40" name="ud_email" value="<?php echo $row['Email'?>"></td></tr>
<tr><td colspan="2" align="center"><input type="Submit" value="Update" /></td></tr>

update.php

PHP Code:
include ('dbconnect.html');

$ud_id=$_POST['ud_id'];
$ud_title=$_POST['ud_title'];
$ud_forename=$_POST['ud_forename'];
$ud_surname=$_POST['ud_surname'];
$ud_address=$_POST['ud_address'];
$ud_tel=$_POST['ud_tel'];
$ud_mob_tel=$_POST['ud_mob_tel'];
$ud_email=$_POST['ud_email'];

//$dbc = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());
$id=$_GET['id'];
@
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' mysql_error());

$query "UPDATE $table SET title='$ud_title', forename='$ud_forename', surname='$ud_surname', address='$ud_address', tel='$ud_tel', mob_tel='$ud_mob_tel', email='$ud_email' WHERE id='$ud_id'";
mysql_query($query);
echo 
"<p class=\"text\">Record Updated</p>";
mysql_close();
echo 
'<br />'
?> 

I have to hand in this bloody assignment on Friday, so any help would be greatly appreciated!

Cheers
  Reply With Quote
Old 14-05-2007, 06:10   #2 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,620
Okay first thought when my insert and updates stop working has a col that is being addressed in the SQL been removed or renamed in the DB?

Secondly you have $table in the query, is $table defined or is it typo?
__________________
  Reply With Quote
Old 14-05-2007, 06:26   #3 (permalink)
pagey
Resident Cheese Expert
 
pagey's Avatar
 
Join Date: Aug 2006
Location: South Wales
Posts: 424
I haven't touched the databse, so it can't be that.

Also, the $table is defined in my dbconnect.php include.

All of the other pages that involve access to the databse are working fine, which makes me think it must be something isolated on one of the update pages.

As I said, it doesn't throw up an error, just acts like its worked, but hasn't!
  Reply With Quote
Old 14-05-2007, 06:49   #4 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,620
is id forbidden name?
try user_id?
__________________
  Reply With Quote
Old 14-05-2007, 07:02   #5 (permalink)
pagey
Resident Cheese Expert
 
pagey's Avatar
 
Join Date: Aug 2006
Location: South Wales
Posts: 424
Nah, its called id in the table, and referred to as 'id' throughout the whole website.

This is highly irritating!
  Reply With Quote
Old 31-05-2007, 06:27   #6 (permalink)
LazyMoon
Registered
 
LazyMoon's Avatar
 
Join Date: Sep 2006
Location: Adelaide, Australia
Posts: 12
The UPDATE command (as far as I remember) wont throw an error if it is trying to update a row (as selected by the WHERE clause) when no such row exists in the table.

Say for example, trying to update row #45 when there is no row with an ID of 45.

Also, I highly recommend you wrap all the field names in "`" to make it easier to distinguish between field names and other data.
  Reply With Quote
Old 31-05-2007, 06:32   #7 (permalink)
LazyMoon
Registered
 
LazyMoon's Avatar
 
Join Date: Sep 2006
Location: Adelaide, Australia
Posts: 12
Also, just in case. You wrote:
Code:
include ('dbconnect.html');
But you referred to it as dbconnect.php.

If you have two files then your script may be referring to an old version of the file which may use a different database or table.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8