2017-05-25 37 views
1

我使用下面的代碼從我的數據庫表中顯示某些行:如何爲表中的每一行創建編輯選項?

<?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(); 
?> 

什麼我掙扎是如何顯示一個編輯鏈接,每一個,讓你打開一個頁面,您可以編輯行/刪除的內容只有那一排。 我希望有人能幫忙,我對此很新。 謝謝!

回答

0

添加旁邊打開一個編輯頁面(或模式),每行按鍵與id內,例如:<button onclick="edit('randomId')">Edit RandomId </button>

您可以實現不同的東西,接受特定行的唯一ID,並打開一個新的頁面或模式與它。

+0

謝謝。這就說得通了。所以如果我在每一行旁邊添加一個按鈕,我該如何創建一個函數來打開一個來自該特定行的數據的頁面? – Kaja

+0

使用GET或POST傳遞id,然後在php頁面中獲取id以使用它。 – Galeaettu

相關問題