Hey guys, me again.

OK, I have this form that gets and populates the select boxes with the necessary info.
Now, what I would like to do is to take the id field of the select box and use that to do a select query on the relevant table. So when the user selects, for eg a person, and then hits the edit person button I would like a new page to open and a form to be filled with the person's details from the database. Then the user should be able to edit that info and submit any changes to the database.

Here is the code that populates the initial select boxes to choose what to edit.

<form class="semantic" method="post" action="">
<fieldset>
<ul>
<li>
<legend>View and/or Edit Person</legend>
<?php
$ops = '';
$sql_select = "select person_id, first_name, middle_name, last_name from `person` order by `last_name` asc ";
$retval_selectperson = mysql_query( $sql_select, $conn );
if(! $retval_selectperson ) { die('Could not select data: ' . mysql_error()); }
while($row = mysql_fetch_assoc($retval_selectperson)) 
{
$ops .=  "<option value='{$row['person_id']}'>{$row['first_name']} {$row['middle_name']} {$row['last_name']}</option>";
}
?>
<div>
<label for="person_id">Select a Person</label>
<select name="person_id" id="person_id">
<option value="">--- Select a Person ---</option>
<?php echo $ops;?>
</select>
</div>
<div class="button-row">
<input name="edit" type="submit" id="edit_person" value="Edit Person">
</div>
</li>

So when the submit button is clicked, a new page opens and the record is displayed and then the editing of that record, if necessary, can take place and then if changed it can be updated to the database using an update query.

I need to know how to go to another page and actually do that.

Dani AI

Generated

A simple, reliable pattern fills the gap in this thread: post the selected person_id to a dedicated script, have that script SELECT the row and render a prefilled edit form, and then POST the updated fields back to the same script to run a prepared UPDATE. was right that the form can be sent to another page — use the form action to point at that edit script (opening a new window with a target is optional and not required).

Example server flow (concise):

  • The select form sends person_id (method="post") to edit_person.php.
  • edit_person.php checks for isset($_POST['edit']), validates person_id as an integer, runs a prepared SELECT, and prints a form with values escaped via htmlspecialchars() and a hidden person_id field.
  • When that form is submitted (isset($_POST['update'])), validate and sanitize each input, run a prepared UPDATE, then redirect (Post/Redirect/Get) or show a success message.

A minimal, safe PHP sketch is below (use your connection details and add full validation):

// edit_person.php (outline)
$mysqli = new mysqli('localhost','dbuser','dbpass','dbname');
if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['edit'])) {
  $id = filter_input(INPUT_POST,'person_id',FILTER_VALIDATE_INT);
  if (!$id) exit('Invalid selection');
  $stmt = $mysqli->prepare('SELECT first_name,middle_name,last_name,email FROM person WHERE person_id=? LIMIT 1');
  $stmt->bind_param('i',$id); $stmt->execute();
  $stmt->bind_result($first,$middle,$last,$email);
  if ($stmt->fetch()) {
    // echo a form with hidden person_id and fields prefilled with htmlspecialchars(...)
  }
  $stmt->close();
}
elseif ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['update'])) {
  // validate inputs, then:
  $stmt = $mysqli->prepare('UPDATE person SET first_name=?,middle_name=?,last_name=?,email=? WHERE person_id=?');
  $stmt->bind_param('ssssi',$first,$middle,$last,$email,$id);
  $stmt->execute();
  header('Location: edit_person.php?updated=1'); exit;
}

Checklist/troubleshooting

  • Make sure the select element has name="person_id" and the initial submit is named edit (so your script can detect it).
  • Escape values when outputting (htmlspecialchars) and always use prepared statements (mysqli or PDO) to avoid SQL injection.
  • Use server-side validation, CSRF tokens, and redirect-after-post to avoid duplicate submissions.
  • If nothing appears, confirm PHP errors are visible or log them, and verify field names match between forms and the processing script.

This fills the missing server-side steps so the chosen record is fetched, shown for editing, and safely updated back to the database.

Recommended Answers

All 7 Replies

The mysqli extensions will be used when the site goes live, so just concentrating on getting the code working locally first and will worry about pdo or mysqli when everything has been done and works.

Member Avatar for Member #120589

have a form element around the fields and set it to...

<form action="somefile.php" target="_blank" ...>

I think that should open the somefile.php in a new window and your data will be passed via GET (or POST if you specify that).

So each different area its own form? Can do that.

Member Avatar for Member #120589

Why each different area in its own form?

That's what I understood a form element to be?
Each button has it's own id. Is that what is to be used as a form element?

Member Avatar for Member #120589

Sorry, I missed the

<form class="semantic" method="post" action="">

at the top - just replace it with

<form class="semantic" method="post" action="" target="_blank">

but you may want to send the form to a different page? If so change the action

Will give that a go tomorrow. Cheers buddy!

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.