tobimichigan Posted September 7, 2010 Share Posted September 7, 2010 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... Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 7, 2010 Share Posted September 7, 2010 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 Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 7, 2010 Author Share Posted September 7, 2010 Lightbearer, thanks for your response but please could you be more clearer in your explanation with an example? I mean what exactly am I doing wrong here? Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 7, 2010 Share Posted September 7, 2010 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 Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 7, 2010 Author Share Posted September 7, 2010 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? Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 7, 2010 Share Posted September 7, 2010 1. your form has NO action 2. Do NOT use both GET and POST to set the values of your variables 3. Where are you setting the session variable? Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 7, 2010 Share Posted September 7, 2010 You are obviously coming to this page via another page where the person has selected a username they want to modify. Please show the code for that page also. Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 7, 2010 Author Share Posted September 7, 2010 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? Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 8, 2010 Author Share Posted September 8, 2010 Please is there some1 out there that could help me with this script..I need to know why my db values are not being updated after clicking update..please some1 come to the rescue... Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 8, 2010 Share Posted September 8, 2010 Does your table have a unique ID for each row? If so, what is the name of that field? Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 8, 2010 Author Share Posted September 8, 2010 Lite, the unique key is "id" still don't know why this script is not updating...any other ideas? U can run it it for your self and see..I'm yet to really know what could be wrong... Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 8, 2010 Share Posted September 8, 2010 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 Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 10, 2010 Author Share Posted September 10, 2010 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? Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 11, 2010 Author Share Posted September 11, 2010 any1 with any better idea? Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 12, 2010 Author Share Posted September 12, 2010 If I can't get a valid answer from here I wonder where I could then get 1...besides this is the best php forum I have come to know on the internet..would some1 please come to the rescue? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2010 Share Posted September 12, 2010 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>'; Quote Link to comment Share on other sites More sharing options...
rwwd Posted September 12, 2010 Share Posted September 12, 2010 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 Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 15, 2010 Author Share Posted September 15, 2010 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? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 15, 2010 Share Posted September 15, 2010 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. Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 17, 2010 Author Share Posted September 17, 2010 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? Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 17, 2010 Share Posted September 17, 2010 Did you say immediately run select query? How on earth am I supposed to do that Pik just put a select query as the next line in your code AFTER the update query line ie. update query where id = userid select query where id = userid Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.