www.webdeveloper.com
Results 1 to 3 of 3

Thread: Edit MySQL database using PHP

  1. #1
    Join Date
    Nov 2012
    Posts
    5

    Edit MySQL database using PHP

    Hello All,

    Thanks in advance for any support you guys can give me. I currently have an admin area of a site where I display records from the database using PHP and MySQL. The records display fine, and am not having issues with it, however I want to give other admins the ability to edit the records. I'm having issues with the edit.php file. It passes the variable from parents.php properly, but nothing is displayed in the actual edit for. I'm at a loss to see why it isn't working. Any help would be greatly appreciated.

    This is the edit link code from the parents.php file

    PHP Code:
    echo '<td><a href="edit3.php?id=' $row['parent_ID'] . '">Edit</a></td>'
    This is the link code from the edit.php file.

    PHP Code:
    <?php
     
    // connect to the database
            
    require_once('config.php');
            
    $db mysql_connect($dbhost$dbuser$dbpassword);
            @
    mysql_select_db($dbdatabase$db) or die("Unable to connect to MySQL");
            
            
    $id $_GET['parent_ID'];
            
    $results mysql_query("SELECT * FROM parents WHERE parent_ID = $id");
            
    $row mysql_fetch_assoc($results);
    /* 
     EDIT.PHP
     Allows user to edit specific entry in database
    */

     
    ?>
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
     <html>
     <head>
     <title>Edit Record</title>
     </head>
     <body>
     <form action="" method="post">
     <input type="hidden" name="parent_ID" value="<?php echo $id?>"/>
     <div>
     <p><strong>ID:</strong> <?php echo $id?></p>
     <strong>Mother:</strong> <input type="text" name="mother" value="<?php echo $mother?>"/><br/>
     <strong>Father:</strong> <input type="text" name="father" value="<?php echo $father?>"/><br/>
     <strong>Mother's Cell:</strong> <input type="text" name="momCell" value="<?php echo $momCell?>"/><br/>
     <strong>Father's Cell:</strong> <input type="text" name="dadCell" value="<?php echo $dadCell?>"/><br/>
     <strong>Mother's Email:</strong> <input type="text" name="momEmail" value="<?php echo $momEmail?>"/><br/>
     <strong>Father's Email:</strong> <input type="text" name="dadEmail" value="<?php echo $dadEmail?>"/><br/>
     <strong>Mother's Work #:</strong> <input type="text" name="momWork" value="<?php echo $momWork?>"/><br/>
     <strong>Father's Work #:</strong> <input type="text" name="dadWork" value="<?php echo $dadWork?>"/><br/>
     <strong>Address:</strong> <input type="text" name="address" value="<?php echo $address?>"/><br/>
     <strong>Apt:</strong> <input type="text" name="address 2" value="<?php echo $add2?>"/><br/>
     <strong>City:</strong> <input type="text" name="city" value="<?php echo $city?>"/><br/>
     <strong>State:</strong> <input type="text" name="state" value="<?php echo $state?>"/><br/>
     <strong>Zip Code:</strong> <input type="text" name="zipCode" value="<?php echo $zipCode?>"/><br/>
     <strong>Form Date:</strong> <input type="text" name="formDate" value="<?php echo $formDate?>"/><br/>
     <input type="submit" name="submit" value="Submit">
     </div>
     </form> 
     </body>
     </html>
    when I click on the edit link I do get the PHP variable passed on.

    PHP Code:
    edit.php?id=
    Yet nothing is displayed in the edit.php form like it should be. While were at it, how would I submit the record for update? I know I will have to use a mysql_query to do the updating, what I'm wondering is how to link the submit button to the UPDATE SQL query.

    Thanks!

  2. #2
    Join Date
    Mar 2009
    Posts
    452
    you are passing the value as id parameter but reading as parent_ID, different names thus no value.

    just change $_GET['parent_ID'] to $_GET['id']

    also you should perform some validation on the parameters before sending it to MySQL, otherwise chances of SQL injection.


    at least use $id = (int)$_GET['id']; so that the only integer (Number) is accepted

  3. #3
    Join Date
    Nov 2012
    Posts
    5
    I figured it out, it was a combination of what you said Zabi plus I was using

    <?php echo $variable; ?>

    Instead of

    <?php echo $row['variable']; ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles