Page 1 of 1

php mysql update problem

Posted: Mon Feb 07, 2011 3:50 am
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
		}
?>

Re: php mysql update problem

Posted: Mon Feb 07, 2011 4:05 am
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'];

Re: php mysql update problem

Posted: Mon Feb 07, 2011 5:26 am
by weismana81
Right! I used the $row['user_name'] and that did it. Thanks so much for your help!!!

Re: php mysql update problem

Posted: Mon Feb 07, 2011 7:12 am
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

Re: php mysql update problem

Posted: Mon Feb 07, 2011 2:31 pm
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!