?id=$id = carrying forward id from sql table

Discussion in 'PHP' started by mika17, Oct 1, 2008.

  1. #1
    Hi - am following a tutorial on php and mysql and have run into a brick wall.

    I have a simple contact database and the index.php outputs all the entries in the database - as follows:

    <?
    include("dbinfo.inc.php");
    
    $query="SELECT * FROM contacts";
    $result=mysql_query($query);
    
    $num=mysql_numrows($result); 
    
    mysql_close();
    
    echo "<b><center>Database Output</center></b><br><br>";
    
    ?>
    <table border="0" cellspacing="2" cellpadding="2">
    <tr> 
    <th><font face="Arial, Helvetica, sans-serif">id</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Name</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Phone</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Mobile</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Fax</font></th>
    <th><font face="Arial, Helvetica, sans-serif">E-mail</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Website</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Update</font></th>
    </tr>
    
    <?
    $i=0;
    while ($i < $num) {
    $id=mysql_result($result,$i,"id");
    $first=mysql_result($result,$i,"first");
    $last=mysql_result($result,$i,"last");
    $phone=mysql_result($result,$i,"phone");
    $mobile=mysql_result($result,$i,"mobile");
    $fax=mysql_result($result,$i,"fax");
    $email=mysql_result($result,$i,"email");
    $web=mysql_result($result,$i,"web"); 
    ?>
    
    
    <tr> 
    <td><font face="Arial, Helvetica, sans-serif"><? echo "$id"; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo "$first $last"; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo "$phone"; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo "$mobile"; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo "$fax"; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo "$email"; ?>">E-mail</a></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><a href="<? echo "$web"; ?>">Website</a></font></td>
    <td><font face="Arial, Helvetica, sans-serif">THIS LINK HERE I CANNOT WORK OUT  Update</a>" ?></font></td>
    </tr>
    <?
    ++$i;
    } 
    echo "</table>";
    ?>
    
    PHP:
    This works well and outputs the contacts in the database. BUT, on Line 48 where I have typed in CAPS is supposed to be a link to update.php where details of the entry can be changed. update.php looks like this:

    <?
    include("dbinfo.inc.php");
    
    $query="SELECT * FROM contacts WHERE id='$id'";
    $result=mysql_query($query);
    $num=mysql_numrows($result); 
    mysql_close();
    
    $i=0;
    while ($i < $num) {
    $first=mysql_result($result,$i,"first");
    $last=mysql_result($result,$i,"last");
    $phone=mysql_result($result,$i,"phone");
    $mobile=mysql_result($result,$i,"mobile");
    $fax=mysql_result($result,$i,"fax");
    $email=mysql_result($result,$i,"email");
    $web=mysql_result($result,$i,"web");
    
    ?>
    
    <form action="updated.php">
    <input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
    First Name: <input type="text" name="ud_first" value="<? echo "$first"?>"><br>
    Last Name: <input type="text" name="ud_last" value="<? echo "$last"?>"><br>
    Phone Number: <input type="text" name="ud_phone" value="<? echo "$phone"?>"><br>
    Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br>
    Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br>
    E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br>
    Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br>
    <input type="Submit" value="Update">
    </form>
    
    <?
    ++$i;
    } 
    ?>
    PHP:
    I cannot figure out what to type so that update.php loads and fetches the data associated with the id (and details of) the appropriate entry in the database.

    I am sure it is something to do with the following:
    ?id=$id
    and this little line:
    $id=$_GET['id'];

    I added the id column to the table, but cannot get update.php to load so that the line in the address bar has the item id in it and all the information is pre-entered in the form.

    I'm sure this is simple but after hours of tooling around I'm going nowhere. Any help greatly appreciated.
     
    mika17, Oct 1, 2008 IP
  2. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Well on the index page the link needs to look something like this:

    
    <a href="update.php?id=<?php echo $id; ?>">Update</a>
    
    Code (markup):
    Then on the update page you first get the id your passing from the url:

    
    $contact_id = intval($_GET['id']);
    $query="SELECT * FROM contacts WHERE id=$contact_id";
    //rest of your code
    
    Code (markup):
    I use intval to make sure the id being passed is a number, cleaning the variable basically before using it in my sql statement.
     
    Sillysoft, Oct 1, 2008 IP
  3. mika17

    mika17 Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks a million Sillysoft.

    I had just figured out:

    <a href='update.php?id=$id'>Update </a>"

    Which worked. I like your intval in the second part and have put that in.

    Thanks a lot.
     
    mika17, Oct 1, 2008 IP