Jump to content

not updating database


searls03

Recommended Posts

why does this code not update database.....any errors you see off the bat?  none of the messages are displaying for whether it posts or not......so I think there is something wrong with posting or a loop or something.

 

<?php
session_start();
include "config2.php";
if (!isset($_SESSION['id'])) { 
   echo 'Please <a href="login.php">log in</a> to access your account';
   exit(); 
}

//Connect to the database through our include 
include_once "connect_to_mysql.php";
// Place Session variable 'id' into local variable
$userid = $_SESSION['id'];
?>
<?php
//action: view users -----------------------------------------------------------------------------
if (isset($_GET['viewUsers'])) {
//get all active users
$query = "SELECT name, username, phone, address, city, state, zip, cell, email, accounttype, badges, password, rank, userid FROM members WHERE userid=userid";
$rs = mysql_query($query);

?>
  <table width="563" border='1'>
    <tr>
      <th width="54">Name</th><th width="84">Username</th><th width="47">Email</th>
      <th width="148">Access Level</th>
      <th width="105"> </th>
      <th width="85"> </th>
      </tr>
    <?php
	//show the users
	while ($row = mysql_fetch_assoc($rs)) {
		?>
    <tr>
      <td><?php echo $row['name'];?></td>
      <td><?php echo $row['username'];?></td>
      <td><?php echo $row['email'];?></td>
      <td><?php echo $row['accounttype']?></td>
      <td> </td>
      <td><a href='admin.php?edit&id=<?php echo $row['userid'];?>'>Edit</a>, <a href='admin.php?delete&id=<?php echo $row['userid'];?>'>Delete</a></td>
      </tr>
    <?php
	}
	?>
    </table>
  <?php
}
//action: edit user -----------------------------------------------------------------------------
if (isset($_GET['edit']) && isset($_GET['id'])) {
$userid = (int) $_GET['id'];
if ($userid == 0) {
	die("Invalid ID provided.");
}
//execution when completed the edit user form and pressed submit button ---------------------
if (isset($_POST['editUser'])) {
	//validate data ------------------------------------------------------------------------
	//check empty fields
	$notRequired = array("email","phone","address", "city", "state","zip","cell" ); //passwords won't be checked, as they are not required
	foreach ($_POST as $k=>$v) {
		if ($v == "" && !in_array($k,$notRequired)) {
			$error[$k] = "<strong>This field is empty</strong>";
		}
	}
	//escape string
	$name = "mysql_real_escape_string{$_POST['fname']} {$_POST['last']}";
	$phone = mysql_real_escape_string($_POST['phone']);
	$address = mysql_real_escape_string($_POST['address']);
	$city = mysql_real_escape_string($_POST['city']);
	$state = mysql_real_escape_string($_POST['state']);
	$zip = mysql_real_escape_string($_POST['zip']);
	$email = mysql_real_escape_string($_POST['email']);
	$cell = mysql_real_escape_string($_POST['cell']);
	$username = mysql_real_escape_string($_POST['username']);
	$last = mysql_real_escape_string($_POST['last']);
	$first = mysql_real_escape_string($_POST['fname']);


	//check email validation, the function is available at config.php

	//check username exists in database
	$res = mysql_query("SELECT username FROM members WHERE username='".$username."' AND username != '".$username."'");
	if (mysql_num_rows($res) == 1) {
		$error['username'] .= " <strong>Username already existst in database!</strong>";
	}
	//check both passwords are the same when password fields are not empty

	//end validate data ---------------------------------------------------------------------

	//save to database when no errors are detected ------------------------------------------
	if (count($error) == 0) {
			$query = "UPDATE members SET username='$username', email='$email', name='".$name."', phone='".$phone."',address='".$address."', city='".$city."',state='".$state."', zip='".$zip."',cell='".$cell."',badges='".$badges."', rank='".$rank."', first='".$first."', last='".$last."' WHERE userid='".$_GET['userid']."'";
		$query1 = "UPDATE sessions SET username='".$username."', email='".$email."',name='".$name."', phone='".$phone."',address='".$address."', city='".$city."',state='".$state."', zip='".$zip."',cell='".$cell."',badges='".$badges."', rank='".$rank."' WHERE id='".$userid."'";




		//update username session if you edit yourself
		if ($userid == $_SESSION['auth_admin_userid']) {
			$_SESSION['auth_admin_username'] = $username;
		}

		if (mysql_query($query)|| mysql_query($query1)) 
		{
			echo "<p><strong>User has been edited and saved to the database.</strong></p>";
		} else {
			echo "<strong>User has NOT been edited and saved into the database. ".mysql_error()."</strong>";
		}
	}
}
//get user from the database and put data into $_POST variables.
$rs = mysql_query("SELECT first, last, username, phone, address, city, state, zip, cell, email, badges, rank, accounttype FROM members WHERE userid = ".$userid."");
if (mysql_num_rows($rs) == 0) {
	die("User does not exists!");
}
$row = mysql_fetch_assoc($rs);
$_POST['fname'] = $row['first'];

$_POST['last'] = $row['last'];
	$_POST['username'] = $row['username'];
$_POST['phone'] = $row['phone'];
	$_POST['address'] = $row['address'];
$_POST['city'] = $row['city'];
$_POST['state'] = $row['state'];
$_POST['zip'] = $row['zip'];
$_POST['cell'] = $row['cell'];
$_POST['email'] = $row['email'];
	$_POST['badges'] = $row['badges'];
$_POST['rank'] = $row['rank'];
	$_POST['accounttype'] = $row['accounttype'];


//if is admin, then $_POST['admin'] exists


?>
  
  
  
  
  
  
  <form action="admin.php?edit&id=<?php echo $userid; ?>" method="post">
  <div id="TabbedPanels1" class="TabbedPanels">
  <ul class="TabbedPanelsTabGroup">
    <li class="TabbedPanelsTab" tabindex="0">My Info</li>
    <li class="TabbedPanelsTab" tabindex="0">Merit Badges</li>
    <li class="TabbedPanelsTab" tabindex="0">Scout Rank</li>
    </ul>
  <div class="TabbedPanelsContentGroup">
  <div class="TabbedPanelsContent">
  
    <table align="center" cellpadding="8" cellspacing="8">
      <tr>  
        <td><div align="right">First Name:</div></td>
        <td>	<input type="text" name="name" value='<?php echo $_POST['fname'];?>' />
          <?php echo(isset($error['fname']))?$error['fname']:"";?></td>
        </tr> <tr>  
        <td><div align="right">Last Name:</div></td>
        <td>	<input type="text" name="name" value='<?php echo $_POST['last'];?>' />
          <?php echo(isset($error['last']))?$error['last']:"";?></td>
        </tr>
      <tr>
        <td><div>Phone Number:</div></td>
        <td><input type="text" name="phone" value='<?php echo $_POST['phone'];?>' />
          <?php echo(isset($error['phone']))?$error['phone']:"";?></td>
        </tr>  
      <tr>
        <td><div align="right">Address:</div></td>
        <td><input type="text" name="address" value='<?php echo $_POST['address'];?>' />
          <?php echo(isset($error['address']))?$error['address']:"";?></td>
        </tr>
      <tr>
        <td><div align="right">City:</div></td>
        <td><input type="text" name="city" value='<?php echo $_POST['city'];?>' />
          <?php echo(isset($error['city']))?$error['city']:"";?></td>
        </tr>
      <tr>
        <td><div align="right">State:</div></td>
        <td><input type="text" name="state" value='<?php echo $_POST['state'];?>' />
          <?php echo(isset($error['state']))?$error['state']:"";?></td>
        </tr>  
      <tr>
        <td><div align="right">Zip Code:</div></td>
        <td><input type="text" name="zip" value='<?php echo $_POST['zip'];?>' />
          <?php echo(isset($error['zip']))?$error['zip']:"";?></td>
        </tr>
      <tr>
        <td><div align="right">Email:</div></td>
        <td>	
         
            <input type="text" name="email" value='<?php echo $_POST['email'];?>' />
          <?php echo(isset($error['email']))?$error['email']:"";?></td>
        </tr> 
      <tr>
        <td><div align="right">Cell Phone:</div></td>
        <td><input type="text" name="cell" value='<?php echo $_POST['cell'];?>' />
          <?php echo(isset($error['cell']))?$error['cell']:"";?></td>
        </tr>  
      <tr>
        <td class="aaaaaaa" align="right"><div>Username:</div></td>
        <td><label for="username"></label>
          <input type="text" name="username" value='<?php echo $_POST['username'];?>' />
          <?php echo(isset($error['username']))?$error['username']:"";?>
          </td>
        </tr>
      <input name="userid" type="hidden" value="<?php echo $userid; ?>" />
      
      </table>
    <p>         
    
    
    </div>
    <div class="TabbedPanelsContent">
      <label for="badges"></label>
      <input name="badges" type="text" id="badges" value='<?php echo $_POST['badges'];?>' />
      <?php echo(isset($error['badges']))?$error['badges']:"";?>
      </div>
    <div class="TabbedPanelsContent">
      <input name="rank" type="text" id="rank" value='<?php echo $_POST['rank'];?>' />
      <?php echo(isset($error['rank']))?$error['rank']:"";?>
      </div>
    </div>
  </div>
    <input name="editUser" type="submit" value="Save" />
    
    
    </form>   
  <p>
    <script type="text/javascript">
var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
      </script>
    </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <p> </p>
  <?php
}
?>
  

<br /><br />





</div></div>
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"../SpryAssets/SpryMenuBarDownHover.gif", imgRight:"../SpryAssets/SpryMenuBarRightHover.gif"});
</script>

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.