3

I would like to be able to view and edit information contained within a table from my web browser however I can't for the life in me get the current values to pull though to an html text field.

Can anyone shed any light as im quite new to php?

Table name: request_details

Column Names: id, name, email_address

My PHP code is:

<?
$order = "SELECT * FROM request_details WHERE id='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>

HTML Code

<form method="post" action="edit_data.php">
  <input type="hidden" name="id" value="<?php echo "$row[id]"?>">
    <tr>        
      <td>Name</td>
      <td>
        <input type="text" name="name" size="20" value="<?php echo "$row[name]"?>">
      </td>
    </tr>
    <tr>
      <td>Email Address</td>
      <td>
        <input type="text" name="email_address" size="40" value="<?php echo "$row[email_address]"?>">
      </td>
    </tr>
    <tr>
      <td align="right">
        <input type="submit" 
      name="submit value" value="Edit">
      </td>
    </tr>
  </form>

Right to make it a bit easier, when I use an actual ID not a variable it works, so for example

<?
  include "db.inc.php";//database connection
  $order = "SELECT * FROM request_details WHERE id='19'";
  $result = mysql_query($order);
  $row = mysql_fetch_array($result,MYSQL_ASSOC);
  ?>

The above displays, yet the below does not

<?
  include "db.inc.php";//database connection
  $order = "SELECT * FROM request_details WHERE id='$id'";
  $result = mysql_query($order);
  $row = mysql_fetch_array($result,MYSQL_ASSOC);
  ?>

Ok, so the premise is I have two web pages, the first displays all the information I require

code is:

<table>
    <tr>
        <td>
            <table border="1">
                <tr>
                    <td>Name</td>
                    <td>Telephone Number</td>
                    <td>Email Address</td>
                    <td>Venue</td>
                    <td>Event Date</td>
                    <td>Guests</td>
                    <td>Chair Cover Colour</td>
                    <td>Sash Colour</td>
                    <td>Price</td>
                    <td>Damage Deposit</td>
                    <td>Status</td>
                    <td>Notes</td>
                </tr>
                <tr>      

                    <?
                    $order = "SELECT * FROM request_details";
                    $result = mysql_query($order);
                    while ($row=mysql_fetch_array($result)){

                    echo ("<tr><td>$row[name]</td>");
                    echo ("<td>$row[telephone_number]</td>");
                    echo ("<td>$row[venue]</td>");
                    echo ("<td>$row[event_date]</td>");
                    echo ("<td>$row[guests]</td>");
                    echo ("<td>$row[cover_colour]</td>");
                    echo ("<td>$row[sash_colour]</td>");
                    echo ("<td>$row[price]</td>");
                    echo ("<td>$row[damage_deposit]</td>");
                    echo ("<td>$row[notes]</td>");      
                    echo ("<td><a href=\"edit_form.php?id=$row[id]\">View</a></td></tr>");
                    }
                    ?>
            </table>
        </td>
    </tr>
</table>

When i click on view it takes me to the following url:

edit_form.php?id=1

for example

edit_form.php?id=19

The code for this page (where the information isn't displaying in the text field):

<table border=1>
    <tr>
        <td>
            <table>

                <?
                $order = "SELECT * FROM request_details WHERE id='$id'";
                $result = mysql_query($order);
                $row = mysql_fetch_array($result,MYSQL_ASSOC);
                ?>

                <form method="post" action="edit_data.php">
                    <tr>
                    <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
                    <tr>        
                        <td>Name</td>
                        <td>
                            <input type="text" name="name" size="20" value="<?php echo $row['name']; ?>">
                        </td>
                    </tr>
                    <tr>
                        <td>Email Address</td>
                        <td>
                            <input type="text" name="email_address" size="40" 
                                   value="<?php echo $row['email_address']; ?>">
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <input type="submit" name="submit value" value="Edit">
                        </td>
                    </tr>
                </form>
            </table>      
        </td>
    </tr>
</table>
5
  • Have you turned on error reporting? Do you get any errors? What troubleshooting steps have you done to identify the issue? Commented Aug 26, 2013 at 12:06
  • 2
    i think you forgot the quotes around the row index name.. $row["name"] not $row[name] Commented Aug 26, 2013 at 12:06
  • 4
    Your code is exceptionally vulnerable to attack using it as it is. You really should investigate PDO (php.net/manual/en/book.pdo.php) and prepared statements. This is like leaving your car windows open while you leave your valuables in the car and go to work. Commented Aug 26, 2013 at 12:09
  • To debug try: var_dump($row); what result you get? Commented May 5, 2020 at 0:17
  • What exactly is not working with the given code? Is this a PHP problem, an HTML problem, or a MySQL problem? Commented May 5, 2023 at 12:03

6 Answers 6

-1

Change your php code to

$row = mysql_fetch_array($result,MYSQL_ASSOC);

Also change the HTML code like the following line

    <input type="hidden" name="id" value="<?php echo $row['id'] ;?>" />
Sign up to request clarification or add additional context in comments.

Comments

-1

You can do like this :

<input type="text" name="email_address" size="40" value="<?=$row['email_address']?>">

Comments

-1

Your code is incomplete, it misses a definition within the call of the row. It doesnt know $row[name] because the variable should be defined by quotations.

<input type="text" name="name" size="20" value="<? echo $row['name']; ?>">
<input type="text" name="email_address" size="40" value="<? echo $row['email_address']; ?>">

Next to that, you are quoting on a variable, which is not needed. A variable doesnt have to be quoted at all. You quote the value type correctly, the double quote doesnt make sense.

Your code is really messy btw. Try putting it correctly down with the appropiate syntax. (dont forget ; behind the variable)

Comments

-1

do them like that

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

  <input type="text" name="name" size="20" value="<?php echo $row['name']; ?>" />

  <input type="text" name="email_address" size="40" value="<?php echo $row['email_address']; ?>" />

edit;

sorry to say that your code is catastroph and messed up . many errors there i cant fix all. any way change your line to this line.

     echo "<td><a href=\"edit_form.php?id=$row['id']\">View</a></td></tr>"; 

and add in the file where you want display edit page , before your query this line

  $id = $_GET['id'] ;

9 Comments

@user2717967 then the problem is not here , this is how you echo values from database. i guesss your problem is in the $id in your query. did you tried to echo it and see if there is value ?
i have tried to just echo and display in a <td></td> tag but no joy there either, so im presuming it is something rather simple that i've missed
i said just try echo $id ; before your query and see if there is something echoed.
then you have no $id , look where you are taking this $di , your problem is with this id. thats why your query its like you say WHERE id = nothing
From the previous page the code is echo ("<td><a href=\"edit_form.php?id=$row[id]\">View</a></td></tr>"); id as defined as the column name in the database
|
-2

i made corrections for the hole code and it works

<html>
<head>
<title>PAGE Edit</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table>
  <tr>
    <td align="center">EDIT DATA</td>
  </tr>
  <tr>
    <td>
      <table border="1">
      <?php
      include"db.inc.php";//database connection
      $order = "SELECT * FROM data_employees";
      $result = mysql_query($order);
      while ($row=mysql_fetch_array($result)){
      // Print "<th>Name:</th> <td>".$data['user'] . "</td> "; 
        echo ("<tr><td>".$row['name']."</td>");
        echo ("<td>".$row['employees_number']."</td>");
        echo ("<td>".$row['address']."</td>");
        echo ("<td><a href=edit_form.php?id=".$row['employees_number'].">Edit</a></td></tr>"); 
      }
      ?>
      </table>
    </td>
   </tr>
</table>
</body>
</html>

Second PART : Calling edit_form.php to edit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>

<body>
<table border=1>
  <tr>
    <td align=center>Form Edit Employees Data</td>
  </tr>
  <tr>
    <td>
      <table>
      <?php
      $id=$_GET['id']; 

      include "db.inc.php";//database connection
      $order = "SELECT * FROM data_employees 
where employees_number='$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[employees_number]"?>">
        <tr>        
          <td>Name</td>
          <td>
            <input type="text" name="name" 
        size="20" value="<?php echo "$row[name]"?>">
          </td>
        </tr>
        <tr>
          <td>Address</td>
          <td>
            <input type="text" name="address" size="40" 
          value="<?php echo "$row[address]"?>">
          </td>
        </tr>
        <tr>
          <td align="right">
            <input type="submit" 
          name="submit value" value="Edit">
          </td>
        </tr>
      </form>
      </table>
    </td>
  </tr>
</table>
</body>
</html>

Finally calling edit_Data.php to update the DATA TABLE

<?php
//edit_data.php
$name=$_POST['name'];
$address=$_POST['address'];
$id=$_POST['id'];
include "db.inc.php";
$order = "UPDATE data_employees 
          SET name='$name', 
              address='$address' 
          WHERE 
          employees_number='$id'";
mysql_query($order);
header("location:edit.php");
?>

I hope that it's clear now ! :D

1 Comment

Please don't recommend the usage of insecure SQL queries
-2

If your URL is edit_form.php?id=19

You need to write your query like:

$order = "SELECT * FROM request_details WHERE id='$_GET[id]'";

1 Comment

Please don't recommend the usage of insecure SQL queries

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.