如何为表中的每一行创建编辑选项?

时间:2017-05-25 14:59:29

标签: php mysql

我使用以下代码显示数据库表中的某些行:

<?php
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];

$searchterm= trim($searchterm);

if (!$searchtype || !$searchterm)
{
   echo 'Error';
   exit;
}

if (!get_magic_quotes_gpc())
{
  $searchtype = addslashes($searchtype);
  $searchterm = addslashes($searchterm);
}

$db = include "connect2db.php"; 

$query = "select * from notes where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);

$num_results = $result->num_rows;

echo '<p>Number of rows found: '.$num_results.'</p>';

for ($i=0; $i <$num_results; $i++)
{
   $row = $result->fetch_assoc(); 
   echo '<i>';
   echo stripslashes($row['date']);
   echo '</i><br /> ';
   echo '<b>';
   echo stripslashes($row['notetitle']);
   echo '</b><br /> ';
   echo stripslashes($row['note']);
   echo '<br /><br /> ';
   echo '</p>';      
}

$result->free();
$db->close();
?>

现在我想为显示的每一行显示一个编辑链接,可以打开一个可以编辑特定行的新页面。我已经有了允许你编辑行的代码:

<?php

  if ($_REQUEST['save']=="Save") { // is data submitted?
    // create variables
    $noteid = $_REQUEST['noteid'];
    $coursename = $_REQUEST['coursename'];
    $notetitle = $_REQUEST['notetitle'];
    $note = $_REQUEST['note'];
    $query = "UPDATE notes SET ";
    $query .= "coursename='$coursename', ";
    $query .= "notetitle='$notetitle', ";
    $query .= "note='$note' ";
    $query .= "WHERE noteid='$noteid'";
    $result = $db->query($query);
} elseif ($_REQUEST['delete']=="Delete") { // is data to be removed?
    $noteid = $_REQUEST['noteid'];
    $query="DELETE FROM notes WHERE noteid='$noteid'";
    $result = $db->query($query);
}

?> 

        <div class="formular">
            <div class="row1">
                <p>Id</p>
                <p>Notetitle</p>
                <p>Note</p>
            </div>


<?php

$query = "SELECT * FROM notes ORDER BY noteid DESC";
$result = $db->query($query);
while ($row = mysqli_fetch_array($result)) {
    echo "<form ".$_SERVER['PHP_SELF']." name='edit-form' method='post' class='row1'>\n";
    echo "<p class='align_top padding_top'>".$row['noteid']."<input type='hidden' name='noteid' value='".$row['noteid']."' /></p>\n";           
    echo "<p class='align_top'><input type='text' name='notetitle' value='".$row['notetitle']."' /></p>\n";     
    echo "<p><textarea name='note' rows='10' cols='50'>".$row['note']."</textarea></p>\n";  
    echo "<p><input type='submit' name='save' value='Save' /></p>";
    echo "<p><input type='submit' name='delete' value='Delete' /></p>";
    echo "</form>\n";

}
echo '</div>';

  $result->free();
  $db->close();
?>

我正在努力的是如何显示每行的编辑链接,让您打开一个页面,您可以在其中编辑/删除该行的内容。 我希望有人可以提供帮助,我对此很新。 谢谢!

1 个答案:

答案 0 :(得分:0)

在每行旁边添加一个按钮,打开编辑页面(或模态),其中包含id,例如:<button onclick="edit('randomId')">Edit RandomId </button>

您可以实现一些不同的东西,接受该特定行的唯一ID,并用它打开一个新的页面或模态。