...

View Full Version : PHP UPDATE not working



sonofjack
06-16-2010, 08:13 AM
I have a webpage of listings data that has the function to edit an entry. Very recently this has seemed to stop working. You can go into edit mode but when you submit it it displays the old data. I've spent ages trying to work out what's happened but I can't figure it out.
The server I'm on has recently upgraded and now runs PHP5 instead of PHP4. So I've had a 'magicquotes off' issue to sort out which now works okay
Could the upgrade also have affected other bits of my code?
Here is the main bulk of PHP code in the editlistings page.
This occurs after the database connection.
Thanks



if (isset($_POST['day'])):
// The news text has been updated.


function safe($value){
return mysql_real_escape_string($value);
}



$day = safe($_POST['day']);
$date = safe($_POST['date']);
$channel = safe($_POST['channel']);
$programmetitle = safe($_POST['programmetitle']);
$starttime = safe($_POST['starttime']);
$duration = safe($_POST['duration']);
$notes = safe($_POST['notes']);
$sql = "UPDATE listings SET
day = '$day',
date = '$date',
channel = '$channel',
programmetitle = '$programmetitle',
starttime = '$starttime',
duration = '$duration',
notes = '$notes'
WHERE id='$id'";
if (@mysql_query($sql)) {
header ("location: addlistings.php");
} else {
echo '<p>Error updating listings: ' .
mysql_error() . '</p>';
}
?>

<p><a href="addlistings.php">Return to listings</a></p>

<?php

else: // Edit listings

$id = $_GET['id'];
$listings = @mysql_query (
"SELECT id, day, date_format(date,'%Y/%m/%d') as d, channel, programmetitle, time_format(starttime, '%h:%i%p') as t, duration, notes FROM listings WHERE id='$id'");
if (!$listings) {
exit ('<p>Error fetching listings: ' .
mysql_error() . '</p>');
}


$listings = mysql_fetch_array($listings);

?>

<?php
function safe($value){
return mysql_real_escape_string($value);
}
?>

<?php

$id = $listings['id'];
$day = $listings['day'];
$date = $listings['date'];
$date = $listings['d'];
$channel = $listings['channel'];
$programmetitle = $listings['programmetitle'];
$starttime = $listings['starttime'];
$starttime = $listings['t'];
$duration = $listings['duration'];
$notes = $listings['notes'];


//Convert special characters for safe use
$id = htmlspecialchars($id);
$day = htmlspecialchars($day);
$date = htmlspecialchars($date);
$date = htmlspecialchars($d);
$channel = htmlspecialchars($channel);
$programmetitle = htmlspecialchars($programmetitle);
$starttime = htmlspecialchars($starttime);
$starttime = htmlspecialchars($t);
$duration = htmlspecialchars($duration);
$notes = htmlspecialchars($notes);
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Enter listings</legend>
<div><label for="day">Day:</label> <br /><input id="day" type="text" name="day" value="<?php echo $day; ?>"></div>
<div><label for="date">Date:</label><br /><input id="date" type="text" name="date" value="<?php echo $date; ?>" /></div>
<div><label for="channel">Channel:</label> <br /><input id="channel" type="text" name="channel" value="<?php echo $channel; ?>" /></div>
<div><label for="prog_title">Prog Title:</label><br /> <textarea id="prog_title" name="programmetitle" rows="1" cols="70"><?php echo $programmetitle; ?></textarea></div>
<div><label for="starttime">Start time:</label><br /> <input id="starttime" type="text" name="starttime" value="<?php echo $starttime; ?>" ></div>
<div><label for="duration">Duration:</label> <br /><input id="duration" type="text" name="duration" value="<?php echo $duration; ?>" ></div>
<div><label for="notes">Notes:</label><br /> <textarea id="notes" name="notes" rows="5" cols="70"><?php echo $notes; ?></textarea></div>
<input type="hidden" name="id" value="<?php echo $id; ?>" >
<div><input type="submit" value="SUBMIT"></div>
</form>


<?php endif; ?>

abduraooft
06-16-2010, 08:33 AM
Try
echo $sql = "UPDATE listings SET
day = '$day',
date = '$date',
channel = '$channel',
programmetitle = '$programmetitle',
starttime = '$starttime',
duration = '$duration',
notes = '$notes'
WHERE id='$id'";
if (mysql_query($sql)) {
header ("location: addlistings.php");
} else {
echo '<p>Error updating listings: ' .
mysql_error() . '</p>';
}
and make sure that your query is correct.

sonofjack
06-16-2010, 09:15 AM
Cheers for the prompt reply.
I've tried it but it's had no effect. After submitting it reverts back to the previous data.
Also I forgot to add before that when you go into edit mode it displays the existing data except the Date field and the Starttime field.

Anything else it could be?

abduraooft
06-16-2010, 10:36 AM
Are you sure that your variable $id in the query gets the expected value?

sonofjack
06-16-2010, 10:55 AM
Hmm, I'm not so sure now.

My addlisting code is

Form...

<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?logout=1">Logout</a></p>
<?php if (isset($_GET['addlistings'])): // Add listings ?>

<form action="/dsadmin/addlistings.php" method="post">
<fieldset>
<legend>Enter listings</legend>
<div><label for="day">Day:</label><br /> <input id="day" type="text" name="day"></div>
<div><label for="date">Date:</label><br /><input id="date" type="text" name="date"></div>
<div><label for="channel">Channel:</label><br /> <input id="channel" type="text" name="channel"></div>
<div><label for="prog_title">Prog Title:</label> <br /><textarea id="prog_title" name="programmetitle" rows="1" cols="70"></textarea></div>
<div><label for="starttime">Start time:</label><br /> <input id="starttime" type="text" name="starttime"></div>
<div><label for="duration">Duration:</label> <br /><input id="duration" type="text" name="duration"></div>
<div><label for="notes">Notes:</label> <br /><textarea id="notes" name="notes" rows="5" cols="70"></textarea></div>
<div><input type="submit" value="SUBMIT" /></div>
</fieldset>
</form>




and after connecting to the database .
Am I missing a $id here or something?



<?php
function safe($value){
return mysql_real_escape_string($value);
}
?>

<?
//If news has been submitted, add it to the database.
if(isset($_POST['day'])) {
$day = safe($_POST['day']);
$date = safe($_POST['date']);
$channel = safe($_POST['channel']);
$programmetitle = safe($_POST['programmetitle']);
$starttime = safe($_POST['starttime']);
$duration = safe($_POST['duration']);
$notes = safe($_POST['notes']);
$sql = "INSERT INTO listings SET
day='$day',
date='$date',
channel='$channel',
programmetitle='$programmetitle',
starttime='$starttime',
duration='$duration',
notes='$notes'";

if (@mysql_query($sql)) {
echo '<p>Listings added.</p>';
} else {
echo '<p>Error adding listing: ' .
mysql_error() . '</p>';
}
}




//If listing has been deleted remove from database.
if (isset($_GET['deletelistings'])) {
$id = $_GET['deletelistings'];
$sql = "DELETE FROM listings
WHERE id='$id'";
if (@mysql_query($sql)) {
echo '<p>This listings has been deleted.</p>';
} else {
echo '<p>Error deleting listings: ' .
mysql_error() . '</p>';
}
}



echo '<p><span style="font-size: 130%;"><b>TV and Radio Listings </b></span> <i>Check back weekly<i></p><br />';



// Request the ID and news text
$result = @mysql_query("SELECT id, day, date_format(date,'%d/%m/%Y') as d, channel, programmetitle, time_format(starttime, '%h:%i%p') as t, duration, notes FROM listings ORDER BY date, starttime ASC");
if(!$result) {
exit('<p>Error performing query: ' .
mysql_error() . '</p>');
}

// Display the listings with Delete and Edit links.
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$day = $row['day'];
$date = $row['date'];
$date = $row['d'];
$channel = $row['channel'];
$programmetitle = $row['programmetitle'];
$starttime = $row['starttime'];
$starttime = $row['t'];
$duration = $row['duration'];
$notes = $row['notes'];

echo '<table><tr>
<td valign="top" align="left" width="50px">
<a href="' . $_SERVER['PHP_SELF'] .
'?deletelistings=' . $id . '">
<img src="bin.jpg" width="20px" border="0"></a>
<a href="editlistings.php?id= ' . $id .'">
<img src="edit.jpg" width="20px" border="0"></a></td>


<td valign="top" align=left width=100>' . $day .'</td>
<td valign="top" align=left width=70>' .$date .' </td>
<td valign="top" align=left width=70>' .$starttime .'</td>
<td valign="top" align=left width=100>' .$channel .'</td>
<td valign="top" align=left width=150><b>' .$programmetitle .'</b> </td>
<td valign="top" align=left width=60>' .$duration .'</td>
</tr><tr><td></td>
<td valign=top colspan=6 align="left">' .$notes .'</td>
</tr></table>';
}

// Add listing

echo '<p><a href="' . $_SERVER['PHP_SELF'] .
'?addlistings=1"><span style="font-size: 130%"><b>Add listing</b></a></span>';
endif;

abduraooft
06-16-2010, 11:06 AM
<?php if (isset($_GET['addlistings'])): // Add listings ?>
You'd need to use a hidden input or similar to pass the value from one page to another via GET/POST

sonofjack
06-16-2010, 11:58 AM
Okay, do you mean that bit of code you quote is incorrect, or I need to add something else too? I thought I was on top of PHP at one stage but I think I'm getting lost a bit now.
Is this connected with going from PHP4 to PHP5?
Thanks again
Paul



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum