SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    May 2003
    Location
    holland
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to update mysql table

    I found a little script form helg but i have a problem:
    when i hit submit nothing hapens.. i get redirected to the previos page..
    Can someone help me with this problem? Here's the code:

    tnx

    btw im sure my database is correct
    Code:
    <HTML>
    <HEAD>
    <TITLE>Update user</TITLE>
    </HEAD>
    <BODY>
    
    <?
    //include ("login.php");
    include ("include/db.php");
    $server = "$host"; 
    $mysql_user = "$user"; 
    $mysql_password = "$pass"; 
    $database = "$db"; 
    
    $userid = $_GET['userid']; // assuming you're sending the userid in the adress. Like: updateuser.php?id=23 
    
    
    // Connecting to mysql 
    $link = mysql_connect($server, $mysql_user, $mysql_password) 
        or die("Could not connect to mysql!<br />mysql error: " . mysql_error()); 
    
    // Selecting databse 
    mysql_select_db ($database, $link) 
        or die("Could not select db!<br />mysql error: " . mysql_error()); 
    
    // The query. You need to change this to reflect your tablename and columnames. 
    $sql = "SELECT userid, password, webspace, userlevel FROM user WHERE userid=$userid"; 
    
    //Retrieving data from database 
    $result = mysql_query($sql) 
        or die ("Invalid query!<br />mysql error: " . mysql_error()); 
    
    if(empty($result)) 
    { 
        echo "Didn't find any user with that userid!"; 
    } elseif(mysql_num_rows($result)>1) { 
        echo "Duplicate users with that userid!"; 
    } else { 
        // Userlevels 
        $levels = array(1=>'Basic', 'Normal', 'Extra', 'Reseller', 'Admin'); 
    
        // Printing out the form 
        echo "<form method=\"post\" action=\"updateuser.php?userid=test\">\n"; 
        echo "<table>\n"; 
    
        // Fetching the result into an associative array   
        $row = mysql_fetch_array($result); 
        echo "<tr><td>username</td><td>$userid</td></tr>\n"; 
        echo "<tr><td>password</td><td><input type=\"text\" name=\"password\" value=\"" . $row['password'] . "\"></td></tr>\n"; 
        echo "<tr><td><select size=\"1\" name=\"D1\">\n"; 
        foreach ($levels as $key => $value) 
        { 
            if ($key == $row['userlevel']) { 
                echo "<option selected=\"selected\" value=\"$key\">$value</option>\n"; 
            } else { 
                echo "<option value=\"$key\">$value</option>\n"; 
            } 
        } 
        echo "</td></tr>\n"; 
        echo "<tr><td>\n<input type=\"submit\" name=\"update\" value=\"update\"></td></tr>\n\n"; 
        echo "</form>\n"; 
    }
    
    
    ?>

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the name of that page? i.e what have you called it?
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, there is nothing on that page to perform any sort of update anyway! What is it supposed to do?
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  4. #4
    SitePoint Member
    Join Date
    May 2003
    Location
    holland
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    eh the page is called updateuser.php and it's supose to update an password from a table in mysql.. but it does nothing
    i allready thougt there was no mysql update.. but does anybody know the exact code?

  5. #5
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you'll need to do something like:

    if(isset($_POST["submit"]))

    to see whether or not they have submitted the form. If they have you can do some error checking an update. If it hasn't then it just loads the form.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  6. #6
    SitePoint Member
    Join Date
    May 2003
    Location
    holland
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think it's trying to submit the detail but it can not send it to the mysql.. How can i update in the db?

  7. #7
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, here's the logic behind it:

    1) The page loads. It just the page with the update form. The user fills in the new details and posts this data (by submitting the form) to itself by reloading the page.

    2) So, we have the page loading again. Unless you check to see whether or not the form was submitted, then you're just gonna keep going round in circles. Therefore in that same page you need to check if the form was submitted, using my advice above.

    So it might look like this:

    PHP Code:
    <HTML>
    <HEAD>
    <TITLE>Update user</TITLE>
    </HEAD>
    <BODY>

    <?
    //include ("login.php" );
    include ("include/db.php" );
    $server "$host"
    $mysql_user "$user"
    $mysql_password "$pass"
    $database "$db"

    $userid $_GET['userid']; // assuming you're sending the userid in the adress. Like: updateuser.php?id=23 


    // Connecting to mysql 
    $link mysql_connect($server$mysql_user$mysql_password
        or die(
    "Could not connect to mysql!<br />mysql error: " mysql_error()); 

    // Selecting databse 
    mysql_select_db ($database$link
        or die(
    "Could not select db!<br />mysql error: " mysql_error()); 

    // we check to see if the form got submitted by seeing if the value of UPDATE (the name of the button) is set

    if(isset($_POST["update"])) {

    // yes it is, therefore do an update

    $update=mysql_query("UPDATE user SET password='$_POST[password]' WHERE userid = $userid");

    if (!
    $update) { echo "Couldn't update" mysql_error(); } else { echo "Update successful!"; }

    } else { 
    // the form hasn't yet been submitted, so we need to display the form

    // The query. You need to change this to reflect your tablename and columnames. 
    $sql "SELECT userid, password, webspace, userlevel FROM user WHERE userid=$userid"

    //Retrieving data from database 
    $result mysql_query($sql
        or die (
    "Invalid query!<br />mysql error: " mysql_error()); 

    if(empty(
    $result)) 

        echo 
    "Didn't find any user with that userid!"
    } elseif(
    mysql_num_rows($result)>1) { 
        echo 
    "Duplicate users with that userid!"
    } else { 
        
    // Userlevels 
        
    $levels = array(1=>'Basic''Normal''Extra''Reseller''Admin'); 

        
    // Printing out the form 
        
    echo "<form method=\"post\" action=\"updateuser.php?userid=test\">\n"
        echo 
    "<table>\n"

        
    // Fetching the result into an associative array   
        
    $row mysql_fetch_array($result); 
        echo 
    "<tr><td>username</td><td>$userid</td></tr>\n"
        echo 
    "<tr><td>password</td><td><input type=\"text\" name=\"password\" value=\"" $row['password'] . "\"></td></tr>\n"
        echo 
    "<tr><td><select size=\"1\" name=\"D1\">\n"
        foreach (
    $levels as $key => $value
        { 
            if (
    $key == $row['userlevel']) { 
                echo 
    "<option selected=\"selected\" value=\"$key\">$value</option>\n"
            } else { 
                echo 
    "<option value=\"$key\">$value</option>\n"
            } 
        } 
        echo 
    "</td></tr>\n"
        echo 
    "<tr><td>\n<input type=\"submit\" name=\"update\" value=\"update\"></td></tr>\n\n"
        echo 
    "</form>\n"
    }
    }


    ?>
    Those are the basics. No idea if that will work because I just did it quickly. You will have to change your update to allow whatever fields to be changed. Also don't forget you may have problems if they are trying to change their password/username etc. if you are using sessions.

    Matt
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  8. #8
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well?
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  9. #9
    SitePoint Member
    Join Date
    May 2003
    Location
    holland
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry i haven't yet replied... but it didn't work..
    maybe i can explain my situation: I have made an admin script that reads out the table user and i wanna make an update script that is for me as admin. So i let admin.php sent out the username to update user.. then i try your version of the updateuser.php.. but still if i go back to the admin and i refresh it won't update the password....
    I am sure my setting's for this script are correct..

    But i had one strange thing when i tryed the orginal version of this script it said it needed to have a table called test.. so i created one but i couldn't find some clue to test so maybe this is gone wrong

  10. #10
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Test will come from the form - if you notice in the script the form action is updateuser.php?userid=test

    I'm now confused as to what it is you are doing, your description is a little confusing.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  11. #11
    SitePoint Member
    Join Date
    May 2003
    Location
    holland
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    eh...

    how can i explain?

    I have created an admin.php there for i made a script that reads out the table: user
    and the rows: userid, password etc etc

    i put all data in a form there is a link that sents updateuser.php?userid=username

    then there are some update fields like password, webspace, userlevel.
    There is a submit button and like you gave me the code it posted the things in mysql (altho it suppost to do)

    but then i go back to admin.php where i refresh my browser and look again at the user that i update with updateuser.php.. but nothing has changed...

    tnx

  12. #12
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK. As I said in my very post there is nothing (that I can see) in updateuser which actually updates anything - which was sort of the point of my modifications.

    PHP Code:
    <HTML> 
    <HEAD> 
    <TITLE>Update user</TITLE> 
    </HEAD> 
    <BODY> 

    <? 
    //include ("login.php" ); 
    include ("include/db.php" ); 
    $server "$host"
    $mysql_user "$user"
    $mysql_password "$pass"
    $database "$db"

    $userid $_GET['userid']; // assuming you're sending the userid in the adress. Like: updateuser.php?id=23 


    // Connecting to mysql 
    $link mysql_connect($server$mysql_user$mysql_password
        or die(
    "Could not connect to mysql!<br />mysql error: " mysql_error()); 

    // Selecting databse 
    mysql_select_db ($database$link
        or die(
    "Could not select db!<br />mysql error: " mysql_error()); 

    // we check to see if the form got submitted by seeing if the value of UPDATE (the name of the button) is set 

    if(isset($_POST["update"])) { 

    // yes it is, therefore do an update 

    $update=mysql_query("UPDATE user SET password='$_POST[password]' WHERE userid = $userid); 

    if (!
    $update) { echo "Couldn't update" mysql_error(); } else { echo "Update successful!"; } 

    } else { 
    // the form hasn't yet been submitted, so we need to display the form 

    // The query. You need to change this to reflect your tablename and columnames. 
    $sql "SELECT userid, password, webspace, userlevel FROM user WHERE userid=$userid"

    //Retrieving data from database 
    $result mysql_query($sql
        or die (
    "Invalid query!<br />mysql error: " mysql_error()); 

    if(empty(
    $result)) 

        echo 
    "Didn't find any user with that userid!"
    } elseif(
    mysql_num_rows($result)>1) { 
        echo 
    "Duplicate users with that userid!"
    } else { 
        
    // Userlevels 
        
    $levels = array(1=>'Basic''Normal''Extra''Reseller''Admin'); 

        
    // Printing out the form 
        
    echo "<form method=\"post\" action=\"updateuser.php?userid=$userid\">\n"
        echo 
    "<table>\n"

        
    // Fetching the result into an associative array    
        
    $row mysql_fetch_array($result); 
        echo 
    "<tr><td>username</td><td>$userid</td></tr>\n"
        echo 
    "<tr><td>password</td><td><input type=\"text\" name=\"password\" value=\"" $row['password'] . "\"></td></tr>\n"
        echo 
    "<tr><td><select size=\"1\" name=\"D1\">\n"
        foreach (
    $levels as $key => $value
        { 
            if (
    $key == $row['userlevel']) { 
                echo 
    "<option selected=\"selected\" value=\"$key\">$value</option>\n"
            } else { 
                echo 
    "<option value=\"$key\">$value</option>\n"
            } 
        } 
        echo 
    "</td></tr>\n"
        echo 
    "<tr><td>\n<input type=\"submit\" name=\"update\" value=\"update\"></td></tr>\n\n"
        echo 
    "</form>\n"




    ?>
    Try that, and tell me if you get any error messages.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky


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
  •