Jump to content

Recommended Posts

I have a form that is meant to edit username, levels, etc when i hit sutbmit but it's not working.  The form was previously created by someone else and I've been trying to tweak to get it to work but no success...

 

The code is as follows:

 

include 'admindbc.php';

$file_name = "admin";

include("headera.php"); 




if($_SESSION['user_level'] == "0"){
    echo "Only administrators are allowed here!";
    echo "<br><br>";
    echo "<a href='../index.php'>Home</a>";
    exit();
}
include 'admindbc.php';

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $useridb = $_GET["id"];
      $sql = "SELECT * FROM users WHERE id=$useridb";
      $result = mysql_query($sql);
      $myrow = mysql_fetch_array($result);
      ?>


<div id="maincontent3">
  <h1>Edit Profile</h1>
  <br>
<form action="edit.php" method="post">

      <input type="hidden" name="cmd" value="edit">

      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
   	  <table>
	<tr>
		<td><b>User ID:</b></td>
		<td><?php echo $myrow["id"] ?></td>
	</tr>
	<tr>
		<td><b>Username:</b></td>
		<td>
		<INPUT NAME="user_name" VALUE="<?php echo $myrow["user_name"] ?>" SIZE=30></font></td>
	</tr>

	<tr>
		<td><b>Activated:
		</b></td>
		<td>


<input class="box" type="checkbox" name="activated" value="1" <?php if($myrow['activated'] == 1){
    echo "checked";
} ?>>

		</font></td>
	</tr>
	<tr>
		<td><b>User Level:
		</b></td>
		<td><select size="1" name="user_level">
<option <?php if($myrow['user_level'] == 0){
    echo "selected";
} ?>>0</option>
<option <?php if($myrow['user_level'] == 1){
    echo "selected";
} ?>>1</option>
<option <?php if($myrow['user_level'] == 2){
    echo "selected";
} ?>>2</option>
</select></font></td>
	</tr>
	<tr>
		<td> </td>
		<td>

      <input type="submit" name="submit" value="Change" class="button"></font></td>
	</tr>
	</table>

      </form>
   <p><a href="admin_users.php">Go Back</a></p>
   <?
   
   include 'admindbc.php';
   
    } ?>

   <?
   if ($_POST["submit"])
   {
      $usernameb = $_POST['user_name'];

  if (!$_POST['activated'])
  { $activatedb = "0"; }
  else {
  $activatedb = $_POST['activated'];
  }

  $user_levelb = $_POST['user_level'];

  $sql = "UPDATE users SET user_name='$usernameb', activated='$activatedb',user_level='$user_levelb' WHERE id=$useridb";
  
  
      $result = mysql_query($sql);
      echo "<div id=\"maincontent3\">Thank you! Information updated.";
       echo "<br><br>";
    echo "<a href='index.php'>Admin Main Menu</a> | <a href='admin_users.php'> Manage Users</a></div>";

}
}
?>
</div>

Link to comment
https://forums.phpfreaks.com/topic/167780-solved-php-form-update-problem/
Share on other sites

saying "doesn't work" won't get you many replies except for maybe what do you mean ?

 

So.. what do you mean ?

 

 

I did a quick review (not knowing what to look for but did noticed that it probably doesn't update, this is due to the fact your pulling the data via get (seams fine) but when posting your not passing the id via get but their is one in POST but you don't use it, so maybe try!

 

   if ($_POST["submit"])
   {
      $usernameb = $_POST['user_name'];
      $useridb = $_POST['id']; //Add

What about if you reload the page, does it update..

 

the reason i ask is because your displaying the details before doing the update

heres a quick clean up (also i noticed an extra } at the end, so I added one at the start.. (so you may want to remove the <?php { at the start)

<?php
{


include 'admindbc.php';
$file_name = "admin";
include("headera.php");

if($_SESSION['user_level'] == "0"){
    echo "Only administrators are allowed here!";
    echo "<br><br>";
    echo "<a href='../index.php'>Home</a>";
    exit();
}
include 'admindbc.php';
if ($_POST["submit"])
{
	$usernameb = $_POST['user_name'];

	if (!$_POST['activated'])
	{
		$activatedb = "0";
	}else{
		$activatedb = "1";
	}
	$user_levelb = (int)$_POST['user_level'];
	$sql = "UPDATE users SET user_name='$usernameb', activated='$activatedb',user_level='$user_levelb' WHERE id=$useridb";
	$result = mysql_query($sql);
}
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_GET["id"]))
   {
      $useridb = (int)$_GET["id"];
      $sql = "SELECT * FROM users WHERE id=$useridb";
      $result = mysql_query($sql);
      $myrow = mysql_fetch_array($result);
      ?>
   <div id="maincontent3">
     <h1>Edit Profile</h1>
     <br>
<form action="edit.php" method="post">

      <input type="hidden" name="cmd" value="edit">

      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
        <table>
      <tr>
         <td><b>User ID:</b></td>
         <td><?php echo $myrow["id"] ?></td>
      </tr>
      <tr>
         <td><b>Username:</b></td>
         <td>
         <INPUT NAME="user_name" VALUE="<?php echo $myrow["user_name"] ?>" SIZE=30></font></td>
      </tr>
      
      <tr>
         <td><b>Activated:
         </b></td>
         <td>


   <input class="box" type="checkbox" name="activated" value="1" <?php if($myrow['activated'] == 1) echo "checked"; ?> >

         </font></td>
      </tr>
      <tr>
         <td><b>User Level:
         </b></td>
         <td><select size="1" name="user_level">
   <option <?php if($myrow['user_level'] == 0) echo "selected";?> >0</option>
   <option <?php if($myrow['user_level'] == 1) echo "selected";?> >1</option>
   <option <?php if($myrow['user_level'] == 2) echo "selected";?> >2</option>
   </select></font></td>
      </tr>
      <tr>
         <td> </td>
         <td>

      <input type="submit" name="submit" value="Change" class="button"></font></td>
      </tr>
      </table>

      </form>
   <p><a href="admin_users.php">Go Back</a></p>
<?php
   			include 'admindbc.php';
    	}
	echo "<div id=\"maincontent3\">Thank you! Information updated.";
	echo "<br><br>";
	echo "<a href='index.php'>Admin Main Menu</a> | <a href='admin_users.php'> Manage Users</a></div>";
}


}
?>
</div>

MadTechie - I've tried what you've posted but still no luck, this time it skips the whole form and just heads straight to the echo after it's submitted...

 

The reason why I'm displaying the details before the update it is so that the user can view the info of the selected entity that needs to be edited/updated if that makes sense?

Replace your PHP under the form with this:

 

<?php

if (isset($_POST["submit"]))
{
    $usernameb = $_POST['user_name'];

  if (!$_POST['activated'])
  { $activatedb = "0"; }
  else {
  $activatedb = $_POST['activated'];
  }

  $user_levelb = $_POST['user_level'];

  $sql = "UPDATE users SET user_name='$usernameb', activated='$activatedb',user_level='$user_levelb' WHERE id=$useridb";
  mysql_query($sql) or die(mysql_error());
  
  echo "<div id=\"maincontent3\">Thank you! Information updated.";
  echo "<br><br>";
  echo "<a href='index.php'>Admin Main Menu</a> | <a href='admin_users.php'> Manage Users</a></div>";

}

?>

well after that i get the following:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

also with error reporting enabled i'm getting notices for undefined index

well after that i get the following:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

you may want to echo the query to check it,

 

also with error reporting enabled i'm getting notices for undefined index

Well that means they haven't been set!

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.