Jump to content

Updating issue


CloudBreaker
Go to solution Solved by QuickOldCar,

Recommended Posts

Not sure what I'm missing here.  I a very similar script updating fields with no problems.   The data is being called from the database without any errors, but as soon as I hit submit whether or not I update a field or not I get a couple of undefined variables.  Did I misplace a curly bracket?

 

thanks,

 

 

CB

<?php


  
 // Connect to MySQL
		$conn = mysqli_connect("localhost","root","","hsa_project_hub");
		
		session_start(); 

		if(!$_SESSION['admin_login']){
		header("location: admin_login.php");
		}
		else {

	
// Grab user id from admin_userList.php and pull user info from database	
		if(isset($_GET['id']) && is_numeric($_GET['id'])){
			
		$edit_id=$_GET['id'];
		
		$sql = "select * from hsa_users where id='$edit_id'";
			
		$result=mysqli_query($conn,$sql);
		$row=mysqli_fetch_assoc($result);	
			
		$run = mysqli_query($conn,$sql);	
			
		$row=mysqli_fetch_array($run);
			$id					=$row['id']; 
			$user_firstName		=$row['user_firstName'];
			$user_lastName		=$row['user_lastName'];
			$user_company		=$row['user_company'];
			$user_discipline	=$row['user_discipline'];
			$user_email			=$row['user_email'];
			$user_loginName		=$row['user_loginName'];
			$user_pass			=$row['user_pass'];
			$email_alerts		=$row['email_alerts'];
				
		
		 

	}

?>

<!DOCTYPE HTML>

<html>

	<head>
		<title>Project Hub New User</title>

	<link href="hsastyle.css" rel="stylesheet">

	</head>
		<body>
			<div id="main_container">
				<p><em>version 1.0 beta</em></p>
		
				<div id="banner">
			
					<div id="logo">
						<img src="images/hsa-logo.jpg" alt=HSA logo>
					</div>
					
					<a href = "admin.php">
						<H2><em>Admin Panel</em></h2>
						<H5><a href="adminLogout.php">Log Out</a></H5>
						<H6>Welcome  <?php echo $_SESSION['admin_login'];?>
					</a>
			
						
				</div> <!--End Banner-->
			
			<!--form for adding users-->

<h1 align="center">Edit User</h1>
				
						
				<form action="edit_user.php" method="post"> 
								
						<table align="center" bgcolor="d8d8d8" width="960">
							<tr align="center">
							</tr>					
							
							<tr>
								<td align="right"><strong>First Name:</strong></td>								
								<td>
									<input type="text" name="user_firstName" value="<?php echo $user_firstName;?>"/> 
								</td>
							</tr>
							
							<tr>
								<td align="right"><strong>Last Name:</strong></td>								
								<td>
									<input type="text" name="user_lastName" value="<?php echo $user_lastName;?>"/> 
								</td>
							</tr>
							
							<tr>
								<td align="right"><strong>Company:</strong></td>								
								<td>
									<input type="text" name="user_company" value="<?php echo $user_company;?>"/> 
								</td>
							</tr>
							
			<tr>
				<td align="right"><strong>Discipline:</strong></td>
				<td>
					<select name="discipline">
						<option><?php echo $user_discipline;?></option>
						<option>Architect</option>
						<option>General Contractor</option>
						<option>Owner</option>
						<option>Structural Engineer</option>
						<option>MEP Engineer</option>
						<option>Civil Engineer</option>
						<option>Landscape Architect</option>
						<option>Developer</option>
					</select>
				</td>
			</tr>
							
							<tr>
								<td align="right"><strong>Email:</strong></td>								
								<td>
									<input type="email" name="user_email" value="<?php echo $user_email;?>"/> 
								</td>
							</tr>
							
							<tr>
								<td align="right"><strong>Log In Name:</strong></td>								
								<td>
									<input type="text" name="user_loginName" value="<?php echo $user_loginName;?>"/> 
								</td>
							</tr>
							
							<tr>
								<td align="right"><strong>Password:</strong></td>								
								<td>
									<input type="text" name="user_pass" value="<?php echo $user_pass;?>"   required="required"/> 
								</td>
							</tr>	

							<tr>
								<td align="right"><strong>Email Alerts:</strong></td>								
								<td><br>
									<input type="radio" name="email_alerts"  value="yes">Yes<br> 
									<input type="radio" name="email_alerts"  value="no">No									
								</td>
							</tr>
							
							<tr align="left">
								<td colspan="6">
								<input type="submit" name="update" value="Update"/>
								</td>
							</tr>
						
						
						
						
						 
					</table>
	
			


		</form>
		
<?php
			//getting the text information and saving in local variables
			if(isset($_POST['update'])){
				
			$user_firstName		=mysqli_real_escape_string($conn,$_POST['user_firstName']);
			$user_lastName		=mysqli_real_escape_string($conn,$_POST['user_lastName']);
			$user_company		=mysqli_real_escape_string($conn,$_POST['user_company']);
			$user_discipline	=mysqli_real_escape_string($conn,$_POST['user_discipline']);
			$user_email			=mysqli_real_escape_string($conn,$_POST['user_email']);
			$user_loginName		=mysqli_real_escape_string($conn,$_POST['user_loginName']);
			$user_pass			=mysqli_real_escape_string($conn,$_POST['user_pass']);
			$email_alerts		=mysqli_real_escape_string($conn,$_POST['email_alerts']);

			
			$update = "UPDATE hsa_users SET `user_firstName`='$user_firstName', `user_lastName`='$user_lastName', `user_company`='$user_company', `user_discipline`='$user_discipline', `user_email`='$user_email', `user_loginName`='$user_loginName',
						`user_pass`='$user_pass', `email_alerts`='$email_alerts' WHERE id=$id";
						
			$run_update = mysqli_query($conn, $update);
			
			if($run_update){
				echo "<script>alert('User has been successfully updated.')</script>";
				echo "<script>window.open('admin_userlist.php','_self')</script>";
			} 
		}
?>
		
			</div> <!--End main container-->
		
		
		
		
		</body>
		
		
</html>
<?php } ?>
Link to comment
Share on other sites

I get a couple of undefined variables.

 

 

care to share what those errors were so that we don't have to guess?

 

we don't have the ability to run your code with your data, so you must share what you know about a problem in order to help those that would take the time to try and help you with your code problems.

Link to comment
Share on other sites

As mac_gyver mentioned, it would be helpful to know the errors.

 

One thing that I noticed is that your select box is named "discipline"

<select name="discipline">

And the code that processes the form submission uses "user_discipline" as the index for $_POST

$user_discipline =mysqli_real_escape_string($conn,$_POST['user_discipline']);
Link to comment
Share on other sites

My apologies...here are the warnings:

 

! ) Notice: Undefined variable: user_firstName in B:\Programs\wamp\www\hsa\edit_user.php on line 91

 

( ! ) Notice: Undefined variable: user_lastName in B:\Programs\wamp\www\hsa\edit_user.php on line98 Call Stack #TimeMemoryFunctionLocation 10.0017266664{main}( )..\edit_user.php:0 "/>

 

( ! ) Notice: Undefined variable: user_company in B:\Programs\wamp\www\hsa\edit_user.php on line 105 Call Stack #TimeMemoryFunctionLocation 10.0017266664{main}( )..\edit_user.php:0 "/>

 

( ! ) Notice: Undefined variable: user_email in B:\Programs\wamp\www\hsa\edit_user.php on line 129 Call Stack #TimeMemoryFunctionLocation 10.0017266664{main}( )..\edit_user.php:0 "/>

 

( ! ) Notice: Undefined variable: user_loginName in B:\Programs\wamp\www\hsa\edit_user.php on line 136 Call Stack #TimeMemoryFunctionLocation 10.0017266664{main}( )..\edit_user.php:0 "/> 

 

 ! ) Notice: Undefined variable: user_pass in B:\Programs\wamp\www\hsa\edit_user.php on line 143 Call Stack #TimeMemoryFunctionLocation 10.0017266664{main}( )..\edit_user.php:0 " required="required"/>

 

Table structure:

 

https://drive.google.com/file/d/0B06KJO0YEuzxcjhIN1pwQWZ6XzA/view?usp=sharing

 

 

( ! ) Notice: Undefined index: user_discipline in B:\Programs\wamp\www\hsa\edit_user.php on line 179

 

( ! ) Notice: Undefined index: email_alerts in B:\Programs\wamp\www\hsa\edit_user.php on line 183

 

( ! ) Notice: Undefined variable: id in B:\Programs\wamp\www\hsa\edit_user.php on line 187

Link to comment
Share on other sites

  • Solution

Besides what other posters said...

 

Initially you get the id from a $_GET['id']

That's fine but when you post the form is not being passed because now using POST and not included in the action, the query does not know where to update to a specific id.

 

Pass the id into the update form a hidden type.

<input type="hidden" name="id" value="<?php echo $id;?>"/>

 

 

Now add the $id back in the update submit check

if(isset($_POST['update'])){

 

if(isset($_POST['id']) && is_numeric($_POST['id'])){
        
        $id=$_POST['id'];
        
}

 

//.....rest of code

 

 

You may as well change the GET id one to $id , when query runs you return the $row['id'] anyway

 

if(isset($_GET['id']) && is_numeric($_GET['id'])){
            
        $id=$_GET['id'];
        
        $sql = "select * from hsa_users where id='$id'";

Edited by QuickOldCar
Link to comment
Share on other sites

Looking over the code....

 

If $_GET['id'] is not set, then none of the variables are going to be defined.

$row is defined twice and only the second definition counts.

Put in some echo's to see what is going on along the code path.

        $row=mysqli_fetch_assoc($result);    
            
        $run = mysqli_query($conn,$sql);    
            
        $row=mysqli_fetch_array($run);
Edited by hansford
Link to comment
Share on other sites

Also....

 

Place this section before your form, are trying to use all variables that do not exist yet.

<?php
            //getting the text information and saving in local variables
            if(isset($_POST['update'])){
            
if(isset($_POST['id']) && is_numeric($_POST['id'])){
        
        $id=$_POST['id'];
        
}
            
            $user_firstName        =mysqli_real_escape_string($conn,$_POST['user_firstName']);
            $user_lastName        =mysqli_real_escape_string($conn,$_POST['user_lastName']);
            $user_company        =mysqli_real_escape_string($conn,$_POST['user_company']);
            $user_discipline    =mysqli_real_escape_string($conn,$_POST['user_discipline']);
            $user_email            =mysqli_real_escape_string($conn,$_POST['user_email']);
            $user_loginName        =mysqli_real_escape_string($conn,$_POST['user_loginName']);
            $user_pass            =mysqli_real_escape_string($conn,$_POST['user_pass']);
            $email_alerts        =mysqli_real_escape_string($conn,$_POST['email_alerts']);
 
            
            $update = "UPDATE hsa_users SET `user_firstName`='$user_firstName', `user_lastName`='$user_lastName', `user_company`='$user_company', `user_discipline`='$user_discipline', `user_email`='$user_email', `user_loginName`='$user_loginName',
                        `user_pass`='$user_pass', `email_alerts`='$email_alerts' WHERE id=$id";
                        
            $run_update = mysqli_query($conn, $update);
            
            if($run_update){
                echo "<script>alert('User has been successfully updated.')</script>";
                echo "<script>window.open('admin_userlist.php','_self')</script>";
            }
        }
?>

Link to comment
Share on other sites

that's more than a couple of errors. a couple of errors would be 2 - post-144491-0-42365500-1442462117_thumb.jpeg
 
did you look at and try to find why your code is producing any of those errors? and given that cyberRobot told you the reason for one of them, why didn't you at least fix that one?
 
with your 60 posts and 2+ months on this forum, you should be at the point, after just a couple (2) or a few (3-4) weeks, of being able to find and fix simple things like undefined variables and undefined indexes in posted form data.

 

in one of your previous threads, someone suggested a code layout for your page. this was primarily to add origination to your code, but it was also to help make your code foolproof, so that you won't have undefined variables that are dependent on either a query retrieving the variables or a form submission supplying the same variables. there was in fact a comment in code in that thread that read -

 

(if you were editing an existing record, you would retrieve that data here...)

 

Edited by mac_gyver
  • Like 1
Link to comment
Share on other sites

This should be better with the changes.

 

Should have a bit more error checking in this though.

<?php
session_start();
// Connect to MySQL
$conn = mysqli_connect("localhost", "root", "", "hsa_project_hub");


if (!$_SESSION['admin_login']) {
    header("location: admin_login.php");
    exit;
}

$id = '';

// Grab user id from admin_userList.php and pull user info from database    
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    
    $id = $_GET['id'];
    
    $sql = "select * from hsa_users where id='$id'";
    
    $result = mysqli_query($conn, $sql);
    $row    = mysqli_fetch_assoc($result);
    
    $run = mysqli_query($conn, $sql);
    
    $row             = mysqli_fetch_array($run);
    $id              = $row['id'];
    $user_firstName  = $row['user_firstName'];
    $user_lastName   = $row['user_lastName'];
    $user_company    = $row['user_company'];
    $user_discipline = $row['user_discipline'];
    $user_email      = $row['user_email'];
    $user_loginName  = $row['user_loginName'];
    $user_pass       = $row['user_pass'];
    $email_alerts    = $row['email_alerts'];
    

}


//getting the text information and saving in local variables
if (isset($_POST['update'])) {
    
    if (isset($_POST['id']) && is_numeric($_POST['id'])) {
        $id = $_POST['id'];
    }
    
    $user_firstName  = mysqli_real_escape_string($conn, $_POST['user_firstName']);
    $user_lastName   = mysqli_real_escape_string($conn, $_POST['user_lastName']);
    $user_company    = mysqli_real_escape_string($conn, $_POST['user_company']);
    $user_discipline = mysqli_real_escape_string($conn, $_POST['user_discipline']);
    $user_email      = mysqli_real_escape_string($conn, $_POST['user_email']);
    $user_loginName  = mysqli_real_escape_string($conn, $_POST['user_loginName']);
    $user_pass       = mysqli_real_escape_string($conn, $_POST['user_pass']);
    $email_alerts    = mysqli_real_escape_string($conn, $_POST['email_alerts']);
    
    
    $update = "UPDATE hsa_users SET `user_firstName`='$user_firstName', `user_lastName`='$user_lastName', `user_company`='$user_company', `user_discipline`='$user_discipline', `user_email`='$user_email', `user_loginName`='$user_loginName',
                            `user_pass`='$user_pass', `email_alerts`='$email_alerts' WHERE id=$id";
    
    $run_update = mysqli_query($conn, $update);
    
    if ($run_update) {
        echo "<script>alert('User has been successfully updated.')</script>";
        echo "<script>window.open('admin_userlist.php','_self')</script>";
    }
}
?> 
     
    <!DOCTYPE HTML>
     
    <html>
     
    	<head>
    		<title>Project Hub New User</title>
     
    	<link href="hsastyle.css" rel="stylesheet">
     
    	</head>
    		<body>
    			<div id="main_container">
    				<p><em>version 1.0 beta</em></p>
    		
    				<div id="banner">
    			
    					<div id="logo">
    						<img src="images/hsa-logo.jpg" alt=HSA logo>
    					</div>
    					
    					<a href = "admin.php">
    						<H2><em>Admin Panel</em></h2>
    						<H5><a href="adminLogout.php">Log Out</a></H5>
    						<H6>Welcome  <?php echo $_SESSION['admin_login'];?>
    					</a>
    			
    						
    				</div> <!--End Banner-->
    			
    			<!--form for adding users-->
     
    <h1 align="center">Edit User</h1>
    				
    						
    				<form action="edit_user.php" method="post"> 
					
    						<input type="hidden" name="id" value="<?php echo $id;?>"/>
							
    						<table align="center" bgcolor="d8d8d8" width="960">
    							<tr align="center">
    							</tr>					
    							
    							<tr>
    								<td align="right"><strong>First Name:</strong></td>								
    								<td>
    									<input type="text" name="user_firstName" value="<?php echo $user_firstName;?>"/> 
    								</td>
    							</tr>
    							
    							<tr>
    								<td align="right"><strong>Last Name:</strong></td>								
    								<td>
    									<input type="text" name="user_lastName" value="<?php echo $user_lastName;?>"/> 
    								</td>
    							</tr>
    							
    							<tr>
    								<td align="right"><strong>Company:</strong></td>								
    								<td>
    									<input type="text" name="user_company" value="<?php echo $user_company;?>"/> 
    								</td>
    							</tr>
    							
    			<tr>
    				<td align="right"><strong>Discipline:</strong></td>
    				<td>
    					<select name="user_discipline">
    						<option><?php echo $user_discipline;?></option>
    						<option>Architect</option>
    						<option>General Contractor</option>
    						<option>Owner</option>
    						<option>Structural Engineer</option>
    						<option>MEP Engineer</option>
    						<option>Civil Engineer</option>
    						<option>Landscape Architect</option>
    						<option>Developer</option>
    					</select>
    				</td>
    			</tr>
    							
    							<tr>
    								<td align="right"><strong>Email:</strong></td>								
    								<td>
    									<input type="email" name="user_email" value="<?php echo $user_email;?>"/> 
    								</td>
    							</tr>
    							
    							<tr>
    								<td align="right"><strong>Log In Name:</strong></td>								
    								<td>
    									<input type="text" name="user_loginName" value="<?php echo $user_loginName;?>"/> 
    								</td>
    							</tr>
    							
    							<tr>
    								<td align="right"><strong>Password:</strong></td>								
    								<td>
    									<input type="text" name="user_pass" value="<?php echo $user_pass;?>"   required="required"/> 
    								</td>
    							</tr>	
     
    							<tr>
    								<td align="right"><strong>Email Alerts:</strong></td>								
    								<td><br>
    									<input type="radio" name="email_alerts"  value="yes">Yes<br> 
    									<input type="radio" name="email_alerts"  value="no">No									
    								</td>
    							</tr>
    							
    							<tr align="left">
    								<td colspan="6">
    								<input type="submit" name="update" value="Update"/>
    								</td>
    							</tr>
    						
    						
    						
    						
    						 
    					</table>
    	
    			
     
     
    		</form>
    		
    
    		
    			</div> <!--End main container-->
    		
    		
    		
    		
    		</body>
    		
    		
</html>
Link to comment
Share on other sites

that's more than a couple of errors. a couple of errors would be 2 - attachicon.gifcats.jpeg

 

did you look at and try to find why your code is producing any of those errors? and given that cyberRobot told you the reason for one of them, why didn't you at least fix that one?

 

with your 60 posts and 2+ months on this forum, you should be at the point, after just a couple (2) or a few (3-4) weeks, of being able to find and fix simple things like undefined variables and undefined indexes in posted form data.

 

in one of your previous threads, someone suggested a code layout for your page. this was primarily to add origination to your code, but it was also to help make your code foolproof, so that you won't have undefined variables that are dependent on either a query retrieving the variables or a form submission supplying the same variables. there was in fact a comment in code in that thread that read -

  Yes mac_gyver i did try to find out why I was getting errors.  I have not been at my computer for awhile because I had a family member pass away and I just did not have recall did when I was trying to code every day after work.  I before I took a break I created a nice file management system that associates files with each rfi or submittal in the user has the need.   I just need to get back into things.  I feel like I forgot a lot.

 

CB

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.