Jump to content

Update Script...


tobimichigan

Recommended Posts

Please could some1 just point out why this php code is not updating the my-sql db? It displays the content of the db but does not update accordingly..

<?php
include("cn.php");
//$username=$_GET['username'];
if (!@$_SESSION['username']){
header("location:Not_Logged.php"); }



if (isset($_POST['update'])) {
$update=$_GET['update'];}
else {$update=0;}	

if ($update = "1") {
$username=$_GET['username'];
$id=$_GET['id'];
    
$username=$_POST['username'];
$password=$_POST['password'];

$select="select * from users where username='$_SESSION[username]'";
$rs_cat=mysqli_query($con,$select);

$row=mysqli_fetch_array($rs_cat);
$username=$row['username'];
$password=$row['password'];

$update="UPDATE users SET username='$username', password='$password' where id='$id'";

$ur=mysqli_query($con,$update) or die (mysql_error());

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UPDATE</title>
</head>

<body>
<?php //echo $_SESSION['username'];?>
<table align="center" >
<form name="login" action=""  method="post"/>
<tr>
<td>

<th scope="col"><strong>UPDATE</strong></th>
</td>
</tr>
<tr>
<td>
USERNAME
<th scope="col"><input name="username" type="text"  value="<?php echo ("$row[username]");?>"/></th>
</td>
</tr>
<tr>
<td>
PASSWORD
<th scope="col"><input name="password" type="password" value="<?php echo ("$row[password]");?>" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="update" type="submit" value="UPDATE" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="id" type="hidden"  value="<?php echo $row[id];?>"  /></th>
</td>
</form>
</table>
</body>
</html>

 

Any sharp locator would be appreciated...

Link to comment
Share on other sites

see comments added...

 

$username=$_GET['username'];
$id=$_GET['id'];
/* 
At this point you overwrite the variables you used GET with the variables from POST - WHY?
*/
$username=$_POST['username'];
$password=$_POST['password'];
$select="select * from users where username='$_SESSION[username]'";
$rs_cat=mysqli_query($con,$select);
$row=mysqli_fetch_array($rs_cat);
/*
At this point you overwrite the SUPPOSED new values with the values ALREADY in the db
WHY?
*/

$username=$row['username'];
$password=$row['password'];

 

The net result is you are simply "UPDATING" using the values already in the db - hence no chnage

Link to comment
Share on other sites

you start by assigning a value to $username via get

then you immediately use POST to assign a value to the exact same variable

THEN you using NEITHER of those values you query the db using a session username value

which you then assign to the $username variable

At this point the variable $username has the value that is already in the database, so even though the 'mechanics' of updating are happening, it is not visible to the 'naked eye'

 

example

$username is assign the GET value (pretend it is 'JOE')

at this point $username is JOE

 

then you assign the post value to $username (pretend POST value is 'MIKE')

$username now equals MIKE

 

THEN you query the database for the username (pretend value in table is 'FRED') and assign it to $username

 

At this point $username = FRED

 

Sooooo when you update the table you are puting FRED in the field rather that the new name

 

Make sense?

then you take and assign the

Link to comment
Share on other sites

Lite, thanks for your responses I'm so grateful...I have made some of your corrections but the db is not updating yet but still reflecting the old values after clicking the old values r still there...what am I doing wrong now?

<?php
include("cn.php");
//$username=$_GET['username'];
if (!@$_SESSION['username']){
header("location:Not_Logged.php"); }

$select="select * from users where username='$_SESSION[username]'";
$rs_cat=mysqli_query($con,$select);

$row=mysqli_fetch_array($rs_cat);
$username=$row['username'];
$password=$row['password'];

if (isset($_POST['update'])) {
$update=$_GET['update'];}
else {$update=0;}	

if ($update = "1") {
//this is where I implemented your corrections...
$username=$_GET['username'];
$username=$_POST['username'];

$password=$_GET['password'];
$password=$_POST['password'];  	

$update="UPDATE users SET username='$username', password='$password' where username='$_POST[username]'";

$ur=mysqli_query($con,$update) or die (mysql_error());

}
//}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UPDATE</title>
</head>

<body>
<?php //echo $_SESSION['username'];?>
<table align="center" >
<form name="login" action=""  method="post"/>
<tr>
<td>

<th scope="col"><strong>UPDATE</strong></th>
</td>
</tr>
<tr>
<td>
USERNAME
<th scope="col"><input name="username" type="text"  value="<?php echo ("$row[username]");?>"/></th>
</td>
</tr>
<tr>
<td>
PASSWORD
<th scope="col"><input name="password" type="password" value="<?php echo ("$row[password]");?>" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="update" type="submit" value="UPDATE" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="id" type="hidden"  value="<?php echo $row[id];?>"  /></th>
</td>
</form>
</table>
</body>
</html>

where is the missing link now?

Link to comment
Share on other sites

Ok Lite, here's the code that loads the update page:

 

<?php
include("cn.php");
//$username=$_GET['username'];
if (!@$_SESSION['username']){
header("location:Not_Logged.php"); }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcomee</title>
</head>

<body>
Welcome <?php echo ("$_SESSION[username]");?>

<p><li><?php echo ("<a href='Logout.php?user=$_SESSION[username]&sessid=$sessid'>Logout</a>");?></li>
<li><?php echo ("<a href='update.php?user=$_SESSION[username]&sessid=$sessid'>Update</a>");?></li>
</body>
</html>

 

As per the session variable, it is in cn.php, here's it:

 

<?php
session_start();
$sessid=md5(uniqid(rand()));
$host="localhost";
$user="root";
$password="";
$db="test";
$con=mysqli_connect("$host","$user","$password","$db");
//$select=mysql_select_db('test',$con);

if (!@$con) {
echo ("Con Failed").mysql_error();
}
//else
//echo "Full Connection Established...Nice Work";
//else echo ("Connected and Db selected..Nice Work");
?>

 

Finally, here's the update code that isn't still working:

 

<?php
include("cn.php");
//$username=$_GET['username'];
if (!@$_SESSION['username']){
header("location:Not_Logged.php"); }

$select="select * from users where username='$_SESSION[username]'";
$rs_cat=mysqli_query($con,$select);

$row=mysqli_fetch_array($rs_cat);
$username=$row['username'];
$password=$row['password'];

if (isset($_POST['update'])) {
$update=$_GET['update'];}
else {$update=0;}	

if ($update = "1") {
  	

$update="UPDATE users SET username='$username', password='$password' where username='$_SESSION[username]'";

$ur=mysqli_query($con,$update) or die (mysql_error());

}
//}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UPDATE</title>
</head>

<body>
<?php //echo $_SESSION['username'];?>
<table align="center" >
<form name="login" action=""  method="post"/>
<tr>
<td>

<th scope="col"><strong>UPDATE</strong></th>
</td>
</tr>
<tr>
<td>
USERNAME
<th scope="col"><input name="username" type="text"  value="<?php echo ("$row[username]");?>"/></th>
</td>
</tr>
<tr>
<td>
PASSWORD
<th scope="col"><input name="password" type="password" value="<?php echo ("$row[password]");?>" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="update" type="submit" value="UPDATE" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="id" type="hidden"  value="<?php echo $row[id];?>"  /></th>
</td>
</form>
</table>
</body>
</html>

 

What else do you think could be wrong here?

Link to comment
Share on other sites

Try this...

 

<?php
include("cn.php");
//$username=$_GET['username'];
if (!@$_SESSION['username']){
header("location:Not_Logged.php");
}


$oldname = $_SESSION['username'];
$select="select id from users where username=''$oldname'";
$rs_cat=mysqli_query($con,$select);
$row=mysqli_fetch_array($rs_cat);
$id = $row['id'];
$old_user_name = $row['username'];
$old_password = $row['password'];

if (isset($_POST['update'])) {
$update=$_POST['update'];
}else{
$update=0;
} 

if ($update = "1") {
$username=$_POST['username'];
$password=$_POST['password']; 
$update="UPDATE users SET username='$username', password='$password' where id = '$id'";
$ur=mysqli_query($con,$update) or die (mysql_error());

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UPDATE</title>
</head>

<body>
<?php //echo $_SESSION['username'];?>
<table align="center" >
<form name="login" action="" method="post"/>
<tr>
<td>

<th scope="col"><strong>UPDATE</strong></th>
</td>
</tr>
<tr>
<td>
USERNAME
<th scope="col"><input name="username" type="text" value="<?php echo ("$row[username]");?>"/></th>
</td>
</tr>
<tr>
<td>
PASSWORD
<th scope="col"><input name="password" type="password" value="<?php echo ("$row[password]");?>" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="update" type="submit" value="UPDATE" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="id" type="hidden" value="<?php echo $row[id];?>" /></th>
</td>
</form>
</table>
</body>
</html>

You may need to do some additional modifications to change the  existing session variable 'username' to the new username value

Link to comment
Share on other sites

Here's my modified code:


<?php
include("cn.php");
//$username=$_GET['username'];
if (!@$_SESSION['username']){
   header("location:Not_Logged.php"); 
   }
$oldname = $_SESSION['username'];
$select="select * from users where username='$_SESSION[username]'";
   $rs_cat=mysql_query($select);

$row=mysql_fetch_array($rs_cat);
$id=$row['id'];
$old_user_name = $row['username'];
$old_password = $row['password'];

if (isset($_POST['update'])) {
   $update=$_GET['update'];}
else {$update=0;}   

if ($update = "1") {
     $username=$_POST['username'];
 $password=$_POST['password'];
$update="UPDATE users SET username='$username', password='$password' where username = '$_SESSION[username]'";
$ur=mysql_query($update) or die (mysql_error());
   
}
//}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UPDATE</title>
</head>

<body>
<?php //echo $_SESSION['username'];?>
<table align="center" >
<form name="login" action=""  method="post"/>
<tr>
<td>

<th scope="col"><strong>UPDATE</strong></th>
</td>
</tr>
<tr>
<td>
USERNAME
<th scope="col"><input name="username" type="text"  value="<?php echo ("$row[username]");?>"/></th>
</td>
</tr>
<tr>
<td>
PASSWORD
<th scope="col"><input name="password" type="password" value="<?php echo ("$row[password]");?>" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="update" type="submit" value="UPDATE" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="id" type="hidden"  value="<?php echo $row[id];?>"  /></th>
</td>
</form>
</table>
</body>
</html>

By the way lite, here is my db struc

 

-- phpMyAdmin SQL Dump

-- version 3.2.4

-- http://www.phpmyadmin.net

--

-- Host: localhost

-- Generation Time: Sep 10, 2010 at 04:51 PM

-- Server version: 5.1.41

-- PHP Version: 5.3.1

 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

 

 

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

/*!40101 SET NAMES utf8 */;

 

--

-- Database: `test`

--

 

-- --------------------------------------------------------

 

--

-- Table structure for table `users`

--

 

CREATE TABLE IF NOT EXISTS `users` (

  `id` int(233) NOT NULL AUTO_INCREMENT,

  `username` varchar(233) DEFAULT NULL,

  `password` varchar(233) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

 

--

-- Dumping data for table `users`

--

 

INSERT INTO `users` (`id`, `username`, `password`) VALUES

(1, '', '');

 

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

 

 

 

The problem now is that the values in the db aren't showing again...what really is going on in here?

Link to comment
Share on other sites

I suspect your problem is with the chunk below. Why are you assigning $update the value of a $_GET var based on whether a $_POST var is set? My suspicion is that $update never equals 1, thus the update query never runs.

if (isset($_POST['update'])) {
$update=$_GET['update'];
}

 

Add this to the top of the script to see exactly what is in the POST and GET superglobals.

echo 'POST array: <pre>'; print_r($_POST); echo '</pre>';
echo 'GET array: <pre>'; print_r($_GET); echo '</pre>';

Link to comment
Share on other sites

Just something to note really:-

@litebearer:

1. your form has NO action

If this hasn't been set then the page/form will just submit to itself, so for validation purposes you do need to have something defined in the action="" attribute, but if your not bothered about W3C validation, just leave it blank; personally I would just put the filename in of the file that is using it ;)

 

Cheers,

Rw

Link to comment
Share on other sites

Sorry, guys been off the net a couple of days or so...back to business.

I suspect your problem is with the chunk below. Why are you assigning $update the value of a $_GET var based on whether a $_POST var is set? My suspicion is that $update never equals 1, thus the update query never runs.

if (isset($_POST['update'])) {
$update=$_GET['update'];
}

 

Add this to the top of the script to see exactly what is in the POST and GET superglobals.

echo 'POST array: <pre>'; print_r($_POST); echo '</pre>';
echo 'GET array: <pre>'; print_r($_GET); echo '</pre>';

 

Yea, Pik, u're sure right about that..though I saw it before reading your reply to this topic...thanks anyway... but check below for something else...

Just something to note really:-

@litebearer:

1. your form has NO action

If this hasn't been set then the page/form will just submit to itself, so for validation purposes you do need to have something defined in the action="" attribute, but if your not bothered about W3C validation, just leave it blank; personally I would just put the filename in of the file that is using it ;)

 

Cheers,

Rw

 

rwwd, thanks also for the response, but let me remind you that its not necessary to do that in php...acod 2 1 of the texts I read up (sitepoint) when I was just starting up in php/mysql has 2 do with normalization...thanks anyway..

 

 

 

 

 

While I was offline, I managed to get the script to work... here's how I went about it I commented on the trouble spots:

 

<?php
include("cn.php");
$msg="Record_Updated";
//$username=$_GET['username'];
if (!@$_SESSION['username']){
header("location:Not_Logged.php"); }

$select="select * from users where username='$_SESSION[username]'";
$rs_cat=mysqli_query($con,$select);

$row=mysqli_fetch_array($rs_cat);
$username=$row['username'];
$password=$row['password'];
$id=$row['id'];

if (isset($_POST['update'])) {//this actually was one of my trouble spot now corrected
$update=$_POST['update'];

//this was another place i MISSED but now corrected
$username=$_POST['username'];
$password=$_POST['password'];	  	
//finally where id='$row[id]' I used now works after passing it as a parameter
$update="UPDATE users SET username='$_POST[username]', password='$_POST[password]' where id='$row[id]'";
$ud=mysqli_query($con,$update);
}
//}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UPDATE</title>
</head>

<body>
<p><li><?php echo ("<a href='Logout.php?user=$_SESSION[username]&sessid=$sessid&id=$id'>Logout</a>");?></li>
<li><?php echo ("<a href='update.php?user=$_SESSION[username]&sessid=$sessid&id=$id'>Update</a>");?></li>
<?php //echo $_SESSION['username'];?>
<table align="center" >
<form name="login" action="<?php $_SERVER['PHP_SELF'];?>" "  method="post"/>
<tr>
<td>

<th scope="col"><strong>UPDATE</strong></th>
</td>
</tr>
<tr>
<td>
USERNAME
<th scope="col"><input name="username" type="text"  value="<?php echo ("$row[username]");?>"/></th>
</td>
</tr>
<tr>
<td>
PASSWORD
<th scope="col"><input name="password" type="password" value="<?php echo ("$row[password]");?>" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="update" type="submit" value="UPDATE" /></th>
</td>
</tr>
<tr>
<td>
<th scope="col"><input name="id" type="hidden"  value="<?php echo $row[id];?>"  /></th>
</td>
</form>
</table>
</body>
</html>

 

Pik and rwwd, The only thing left now is that after it updates, it seems to leave the form empty rather than reflect the actual updated values..why? Until I log out and log in again, before the form shows it. What do you guys think might be wrong there?

 

Link to comment
Share on other sites

Using phone, so pardon my brevity, but u need to immediately run SELECT query after successful INSERT query to repopulate form fields, if I understand correctly.

 

 

Did you say immediately run select query? How on earth am I supposed to do that Pik ? Did you get my question at all? I'm saying if an authenticated logged user updates his username, password with this => code

 

<?phpinclude("cn.php");$msg="Record_Updated";//$username=$_GET['username'];if (!@$_SESSION['username']){   header("location:Not_Logged.php"); }$select="select * from users where username='$_SESSION[username]'";   $rs_cat=mysqli_query($con,$select);$row=mysqli_fetch_array($rs_cat);$username=$row['username'];$password=$row['password'];$id=$row['id'];if (isset($_POST['update'])) {//this actually was one of my trouble spot now corrected   $update=$_POST['update'];   //this was another place i MISSED but now corrected$username=$_POST['username'];$password=$_POST['password'];        //finally where id='$row[id]' I used now works after passing it as a parameter$update="UPDATE users SET username='$_POST[username]', password='$_POST[password]' where id='$row[id]'";$ud=mysqli_query($con,$update);}//}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>UPDATE</title></head><body><p><li><?php echo ("<a href='Logout.php?user=$_SESSION[username]&sessid=$sessid&id=$id'>Logout</a>");?></li><li><?php echo ("<a href='update.php?user=$_SESSION[username]&sessid=$sessid&id=$id'>Update</a>");?></li><?php //echo $_SESSION['username'];?><table align="center" ><form name="login" action="<?php $_SERVER['PHP_SELF'];?>" "  method="post"/><tr><td><th scope="col"><strong>UPDATE</strong></th></td></tr><tr><td>USERNAME<th scope="col"><input name="username" type="text"  value="<?php echo ("$row[username]");?>"/></th></td></tr><tr><td>PASSWORD<th scope="col"><input name="password" type="password" value="<?php echo ("$row[password]");?>" /></th></td></tr><tr><td><th scope="col"><input name="update" type="submit" value="UPDATE" /></th></td></tr><tr><td><th scope="col"><input name="id" type="hidden"  value="<?php echo $row[id];?>"  /></th></td></form></table></body></html>

 

 

the form does not display the updated  values until the user logs out and in again. What do you think might be wrong there?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.