Page 1 of 1

problems using a form to UPDATE records

Posted: Thu Sep 02, 2010 7:07 am
by jbarner
hi, I am having a problem with a pair of scripts that are meant to allow an admin to update usernames and passwords on a database. I am relatively new to this, but am trying to really get into it, I enjoy the problem solving. I have changed every element I can think of.

the fist script is this:

Code: Select all

<?php
session_start();
include("dbinfo.inc.php");
mysql_connect($servname,$dbusername,$dbpassword);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM ($newdbname) ";
$result=mysql_query($query);
$num=mysql_numrows($result); 
mysql_close();

$i=0;
while ($i < $num) {
$user_id=mysql_result($result, $i, "user_id");
$username=mysql_result($result,$i,"username");
$password=mysql_result($result,$i,"password");
echo "$username, $password, $user_id";
?>
<form action="updated.php">
<input type="hidden" name="user_id" value="<?php echo "$user_id"; ?>">
Username: <input type="text" name="username" value="<?php echo "$username"?>"><br>
Password: <input type="text" name="password" value="<?php echo "$password"?>"><br>
<input type="Submit" value="Update">
</form>
<?php
++$i;
} 
?>
and the second is this:

Code: Select all

<?php
session_start();
include("dbinfo.inc.php");
mysql_connect($servname,$dbusername,$dbpassword);
if(isset($_GET['user_id']))
         {
         $ud_id = $_GET['user_id'];
         }
if(isset($_GET['username']))
         {
         $ud_username = $_GET['username'];
         }
if(isset($_GET['password']))
         {
         $ud_password = $_GET['password'];
         }
$query="UPDATE * FROM $newdbname SET username ='$ud_username', password= PASSWORD('$ud_password') WHERE id='$ud_id'";
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
printf ("Updated records: %d\n", mysql_affected_rows());
echo '</br><a href="update.php"> Click here to update more records</a>';
mysql_close();
?>

here is what i see happening:

in the first script,

database is connected to, and

$query is set as an action to pull all the info from the database
$result is set to actually pull the info using $query
$num is set as the total number of rows

a while loop is set to make sure all results are returned and filled into the form.

$user_id, $username, and $password are set as the corresponding values pulled form the database, and are displayed.

the form allows for those values to be changed, and passes the values via the submit button to the next script.

in the second script,

database is connected to, and

the values of $ud_id, $ud_username, and $ud_password are set to the values returned by the GET action.

$query is set to UPDATE the values in the table WHERE the id equals the value returned by that GET action ($ud_id)

database is connected to and $query is run

the number of affected rows is displayed, along with a link to return and change more records.
what am I missing? Is it just syntax? I'm not getting any errors, and I've already changed everything I can think of.


help. me. please.

Re: problems using a form to UPDATE records

Posted: Thu Sep 02, 2010 8:48 am
by mikosiko
you didn't say what exactly your problem is... but assuming that your UPDATE is not effectively updating the record (second script) I have the following comments:

- In your $query definition... what is the value of $newdbname ? do you have that variable defined in your dbinfo.inc.php file?
- Suppress the "@" before your @mysql_select_db if there are an error you are suppressing the display of it.
- write the line mysql_query($query) at least in this way

Code: Select all

mysql_query($query) or die("Query Error: " . mysql_error());
that will show that you have an error in your UPDATE sentence

Code: Select all

UPDATE * FROM table SET ....
is not a valid syntax for the UPDATE clause
http://dev.mysql.com/doc/refman/5.0/en/update.html