简体   繁体   中英

UPDATE with form php/mysql

I found this code online and now im trying to make it work. I get an error when I press Edit from the first page.

Notice: Undefined variable: id in edit_form2.php on line 19

I'm not sure why this variable isn't included in the code, where should it go? or does it have something to do with my table?

Database = album 
Table = data_employees

1. id         primaray key & a_i
2. name
3. address

When I press the edit button the next page should load the existing data from the database. Now it just turns up empty + the error code I wrote. What should I do?

edit.php

<td align="center">DATA</td>
</tr>
<tr>
<td>
  <table border="1">
  <?php
  include"dbinc.php";//database connection
  $order = "SELECT * FROM data_employees";
  $result = mysql_query($order);
  while ($row=mysql_fetch_array($result)){
    echo ("<tr><td>$row[name]</td>");
    echo ("<td>$row[employees_number]</td>");
    echo ("<td>$row[address]</td>");
    echo ("<td><a href=\"edit_form2.php?id=$row[employees_number]\">Edit</a></td></tr>");
  }
  ?>
  </table>

edit_form2.php

  <?php
  include "dbinc.php";//database connection
  $order = "SELECT * FROM data_employees 
            where employees_number='$id'";
  $result = mysql_query($order);
  $row = mysql_fetch_array($result);
  ?>
  <form method="post" name=form action="edit_data2.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>

edit_data2.php

<?php
//edit_data.php
include "dbinc.php";

$name = $_POST["name"];
$address = $_POST["address"];
$id = $_POST["id"];

$order = "UPDATE data_employees 
      SET name='$name', 
          address='$address' 
      WHERE 
      employees_number='$id'";
mysql_query($order);
header("location:edit.php");
?>

$id variable must be declared before use; like-

$id = mysql_real_escape_string($_GET['id']);

This line is wrong

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

try

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

So quite likely as the input field does not get built properly the browser is ignoring that field completely.

edit.php

replace

echo ("<tr><td>$row[name]</td>");
echo ("<td>$row[employees_number]</td>");
echo ("<td>$row[address]</td>");
echo ("<td><a href=\"edit_form2.php?id=$row[employees_number]\">Edit</a></td></tr>");

with

echo "<tr><td>".$row['name']."</td>";
echo "<td>".$row['employees_number']."</td>";
echo "<td>".$row['address']."</td>";
echo "<td><a href=\"edit_form2.php?id=".$row['employees_number']."\">Edit</a></td></tr>";

edit_form.php

<form method="post" name=form action="edit_data2.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>

should be

 <?php
    $id = mysql_real_escape_string($_GET['id']);include "dbinc.php";//database connection
   $order = "SELECT * FROM data_employees 
        where employees_number='$id'";
   $result = mysql_query($order);
   $row = mysql_fetch_array($result);
 ?>

    <form method="post" name=form action="edit_data2.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>

and like Mukesh Soni said, you need to grab te id from the url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM