Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to do an edit page and after editing ah should be able to update the information in other words am doing both an edit page and update but i can get the edit page to either edit or update the information.


in the update page, I am get these errors and have been banging my head trying to solve it.

this is the error no 1.
PHP
Undefined variable: userName in C:\wamp64\www\luana_itec244\php\update.php on line 30



this is error no. 2
PHP
: Undefined variable: password in C:\wamp64\www\luana_itec244\php\update.php on line 30



and here is what i have so far, I really need to move one from this but I have to get the working and I am stuck, can someone please help me out here. much thanks

What I have tried:

this is the update codes.

PHP
<pre><?php
		
			session_start();
			$_SESSION['user'];
			//$title=$_POST['title'];
			$firstname=$_POST['firstname'];
			$lastname=$_POST['lastname'];
			//$address=$_POST['address'];
			//$txtemail=$_POST['txtemail'];
			//$gender=$_POST['gender'];
			//
			
			
			//create connection to database
			$db_host='localhost';
			$db_username='root';
			$db_password="";
			
			
				//check connection
				$con = mysqli_connect($db_host, $db_username, $db_password) or die(mysqli_connect_error());
			
				//select the database
				mysqli_select_db($con, 'food') or die(mysqli_error($con));
				
				  	
					//$userName=$_SESSION['user'];
					//$password=$_SESSION['user'];
				 
					$sql="SELECT * FROM member WHERE userName='$userName' AND pword=md5('$password')";
					$result=mysqli_query($con, $sql) or die ("Error:" .mysqli_error());
					
					//collect info from database
					if($result)
					{
						$rowcount=mysqli_num_rows($result);
						
						if($rowcount==1)
						{
							
							//update database with information from the edit form
							$update="UPDATE member SET firstname='$firstname' WHERE userName='".$_SESSION['user']."'";
							$update="UPDATE member SET lastname='$lastname' WHERE userName='".$_SESSION['user']."'";
							
							//mysqli_query($con, $update) or die(mysqli_error($con));
							
							if(mysqli_query($con, $update))
							{                               
						
								echo "<script type=\"text/javascript\">
									alert('Profile successfully updated.');
									window.location=\"profile.php\";
									</script>";
									
								mysqli_close($con);
								header("location:edit.php");
								
							}
							else
							{
								echo "<scrip type=\"text/javascript\">
										  alert('Incorrect password');
										  window.locaton=\"edit.php\";
									  </script>";
							}
						}	
						
					}
			
		
		

?>
Posted
Updated 19-May-18 9:06am

The error comes from your SQL query,
$sql="SELECT * FROM member WHERE userName='$userName' AND pword=md5('$password')";

You are trying to access the variables that are not defined anywhere in the visible code of PHP, either perhaps you need to check via isset, to see if this can be done or not,
PHP
if(isset($userName, $password)) {
   // they are set, execute query
} else {
   // other wise
}

Check with these and then execute, that will be safely executed.
PHP: isset - Manual[^]
 
Share this answer
 
Quote:
How do you get rid of these two annoying errors plzzzz

May be you should reread your code and use existing variables like $db_username and $db_password

PHP
$sql="SELECT * FROM member WHERE userName='$userName' AND pword=md5('$password')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
thank you both, ppolymorphe and Afzaal Ahmad
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900