Edit Records From Mysql Table

Discussion in 'PHP' started by rhoula, Jan 29, 2013.

  1. #1
    I have a Database "Checkthisup" a table "Users" I want to be able to change the records from the table by searching by user id. If you have the code to do this please help me.

    I want to enter the row id and then have all the information posted in a form so I can change it then send it back to the database.

    These are the fields of the database:
    id, name, phone, page, dob, email.

    Thank you in advance :)
     
    rhoula, Jan 29, 2013 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    You want the code for the whole site here, or the SQL code or what?
     
    Rukbat, Jan 29, 2013 IP
  3. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #3
    The code to fetch the data and put in a form to be edited so it could be uploaded back to the database.

    Thank you
     
    rhoula, Jan 29, 2013 IP
  4. Tom-Brown

    Tom-Brown Well-Known Member

    Messages:
    105
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #4
    In PHP you'll need to create the connection via mysql_connect() and mysql_select_db().

    Then if you want to update fields you'll be required to update via queries. Such as,
    mysql_query("UPDATE database SET field1=value, field2=value, field3=value WHERE idfield=idtoupdate")

    To insert
    mysql_query("INSERT INTO database (field1, field2, field3, field3) VALUES (value1, value2, value3, value4)")

    To select
    mysql_query("SELECT field1, field2, field3 FROM database WHERE idfield=idtoselect")

    All pretty easy, (These should be correct but it's been a while so tell me if there is a mistake!)
     
    Tom-Brown, Jan 29, 2013 IP
  5. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #5
    can all this be made into a couple files or do i need to have more than 2 files?

    I was thinking about a first file (ex: get_id.php) where the user will be asked to enter the id of the row and clicks submit he will be taken to file 2 (ex:update.php) in this file there will be a form where all information from that row will be placed. The after he modifies the fields that need to be modified he submits the form that will update the database.

    The problem I m having now is that I can build the html part of the script, but I don't have no idea how to get the information inside the php form.

    If you could please help me, I would be very grateful.

    Thank you
     
    rhoula, Jan 29, 2013 IP
  6. Dangy

    Dangy Well-Known Member

    Messages:
    841
    Likes Received:
    25
    Best Answers:
    2
    Trophy Points:
    155
    #6
    
     
    <?
    function connect(){
    $host = "localhost";
    $username = "database_username";
    $pass = "passhere";
    $db_name = "database_name";
    mysql_connect("$host", "$username", "$pass")or die("cannot connect");
    mysql_select_db($db_name)or die(mysql_error());  
    }
    connect();
     
    $user = $_GET['user']; // Gets Username From Url
    $query="select * from User where user='$user' limit 1"; 
    $result = mysql_query($query);
    $row = mysql_fetch_object($result);
     
    //Display Fetched Database Below
     
    echo $row->id;
    echo $row->name;
    echo $row->phone;
    echo $row->page;
    echo $row->dob;
    echo $row->email;
     
    /* This is example how to display the information in a form outside of PHP
    <form>
    <input type="text" class="text" name="name" value="<?=$row->name;?>"/>
    </form>
    */
     
    //How to change the information that is there.
     
    mysql_query("UPDATE `User` SET name='$name', phone='$phone' WHERE user = '$user'");
    ?>
     
    
    PHP:
    That should give you everything you need.
     
    Dangy, Jan 29, 2013 IP
  7. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #7
    for some reason it giving me a blank page. Am I supposed to have a form that calls this file?
     
    rhoula, Jan 29, 2013 IP
  8. phpwreker

    phpwreker Greenhorn

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    do you want to edit it on your localhost .. or a remote host from a webpage ? :)
     
    phpwreker, Jan 30, 2013 IP
  9. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #9
    Using a form from a webpage.
     
    rhoula, Jan 30, 2013 IP
  10. AfterHoursProgramming

    AfterHoursProgramming Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #10
    What year is this! Don't use mysql_query. Use the PDO class.
     
    AfterHoursProgramming, Jan 30, 2013 IP
  11. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #11

    .. or mysqli. Whatever you prefer for your coding style (or meets your database needs).
     
    ryan_uk, Jan 30, 2013 IP
  12. AfterHoursProgramming

    AfterHoursProgramming Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #12
    ryan_uk Good point and very true. I just have a preference for PDO's class structure.
     
    AfterHoursProgramming, Feb 2, 2013 IP
  13. Blaxus

    Blaxus Active Member

    Messages:
    23
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    78
    #13
    <?php
    # Connect to MySQL - MYSQL_HOST | MYSQL_USERNAME | MYSQL_PASSWORD
    mysql_connect('localhost','root','root')or die(mysql_error());
     
    # Connect to Database - Checkthisup
    mysql_select_db('Checkthisup')or die(mysql_error());
     
    # Check to see if the button has been clicked
    if(isset($_POST['submit']))
    {
       # Send Update Query to Database
       mysql_query("UPDATE users SET username = '".mysql_real_escape_string($_POST['username'])."' WHERE id = '".mysql_real_escape_string($_POST['id'])."'")or die(mysql_error());
        
        # Print out success
        echo 'User has been updated.';
    }
    else
    {
       # Print out HTML form
       echo '<form method="POST" action="file_name_here.php">
           
        <!-- user id you wanted -->
        User Id: <input type="text" name="id"><br />
        
        <!-- user name as example data, name can be accessed from $_POST['username'] -->
        Username: <input type="text" name="username"><br />
        
        <input type="button" name="submit" value="Update">
        
        </form>';
    }
    
    PHP:
     
    Last edited: Feb 3, 2013
    Blaxus, Feb 3, 2013 IP