1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

help me with update records in mysql database

Discussion in 'Databases' started by tyrone99, Dec 3, 2014.

  1. #1
    I have a problem .
    I 've been trying for a long time to make an update for php mysql to change the data.
    but every time I do not manage to make it work with a form.
    but it works if I only if I put this ($ sql = "UPDATE users SET username = 'value' WHERE id = 10 " ; )
    so it only works when I put the value of the id.

    but I want in an html form to indicate what I want to change and what id goes.

    but I have tried so long that I do not feel like I so want someone help me.

    make the same database and same as my records and make the code and test it if it works show me please



    my database name : web test
    my table called : users
    my records are called :

    id int ( 11) AUTO_INNCREMENT
    username , varchar ( 255 )
    password , varchar ( 255 )
    first_name , varchar ( 255 )
    last_name , varchar ( 255 )
    email, varchar ( 255 )
    Age, int ( 11)

    please help me out!!!
     
    tyrone99, Dec 3, 2014 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    While you can do a global change by leaving out the id it is normal to have something to identify the records that need to be changed.

    Can you give a scenario to help us understand what you are trying to do? It will help us to understand the problems you are encountering.
     
    sarahk, Dec 3, 2014 IP
  3. tyrone99

    tyrone99 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    okay
    Look, my update.php is like this now
    <?php
    $servername = "localhost";
    $username = "root";
    $password = ".....";
    $dbname = "webtest";
    
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "UPDATE users SET password='cotton candy' WHERE id=10";
    
    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
    
    $conn->close();
    ?>
    PHP:

    but now i have still have to go into the php file to change the valeu or the id
    but i looked on site and youtube how to put it in a simple html form
    but it still does not work.

    i want it in a html from.
    I want that when I enter the ID that the data of the user appears and that I can change any valeu separately.

    is it more clear?
     
    Last edited by a moderator: Dec 3, 2014
    tyrone99, Dec 3, 2014 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    You need to go back to basics and look at some really simple form samples.

    You need to have fields for the id and the password, then you need to get them from the $_POST variable and build them into the sql statement.

    I'm loathe to give you an actual example because there are lots of security issues with those particular calls. Passwords should always be encrypted before they are saved but that means you also need to change your login routine to encrypt the password that the user gives before it is compared to the one in the database.

    You need to protect against sql injection and you need to make sure that only authorised people can see the page - you don't want Google serving it up in the results.
     
    sarahk, Dec 3, 2014 IP
  5. Naina S

    Naina S Active Member

    Messages:
    203
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    93
    #5
    Please explain what exactly you are trying to do. If you want to capture the value of input field and then tranfering it to the database query you should send the value of input filed to the query like this.
    update(Convert.ToInt32(rowIdTextbox.Text), nameTextBox.Text);

    public void update(int rowId, string name)
    {
    call update query procedure.
    }
     
    Naina S, Dec 4, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    You'll need to have an input for each of the values you want to change. Let's say you want to change the password for a user, then the form would look something like this:
    
    <form method="post" action="#">
      <input type="text" name="change_password" value="" placeholder="input new password">
      <input type="text" name="user_id" value="" placeholder="input user id"> // however, this should probably be a select-box, gotten from the already existing users
      <input type="submit" name="submit_changepass" value="Change password">
    </form>
    
    Code (markup):
    and in the PHP-part, you'd have something like this:
    
    $changepass = (isset($_POST['submit_changepass']) && !empty($_POST['change_password'])) ? $_POST['change_password'] : '';
    $userid = (isset($_POST['submit_changepass']) && !empty($_POST['user_id'])) ? $_POST['user_id'] : '';
    
    if (!empty($userid) && !empty($changepass)) {
    $sql = "UPDATE users SET password = '$changepass' WHERE user_id = '$userid'";
    }
    
    PHP:
    Be aware that this is VERY simplified, and not proper code AT ALL - the form lacks labels and other stuff, the PHP-part doesn't do ANY kind of cleaning of the input, and this whole thing is just a basic setup to show you how to set up the update - however, this should definitely be secured, and preferably also done via a prepared query (I saw you're using mysqli_ so that should be fairly easy to manage).
     
    PoPSiCLe, Dec 4, 2014 IP