Jump to content

Help Please! Edit Record Query


One MA

Recommended Posts

Hi - I'm obviously a noob to this but that's why I'm here - I've been using PHP for just over a month and I'm really enjoying it. I'm looking to create an Edit query when I click the edit submit button next to each record in a table. I'm trying to do it all in one script - viewuser.php this is what I have so far - I don't know what is wrong or why it doesn't work - I would really appreciate your help - thanks ever so much in advance - I'm already very grateful for people's help on another script.

[code]<html><head><title>View Users</title></head>
<body>

<?php

session_start();

if (!isset($_SESSION['loggedin'])
   || $_SESSION['loggedin'] !== true) {

   header('Location: login.php');
   exit;
}

$conn=@mysql_connect( "localhost", "alexm", "rugby" )
             or die( "Err:Conn" );

$db = @mysql_select_db( "rugby_project", $conn) or die( "Err:Db" );

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $userID = $_GET["userID"];
      $sql = "SELECT * FROM phone_numbers WHERE userID=$userID";
      $result = mysql_query($sql);        
      $row = mysql_fetch_array($result);
      
echo' <form action="$self" method="post">
      <input type=hidden name="userID" value="<?php echo $row["userID"] ?>">

      User<INPUT TYPE="TEXT" NAME="userID" VALUE=" $row["userID"] >" SIZE=30><br>
      Number<INPUT TYPE="TEXT" NAME="number" VALUE=" $row["number"] >" SIZE=30><br>
      Password<INPUT TYPE="TEXT" NAME="password" VALUE=" $row["password] ?>" SIZE=30><br>
  
      <input type="hidden" name="cmd" value="edit">
  
      <input type="submit" name="submit" value="submit">
  
      </form> ';  
    
    echo( "<center><p><font size=\"1\" face=\"Verdana\">
<a href=\"viewdevices.php\">Back</a>&nbsp;&nbsp;|&nbsp;
<a href=\"control.php\">Return to Control Panel</a>&nbsp;&nbsp;|&nbsp;
<a href=\"logout.php\">Log out<p></a>
</body></html>" );
  }

}
else
{

$sql = "select userID, number, password from phone_numbers order by userID";

$rs = @mysql_query( $sql,$conn);
$self = $_SERVER['PHP_SELF'];
$rows = mysql_num_rows($rs);

echo( "<table width=\"30%\" border=\"1\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bordercolor=\"#000000\">
<tr bgcolor=\"#CCCCCC\">
<td colspan=\"4\"><font size=\"1\" face=\"Verdana\">
<center>User View</center></font></td></tr>" );
   echo( "<tr><td><font size=\"1\" face=\"Verdana\"><strong>User</td>" );
   echo( "<td><font size=\"1\" face=\"Verdana\"><strong>Phone Number</td>" );
   echo( "<td><font size=\"1\" face=\"Verdana\"><strong>Password</td>" );
   echo( "<td><font size=\"1\" face=\"Verdana\"><i><center>Edit</td></tr>" );


if(mysql_num_rows($rs))
{

echo( "<form name=\"editform\" method=\"post\" action=\"$self\">");

while( $row = mysql_fetch_array( $rs ) ){
   $userID=$row["userID"];
   $number=$row["number"];
   $password=$row["password"];
   echo '<tr><td><font size="2" face="Verdana">' . $userID . '</td>';
   echo '<td><font size="2" face="Verdana">' . $number . '</td>';
   echo '<td><font size="2" face="Verdana">' . $password . '</td>';
   echo '<td><center><input type="hidden" name="edit[]" value="' . $userID . '" /><input type="submit" name="edit" value="Edit"></td></tr>';  
}

echo( "</table><p><table width=\"30%\" border=\"1\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bordercolor=\"#000000\">
<tr bgcolor=\"#CCCCCC\">
<td colspan=\"2\"><font size=\"1\" face=\"Verdana\">
<center>There are $rows records in this table</center></font></td></tr></table>" );
  
   echo( "<center><p><font size=\"1\" face=\"Verdana\">
<a href=\"control.php\">Return to Control Panel</a>&nbsp;&nbsp;|&nbsp;
<a href=\"logout.php\">Log out<p></a>
</body></html>" );

}
else
{
echo( "<table width=\"30%\" border=\"1\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bordercolor=\"#000000\">
<tr bgcolor=\"#CCCCCC\">
<td colspan=\"2\"><font size=\"1\" face=\"Verdana\">
<center>This table has no records yet</center></font></td></tr></table>" );
}
}

?>[/code]
Link to comment
Share on other sites

I noticed some pretty stupid mistakes which I didn't see before - I have replaced the errors but the script still doesn't function entirely - i.e. the edit button next to each record doesn't take me into the input form to edit the record... here is the amended code:

[code]
<html><head><title>View Users</title></head>
<body>

<?php

session_start();

if (!isset($_SESSION['loggedin'])
   || $_SESSION['loggedin'] !== true) {

   header('Location: login.php');
   exit;
}

$conn=@mysql_connect( "blah", "blah", "blah" )
             or die( "Err:Conn" );

$db = @mysql_select_db( "rugby_project", $conn) or die( "Err:Db" );

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $userID = $_GET["userID"];
      $sql = "SELECT * FROM phone_numbers WHERE userID=$userID";
      $result = mysql_query($sql);        
      $row = mysql_fetch_array($result);
      
echo' <form action="$self" method="post">
      <input type=hidden name="userID" value="' . $row["userID"] . '">
      User<INPUT TYPE="TEXT" NAME="userID" VALUE="' . $row["userID"] . '" SIZE=30><br>
      Number<INPUT TYPE="TEXT" NAME="number" VALUE="' . $row["number"] . '" SIZE=30><br>
      Password<INPUT TYPE="TEXT" NAME="password" VALUE="' . $row["password"] . '" SIZE=30><br>

      <input type="hidden" name="cmd" value="edit">
  
      <input type="submit" name="submit" value="submit">
  
      </form> ';  
    
    echo( "<center><p><font size=\"1\" face=\"Verdana\">
<a href=\"viewdevices.php\">Back</a>&nbsp;&nbsp;|&nbsp;
<a href=\"control.php\">Return to Control Panel</a>&nbsp;&nbsp;|&nbsp;
<a href=\"logout.php\">Log out<p></a>
</body></html>" );
  }

}
else
{

$sql = "select userID, number, password from phone_numbers order by userID";

$rs = @mysql_query( $sql,$conn);
$self = $_SERVER['PHP_SELF'];
$rows = mysql_num_rows($rs);

echo( "<table width=\"30%\" border=\"1\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bordercolor=\"#000000\">
<tr bgcolor=\"#CCCCCC\">
<td colspan=\"4\"><font size=\"1\" face=\"Verdana\">
<center>User View</center></font></td></tr>" );
   echo( "<tr><td><font size=\"1\" face=\"Verdana\"><strong>User</td>" );
   echo( "<td><font size=\"1\" face=\"Verdana\"><strong>Phone Number</td>" );
   echo( "<td><font size=\"1\" face=\"Verdana\"><strong>Password</td>" );
   echo( "<td><font size=\"1\" face=\"Verdana\"><i><center>Edit</td></tr>" );


if(mysql_num_rows($rs))
{

echo( "<form name=\"editform\" method=\"post\" action=\"$self\">");

while( $row = mysql_fetch_array( $rs ) ){
   $userID=$row["userID"];
   $number=$row["number"];
   $password=$row["password"];
   echo '<tr><td><font size="2" face="Verdana">' . $userID . '</td>';
   echo '<td><font size="2" face="Verdana">' . $number . '</td>';
   echo '<td><font size="2" face="Verdana">' . $password . '</td>';
   echo '<td><center><input type="hidden" name="edit[]" value="' . $userID . '" /><input type="submit" name="edit" value="Edit"></td></tr>';  
}

echo( "</table><p><table width=\"30%\" border=\"1\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bordercolor=\"#000000\">
<tr bgcolor=\"#CCCCCC\">
<td colspan=\"2\"><font size=\"1\" face=\"Verdana\">
<center>There are $rows records in this table</center></font></td></tr></table>" );
  
   echo( "<center><p><font size=\"1\" face=\"Verdana\">
<a href=\"control.php\">Return to Control Panel</a>&nbsp;&nbsp;|&nbsp;
<a href=\"logout.php\">Log out<p></a>
</body></html>" );

}
else
{
echo( "<table width=\"30%\" border=\"1\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bordercolor=\"#000000\">
<tr bgcolor=\"#CCCCCC\">
<td colspan=\"2\"><font size=\"1\" face=\"Verdana\">
<center>This table has no records yet</center></font></td></tr></table>" );
}
}

?>
[/code]
Link to comment
Share on other sites

Guest edwinsweep
hi One Ma are you getting any error, if so what kind?
or a blank page?
you could try adding these to lines to the verry beginning of your php script.

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

this will display(most of) the error's that the php script will encounter.
ill take a look at your script in a minute.

[code]
$conn=@mysql_connect( "blah", "blah", "blah" )
             or die( "Err:Conn" );
[/code]
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.