SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help with PHP/MySQL Update Page

    I checked around and read other posts, made changes to the code, checked out multiple examples, but nothing seems to help.

    Here is the deal, my update page for my php site to update a single record after selecting from the select page displays everything on the update page just fine for the record, so I change the text a bit and everything works fine. The thing is when I go back to view the record it doesn't actually update it. Any help would be appreciated. Thanks in advance.

    Code:
    <?
    
    // Make a MySQL Connection
    mysql_connect("localhost", "user", "pass") or die(mysql_error());
    mysql_select_db("db1") or die(mysql_error());
    
    // ***** This part will process when you Click on "Submit" button ***** 
    // Check, if you clicked "Submit" button 
    if($_POST['Submit']){
    
    // Get parameters from form. 
    $id=$_POST['id'];
    $name=$_POST['name'];
    $phone=$_POST['phone'];
    $note=$_POST['note'];
    
    // Do update statement. 
    mysql_query("update db1 set name='$name', phone='$phone', note='$note' where id='$id'");
    
    echo "Record updated!";
    echo "<BR>";
    echo "Back to main page (can't post links yet in sitepoint forums, so removed html code tags and links)";
    exit;
    }
    // ************* End update part *************
    
    // *** Select data to show on text fields in form. ***
    
    // Get id parameter (GET method) from select.php 
    $id=$_GET['id'];
    
    
    // Get records in all columns from table where column id equal in $id and put it in $result.
    $result=mysql_query("select * from db1 where id='$id'");
    
    // Split records in $result by table rows and put them in $row. 
    $row=mysql_fetch_assoc($result);
    
    
    // Close database connection. 
    mysql_close();
    ?>
    
    
    <form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
    <table  border="0" align="center" cellpadding="0" cellspacing="1">
    <tr>
    <td >Name: </td>
    <td ><input name="name" type="text" id="name" value="<? echo $row['name']; ?>"/></td>
    </tr>
    <tr>
    <td>Phone: </td>
    <td><input name="phone" type="text" id="phone" value="<? echo $row['phone']; ?>"/></td>
    </tr>
    <tr>
    <td>Note: </td>
    <td><input name="note" type="text" id="note" value="<? echo $row['note']; ?>"/></td>
    </tr>
    <tr>
    <td align="center"></td>
    <td align="center"><input style="cursor: pointer;" type="submit" name="Submit" value="Submit" /></td>
    </tr></table></form>

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your form does not contain an input named "id", so $_POST['id'] is empty and can't be used to identify which row to update.

    Add a hidden input to your form which contains the id of the selected post.

    You'll also need to run all the input through mysql_real_escape_string() or the query will break as soon as someone types a single quote mark in any of the text.

    You'll also want to remove the single quotes around the value for the id column in the query if this is a numeric column. Only strings get quoted.

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Awesome thanks Dan, that worked

  4. #4
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    trying to figure out how to mark as resolved, lol

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    SitePoint doesn't have a feature like that, the post suffices


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •