Editting data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
captain_scarlet87
Forum Newbie
Posts: 7
Joined: Thu Mar 11, 2010 3:27 pm

Editting data

Post by captain_scarlet87 »

Hi,

I have 3 bits of code which are suppose to display all the current usernames from a table and alongside the usernames a button can be clicked to edit that specific username.

So far all the usernames are being displayed correctly however when clicking to edit the edit_form.php page displays the text box without the current username in there to edit it. I've tried just entering in a different username but it just takes me back to the edit.php page with the username unchanged.

Any suggestions where I have gone wrong?

edit.php:

Code: Select all

<?php # Script 13.8 - edit.php
 
// Include the configuration file for error management and such.
require_once ('./includes/config.inc.php');
 
// Set the page title and include the HTML header.
$page_title = 'Users';
include ('./includes/header.html');
?>
 
<table>
      <tr>
        <td align="center">Edit Users</td>
      </tr>
      <tr>
        <td>
          <table border="1">
          <?php
          require_once ('../mysql_connect.php'); // Connect to the database.
          $order = "SELECT username FROM users";
          $result = mysql_query($order);
          while ($row=mysql_fetch_array($result)){
            echo ("<tr><td>$row[username]</td>");
            echo ("<td><a href=\"edit_form.php?id=$row[username]\">Edit</a></td>");
            echo ("<td><a href=\"delete.php?id=$row[username]\">Delete</a></td></tr>");
          }
          ?>
          </table>
        </td>
       </tr>
    </table>
 
<?php
include ('./includes/footer.html');
?>
edit_form.php:

Code: Select all

<table border=1>
          <tr>
            <td align=center>Form Edit Employees Data</td>
          </tr>
          <tr>
            <td>
              <table>
              <?php
              require_once ('../mysql_connect.php'); // Connect to the database.
              $order = "SELECT username FROM users
        where username='$id'";
 
              $result = mysql_query($order);
              $row = mysql_fetch_array($result);
              ?>
              <form method="post" action="edit_data.php">
              <input type="hidden" name="id" value="<?php echo "$row[username]"?>">
                <tr>
                  <td>Username</td>
                  <td>
                    <input type="text" name="username"
                size="20" value="<?php echo "$row[username]"?>">
                  </td>
                </tr>
                <tr>
                  <td align="right">
                    <input type="submit"
                  name="submit value" value="Edit">
                  </td>
                </tr>
              </form>
              </table>
            </td>
          </tr>
    </table>
edit_data.php:

Code: Select all

<?php
    //edit_data.php
    require_once ('../mysql_connect.php'); // Connect to the database.
    $order = "UPDATE users
              SET username='$username',
 
              WHERE
              username='$id'";
    mysql_query($order);
    header("location:edit.php");
    ?>
User avatar
StathisG
Forum Newbie
Posts: 14
Joined: Sat Mar 13, 2010 7:15 pm
Location: UK

Re: Editting data

Post by StathisG »

As I can see, you're not retrieving the data you're sending:

edit_form.php:

Code: Select all

$id = mysql_real_escape_string($_GET['id']);
edit_data.php:

Code: Select all

$username = mysql_real_escape_string($_POST['username']);
I would also propose to add a user_id field (primary key, auto increment) in your users database and use this as an identifier for your users.
captain_scarlet87
Forum Newbie
Posts: 7
Joined: Thu Mar 11, 2010 3:27 pm

Re: Editting data

Post by captain_scarlet87 »

I add those two lines of code but get these errors come up:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\html\edit_form.php on line 9

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\wamp\www\html\edit_form.php on line 9

I'm using this to connect to the database which obviously works as I use it in edit.php to retrieve the usernames and display them:

require_once ('../mysql_connect.php'); // Connect to the database.

Know why this is happening?

Thanks.
User avatar
StathisG
Forum Newbie
Posts: 14
Joined: Sat Mar 13, 2010 7:15 pm
Location: UK

Re: Editting data

Post by StathisG »

captain_scarlet87 wrote:I add those two lines of code but get these errors come up:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\html\edit_form.php on line 9

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\wamp\www\html\edit_form.php on line 9

I'm using this to connect to the database which obviously works as I use it in edit.php to retrieve the usernames and display them:

require_once ('../mysql_connect.php'); // Connect to the database.

Know why this is happening?

Thanks.
If you have the connection to your database in a variable (e.g. $link) place it in there:

Code: Select all

$id = mysql_real_escape_string($_GET['id'], $link);
Anyway, if you remove the mysql_real_escape_string() it will work but you have to escape the data for security reasons.
captain_scarlet87
Forum Newbie
Posts: 7
Joined: Thu Mar 11, 2010 3:27 pm

Re: Editting data

Post by captain_scarlet87 »

Hi,

I've managed to solve that little issue and I've now got the current username filling in the text box to edit. However when I edit it, it's still not changing in the db or displaying as changed on the edit.php page. At least i'm one step closer just need to sort this last bit out which should only be a simple problem. Can anyone spot anything wrong?

Current code:
edit.php:

Code: Select all

<?php # Script 13.8 - edit.php
 
// Include the configuration file for error management and such.
require_once ('./includes/config.inc.php');
 
// Set the page title and include the HTML header.
$page_title = 'Users';
include ('./includes/header.html');
?>
 
<table>
      <tr>
        <td align="center">Edit Users</td>
      </tr>
      <tr>
        <td>
          <table border="1">
          <?php
          require_once ('../mysql_connect.php'); // Connect to the database.
          $order = "SELECT username FROM users";
          $result = mysql_query($order);
          while ($row=mysql_fetch_array($result)){
            echo ("<tr><td>$row[username]</td>");
            echo ("<td><a href=\"edit_form.php?id=$row[username]\">Edit</a></td>");
            echo ("<td><a href=\"delete.php?id=$row[username]\">Delete</a></td></tr>");
          }
          ?>
          </table>
        </td>
       </tr>
    </table>
 
<?php
include ('./includes/footer.html');
?>
edit_form.php:

Code: Select all

<table border=1>
          <tr>
            <td align=center>Form Edit Employees Data</td>
          </tr>
          <tr>
            <td>
              <table>
              <?php
              require_once ('../mysql_connect.php'); // Connect to the database.
              $id = mysql_real_escape_string($_GET['id']);
              $order = "SELECT username FROM users
                        where username='$id'";
 
              $result = mysql_query($order);
              $row = mysql_fetch_array($result);
              ?>
              <form method="post" action="edit_data.php">
              <input type="hidden" name="id" value="<?php echo "$row[username]"?>">
                <tr>
                  <td>Username</td>
                  <td>
                    <input type="text" name="username"
                size="20" value="<?php echo "$row[username]"?>">
                  </td>
                </tr>
                <tr>
                  <td align="right">
                    <input type="submit"
                  name="submit value" value="Edit">
                  </td>
                </tr>
              </form>
              </table>
            </td>
          </tr>
    </table>
 
edit_data.php:

Code: Select all

<?php
    //edit_data.php
    require_once ('../mysql_connect.php'); // Connect to the database.
    $username = mysql_real_escape_string($_POST['username']);
    $order = "UPDATE users
              SET username='$username',
 
              WHERE
              username='$id'";
    mysql_query($order);
    header("location:edit.php");
    ?>
User avatar
StathisG
Forum Newbie
Posts: 14
Joined: Sat Mar 13, 2010 7:15 pm
Location: UK

Re: Editting data

Post by StathisG »

edit_data.php:

Code: Select all

WHERE username='$id'";
You're not getting the value from the request.. Please read carefully your code before asking a question :)
krishyy236
Forum Newbie
Posts: 2
Joined: Mon Mar 15, 2010 1:49 pm

Re: Editting data

Post by krishyy236 »

@captain_scarlet87

I need help in display the userinfo in the next page with the login page...for the user...Would you help in this??

denlion
Post Reply