Hi,

Firstly apologies as i shouldnt have set a previous thread as solved.

The following script should:

1. get record with id of 1 and display in text box
2. after user changes it, it is written to mysql
3. the screen informs user that this has been achieved

<?php
$result = mysql_query("select * from indexinfo  where id = 1");
$row = mysql_fetch_assoc($result);
if ( isset( $_POST['send'] ) ) {
  // The author's details have been updated.

  
  $title = $_POST['Title'];
  $subtitle = $_POST['Subtitle'];
  $content = $_POST['Content'];
  $author = $_POST['Author'];
  $date = $_POST['Date'];
  $id = $_POST['id'];

  $sql = "UPDATE `indexinfo` SET
          `Title` = '$title',
          `Subtitle` = '$subtitle',
	    `Content` = '$content',
	    `Author` = '$author',
          `Date` = '$date'
          WHERE `id` = '$id'";
  if (mysql_query($sql)) {
    echo '<p>Author details updated.</p>';
  } else {
    echo '<p>Error updating author details: ' .
        mysql_error() . '</p>';
  }

?>


<form action="#" method="post">
<p>Edit the author:</p>
<label>Title: <input type="text" name="Title" value="<?php echo $title; ?>" /></label><br />
<label>Subtitle: <input type="text" name="Subtitle" value="<?php echo $subtitle; ?>" /></label><br />
<label>Content: <TEXTAREA name="Content" ROWS=10 COLS=100><?php echo $content; ?></TEXTAREA><br />
<label>Author: <input type="text" name="Author" value="<?php echo $author; ?>" /></label><br />
<label>Date: <input type="text" name="Date" value="<?php echo $date; ?>" /></label><br />
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="submit" value="Submit" name="send" /></p>



</form>
<?php
} else {
?>
<form action="#" method="post">
<p>Edit the author:</p>
<label>Title: <input type="text" name="Title" /></label><br />
<label>Subtitle: <input type="text" name="Subtitle" /></label><br />
<label>Content: <TEXTAREA name="Content" ROWS=10 COLS=100></TEXTAREA><br />
<label>Author: <input type="text" name="Author" /></label><br />
<label>Date: <input type="text" name="Date" /></label><br />
<input type="hidden" name="id" />
<input type="submit" value="Submit" name="send" /></p>
</form>
<?php } ?>

Dani AI

Generated

— the behaviour described matches three common mistakes: the SELECTed row is fetched but never used to populate the form on first load, the hidden id field can be empty so the UPDATE's WHERE matches nothing, and a successful mysql_query call can still affect zero rows (so you need to check the affected-rows count and any DB error output). was right to flag the unused $row, and 's request for errors is exactly what to do next. 's "or die" tip is useful while debugging, but a cleaner approach is to check return values and report the specific error and affected rows.

Quick debug checklist

  • Ensure a working DB connection and check the SELECT result immediately; if it fails print/log the error.
  • Populate form values from the fetched row (or assign variables from the row) before rendering the HTML; include the record id in a hidden field so the UPDATE has a target. Use htmlspecialchars when echoing into value attributes to avoid breaking the HTML.
  • On POST, validate/sanitize inputs and run the UPDATE. Check the query result and also check the number of affected rows — 0 means nothing changed (either WHERE mismatched or values were identical). If debugging, log the final SQL string (with care for sensitive data) or the DB error text.
  • After a successful update, re-query to repopulate the form or redirect (Post/Redirect/Get) to avoid duplicate submissions.

A concise, safer pattern (use mysqli with prepared statements rather than old mysql_* calls):

$mysqli = new mysqli('host','user','pass','db');
if ($mysqli->connect_errno) die('Connect error: '.$mysqli->connect_error);

$stmt = $mysqli->prepare('SELECT Title, Subtitle, Content, Author, Date FROM indexinfo WHERE id=?');
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->bind_result($title,$subtitle,$content,$author,$date);
$stmt->fetch();
$stmt->close();

if ($_SERVER['REQUEST_METHOD']==='POST') {
  // validate/trim $_POST values, then:
  $stmt = $mysqli->prepare('UPDATE indexinfo SET Title=?, Subtitle=?, Content=?, Author=?, Date=? WHERE id=?');
  $stmt->bind_param('sssssi',$title,$subtitle,$content,$author,$date,$id);
  $stmt->execute();
  if ($stmt->affected_rows>0) { /* success */ }
  $stmt->close();
  header('Location: '.$_SERVER['REQUEST_URI']); exit;
}

Final notes: migrate off deprecated mysql_* functions (use mysqli or PDO), always escape output with htmlspecialchars, prefer prepared statements to prevent injection, and avoid using ambiguous column names like Date where possible.

Recommended Answers

All 5 Replies

It'd help if you told us what errors you're getting.

Hi there,
You don't actually have a problem statement in your post, so I'm not really sure where you are going wrong.

But, I notice that after querying the database at the top of the file, you don't do anything with the $row variable, which is why you aren't seeing anything in your form the first time you load the page.

Hope this helped

What do you mean as in if you haven't submitted the form you still get the row updated message?

The form itself displays each of the headers.... title, subtitle, content, author and date..... but it doesnt echo the data that is held in the mysql database.

On changing the cell information and clicking submit, a success message is given, but the row in the database is not updated.

The form itself displays each of the headers.... title, subtitle, content, author and date..... but it doesnt echo the data that is held in the mysql database.

On changing the cell information and clicking submit, a success message is given, but the row in the database is not updated.

Okay in that case, please add the following at the end of your update query.

$query = mysql_query("QUERY") or die( "UPDATE ERROR:" . mysql_error() );
// QUERY being the update query you have been using.
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.