dsrw29618 2014-06-11 19:37
浏览 17
已采纳

编辑表中的数据

I have a table with an edit button(1 per each product) that when clicked I want it to edit that particular row..

This is my table

<?php
    require 'connect.php';

    $query = $link->query("SELECT * FROM fornecedor");

    echo "<table border='1' cellpadding='2' cellspacing='2'";

    echo "<tr>
            <td>Nome</td>
            <td>NIF</td>
            <td>Cidade</td>
            <td>Rua</td>
            <td>NrPorta</td>
            <td>Website</td>
            <td>email</td>
            <td>Editar</td>
            <td>Eliminar</td>
            <td>Adicionar</td>
        </tr>"; 

        while ($row = $query->fetch_array() ) {

            echo "<tr>";
            echo "<td>" . $row["Nome"] . "</td>";
            echo "<td>" . $row["NIF"] . "</td>";
            echo "<td>" . $row["Cidade"] . "</td>";
            echo "<td>" . $row["Rua"] . "</td>";
            echo "<td>" . $row["NrPorta"] . "</td>";
            echo "<td>" . $row["Website"] . "</td>";
            echo "<td>" . $row["Email"] . "</td>";
            echo "<td><a href='editarFornecedor.php'><img src='images/edit.svg' width='40px' height='30px'></a></td>";
            echo "<td><a href='eliminarFornecedor.php'><img src='images/delete.png' width='30px' height='30px'></a></td>";
            echo "</tr>";  
        };
          echo "</table>";

          $link->close();

?>

And this is my edit page:

include 'connect.php';

$result = mysqli_query($link,"SELECT * FROM Fornecedor");

while($row = mysqli_fetch_array($result))

?>

  <form method="post" action="edit_data.php">
  <input type="text" name="id" value="<?php echo "$row[id]"?>">
    <tr>        
      <td>Nome</td>
      <td>
        <input type="text" name="Nome" size="40" value="<?php echo "$row[Nome]"?>">
      </td>
    </tr>
    <tr>
      <td>Nif</td>
      <td>
        <input type="text" name="Nif" size="40"  value="<?php echo "$row[NIF]"?>">
      </td>
    </tr>
    <tr>
      <td>Cidade</td>
      <td>
       <input type="text" name="Cidade" size="40" value="<?php echo "$row[Cidade]"?>">
      </td>
    </tr>
    <tr>
      <td>Rua</td>
      <td>
        <input type="text" name="Rua" size="40" value="<?php echo "$row[Rua]"?>">
      </td>
    </tr>
    <tr>
      <td>NrPorta</td>
      <td>
        <input type="text" name="NrPorta" size="40"  value="<?php echo "$row[NrPorta]"?>">
      </td>
    </tr>
    <tr>
      <td>Website</td>
      <td>
        <input type="text" name="Website" size="40"  value="<?php echo "$row[Website]"?>">
      </td>
    </tr>
    <tr>
      <td>Email</td>
      <td>
        <input type="text" name="Email" size="40" value="<?php echo "$row[Email]"?>">
      </td>
    </tr>
    <tr>
      <td align="right">
        <input type="submit" name="submit value" value="Edit">
      </td>
    </tr>
  </form>
  </table>


<?php mysqli_close($link);?> 

I only see the form without values , and i wanted to make them visible so the user can edit the data. What am i doing wrong ? bad coding or something missing?

  • 写回答

5条回答 默认 最新

  • duanpanyang1962 2014-06-11 19:40
    关注

    All of your $row values are something like this:

    $row[Nome]
    

    When they should have single quotes like this:

    $row['Nome']
    

    Additionally your echo lines within the HTML are like this:

    <input type="text" name="Email" size="40" value="<?php echo "$row[Email]"?>">
    

    But those " double quotes are not needed, so it should be like this:

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

    Here is my attempt to clean up the issues mentioned above. I believe it should work:

      <form method="post" action="edit_data.php">
      <input type="text" name="id" value="<?php echo $row['id'] ?>">
        <tr>        
          <td>Nome</td>
          <td>
            <input type="text" name="Nome" size="40" value="<?php echo $row['Nome'] ?>">
          </td>
        </tr>
        <tr>
          <td>Nif</td>
          <td>
            <input type="text" name="Nif" size="40"  value="<?php echo $row['NIF'] ?>">
          </td>
        </tr>
        <tr>
          <td>Cidade</td>
          <td>
           <input type="text" name="Cidade" size="40" value="<?php echo $row['Cidade'] ?>">
          </td>
        </tr>
        <tr>
          <td>Rua</td>
          <td>
            <input type="text" name="Rua" size="40" value="<?php echo $row['Rua'] ?>">
          </td>
        </tr>
        <tr>
          <td>NrPorta</td>
          <td>
            <input type="text" name="NrPorta" size="40"  value="<?php echo $row['NrPorta'] ?>">
          </td>
        </tr>
        <tr>
          <td>Website</td>
          <td>
            <input type="text" name="Website" size="40"  value="<?php echo $row['Website'] ?>">
          </td>
        </tr>
        <tr>
          <td>Email</td>
          <td>
            <input type="text" name="Email" size="40" value="<?php echo $row['Email'] ?>">
          </td>
        </tr>
        <tr>
          <td align="right">
            <input type="submit" name="submit value" value="Edit">
          </td>
        </tr>
      </form>
      </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制