0

I am updating an entry into my database on a specific page. I have a table for studies and a table for authors. The studies table just shows a number in a field to indicate the id or the author who wrote it.

When editing the number shows up for the author ($row[4]), but when I refresh the page, the name of the author shows up. I want the name to show up and not the number. How can I make the name show up the first time?

By the way, the dropdown itself populates the names.

Here is the code I have for this section.

Code in header

<?php
if(isset($_GET['edit']))
{
$id=$_GET['edit'];
$response=mysql_query("SELECT * FROM studies WHERE id='$id'");
$row=mysql_fetch_array($response);
}
?>

Code in the HTML body

<select name="new_estudo_author">
    <option value="<? echo $row[4]?>"><?php echo $row[4]?>...</option>
<?
    $authorlist=mysql_query("select * from authors order by name asc");
    while($row_authorlist=mysql_fetch_assoc($authorlist)){
?>
    <option value="<?echo $row_authorlist['id']?>"><?echo $row_authorlist['name'];?></option>
<?
    }
?>
</select>
  • 2
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Oct 25 '13 at 18:59
  • Thank for the tip and all the links. I will try and fix that. I am just starting on php so there is much I am sure I am doing incorrectly. – Jeremy Tyler Oct 25 '13 at 19:05
  • Just invert the order – Mihai Oct 25 '13 at 19:06
  • what does your $row contain when $_GET['edit'] is not set? – rakeshjain Oct 25 '13 at 19:08
  • That section is actually working correctly. The problem is echo $row[4]?>"> which gives me the id, but on refresh gives me the actual author name. – Jeremy Tyler Oct 25 '13 at 19:10
  • $_GET['edit'] comes from another page which pulls up the row. If I remove that, then I just get the dropdown without the value in the database. – Jeremy Tyler Oct 25 '13 at 19:13

0 Answers0