php mysql update problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
weismana81
Forum Newbie
Posts: 20
Joined: Mon Feb 07, 2011 3:36 am

php mysql update problem

Post by weismana81 »

I'm stuck. I've followed a couple tutorials on how to update a mysql database via php form, and I'm getting there, but I just can't seem to get past this. First let me say I'm sorry this is so basic, and I hope it's ok that I'm posting here. I'm not even to the point of getting "user_updated.php" working. Everything seems to ok, except the text box value is always " ". The user_id is passing (from the previous page) just fine, but the user_name isn't passing anything. This is what I have. Any help that anyone can offer is much appreciated!!!!

Code: Select all

<?php
$con =mysql_connect("localhost","myname","");
if (!$con)
	{die('could not connect: ' . mysql_error());
	}
	$user_id=$_GET['user_id'];
	mysql_select_db("test", $con);
	$result = mysql_query("SELECT * FROM sb_users WHERE user_id='$user_id'");
	while ($row = mysql_fetch_array($result))
		{
		echo $row['user_name'] . "";
		echo "<br/>";
		?>
        
        <form action="user_updated.php" method="post">
		<input type="hidden" name="user_id" value="<? echo $user_id; ?>">
		Name: <input type="text" name="user_name" value="<? echo $user_name; ?>"><br>
		<input type="Submit" value="Update">
		</form>
                
        <?php
		}
?>
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: php mysql update problem

Post by Domsore »

From what I can see the $user_name variable is not set any where. That's all I can see is wrong.

You need something like:

Code: Select all

$user_name = $_GET['user_name'];
weismana81
Forum Newbie
Posts: 20
Joined: Mon Feb 07, 2011 3:36 am

Re: php mysql update problem

Post by weismana81 »

Right! I used the $row['user_name'] and that did it. Thanks so much for your help!!!
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: php mysql update problem

Post by Domsore »

If you are using unique username you won't need the while loop and you could use something like:

Code: Select all

<?php
$con=mysql_connect("localhost","myname","");
if (!$con)

        {die('could not connect: ' .mysql_error());

        }

        $user_id=$_GET['user_id'];

        mysql_select_db("test", $con);

        $result = mysql_query("SELECT * FROM sb_users WHERE user_id='$user_id'");

        $row=mysql_fetch_array($result);

        $username = $row['user_name'];

        echo $username;

        echo '<br />';

                ?>

        

        <form action="user_updated.php" method="post">

                <input type="hidden" name="user_id" value="<? echo $user_id;?>">

                Name: <input type="text" name="user_name" value="<? echo $username; ?>"><br>

                <input type="Submit" value="Update">

                </form>

                

        <?php

                }
?>

 

Or put it into functions. Search php oop or something. Just some extra tips
weismana81
Forum Newbie
Posts: 20
Joined: Mon Feb 07, 2011 3:36 am

Re: php mysql update problem

Post by weismana81 »

Ok that makes complete sense. It's difficult following different tutorials. I'm getting some working php, but I only have a partial understanding of what I'm writing. I've done this before, but I've always used dreamweaver to build sites, then manipulated that code as needed. Anyway, thanks for the tip, it is super helpful!
Post Reply