473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Update row via HTML form

23
A little problem I've run into is the following.

I have a script that allows a user to edit a story.

I have an HTML form for title and main_text which gets it's values by pulling the selected data from the database.

If the user either

i) doesn't change anything, and then saves.
or
ii)enters the exact same text.

[php]if (mysql_affected_rows() == 1) [/php]

fails, so an error message is output.

But in fact there is no error, as everything ran fine.

Hopefully I explained that alright.

Do you have any ideas on how to ffix this problem?

Thanks

Here is the full code
[php]
<?php
# Filename - edit_story.php
# Date - 9th August 2007
# Author - Stephen Hoult
# Author Email - stephen@hoult.org.uk

// This file allows logged in users to edit a story.

// Include config file for error management and such
include('./includes/config.inc.php');

// Set page title and include HTML header
$page_title = 'Edit a Story';
include('./includes/header.html');


// If no first_name variable exists, redirect the user.
if (!isset($_SESSION['first_name'])) {

// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}

// Add the page.
$url .= '/login.php';

ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.

} else { // First name variable exists - user is logged in


// Connect to the Database
require_once('../mysql_connect_ur.php');


if (isset($_POST['submitted'])) { // if the form has beeen submitted

// Get story ID
if (isset($_POST['id'])) {
$id = escape_data($_POST['id']);

} else {
$id = FALSE;
}

// Handle the form

// Validate title
if(!empty($_POST['title'])) {
$t = escape_data($_POST['title']);
} else {
echo '<p><font color="red" size="+1">Please enter a title.</font></p>';
}

// Validate main text
if(!empty($_POST['main_text'])) {
$mt = escape_data($_POST['main_text']);
} else {
echo '<p><font color="red" size="+1">Please enter the main text.</font></p>';
}


// Check that the ID exists in the database.
// Build query
$query = "SELECT id, title, main_text FROM content WHERE id = $id";
$result = mysql_query($query);
$num = mysql_num_rows($result);



if ($num == 1) { // If a row was found by the query - ie the story id exists in the table.

// Update the database
$query = "UPDATE content SET title='$t', main_text='$mt' WHERE id = $id";
$result = mysql_query($query);


// Check to see if update was successful
if (mysql_affected_rows() == 1) { // If 1 row was affected by the update
echo '<p>The story has been edited.</p>';

} else {
echo '<p><font color="red" size="+1">The story could not be updated. Please try again</font></p>';
}// End If update was successful


} else {
echo '<p><font color="red" size="+1">The selected ID does not exist in the database. Please try again.</font></p>';
} // End mysql_num_rows() to see if id exists in the table.


} else { // The form has not yet been submitted - Display the form

// Get story ID
if (isset($_GET['id'])) {
$id = escape_data($_GET['id']);

} else {
$id = FALSE;
}

$query = "SELECT id, title, main_text FROM content WHERE id = $id";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
?>



<fieldset><legend>Edit a story</legend>

<form action="edit_story.php" method="post" >

<p>Title: <input type="text" name="title" size="30" maxlength="50" value="<?php if (isset($_POST['title'])) {echo $_POST['title'];
} else { echo $row['title'];} ?>" /></p>

<p>Main Text<textarea name="main_text" cols="40" rows="5"/><?php if (isset($_POST['main_text'])) {echo $_POST['main_text'];
} else { echo $row['main_text'];} ?></textarea></p>

<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="<?php $_POST['id'] = $id; echo $_POST['id'] ;?>">


</form>

</fieldset>

<?php
}// End of if is submitted

} // End of is logged in

include('./includes/footer.html'); // Include HTML footer
?>[/php]
Aug 11 '07 #1
5 2456
keeps21
23
I assume one way to do this would be to add a field 'modified_on' as a timestamp, to my database and add the appropriate values to the query.

Then when updated, even if none of the title, or main text changes, the 'modified_on' field would, so the error would not be kicked out.
Aug 11 '07 #2
Atli
5,058 Expert 4TB
Try changing
Expand|Select|Wrap|Line Numbers
  1. if(mysql_affected_rows() == 1)
  2.  
To
Expand|Select|Wrap|Line Numbers
  1. if($result)
  2.  
If a query failes, the $result is always FALSE. But when a query is successful it either retuns data, or it retuns TRUE which will both be evaluated as true in an if statement.
Aug 11 '07 #3
keeps21
23
Thanks that works great.

I tried adding a timestamp as i thought about in post #2 and updating it when the query runs and it also worked great.
Aug 11 '07 #4
Atli
5,058 Expert 4TB
Glad I could help.

The timestamp thing is a good idea. You could add a 'Last edited at' thing to your stories :)
Aug 11 '07 #5
keeps21
23
Yeah that was what I thought when I was thinking about adding the TIMESTAMP.

Although in hindsight it may be easier in the long run to use DATETIME instead of TIMESTAMP .

This would save having to convert back and forth between the two whe outputing the Last modified date and time.

Thanks for your help. :)
Aug 11 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mark | last post by:
A beginner in this area, I have been able to read a record from a MySQL database and populate an HTML form (wow!). Now, my goal is to allow the user to edit the contents of the form and then...
3
by: Fredrik/Sweden | last post by:
Hi folks ! got this problem... i have a table 'Accounts' in my database, which contains a bunch of users. From the main menu i choose "edit user" and all users in the db are presented in a table....
1
by: mursyidatun ismail | last post by:
Dear all, database use: Ms Access. platform: .Net i'm trying to update a record/records in a table called t_doctors by clicking da edit link provided in the database. when i ran through da...
0
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase...
5
by: cover | last post by:
I have an input form that passes data when submitted to a second form to let the user know what they have just entered into the db. My question comes with using 'update'. I'd like to query the...
1
by: teenagelcruise | last post by:
hi, i have a problem with my code which is i cannot update and addnew data into the database but i can delete the data.plz give me an idea.this is my code that i wrote. <html> <head> <meta...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
11
by: stantron | last post by:
Setup: I only have one database with one table in it. The first page has a form that adds a record (w/ 6 fields in it) to the mySQL database's lone table via PHP. This works fine. I also have a PHP...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.