2011-07-12 19 views
-2

正在開發一個在線應用程序,我有一個php搜索腳本,可以從數據庫中獲取所需的信息。我已經設法在腳本中包含一個刪除和更新按鈕,這樣當用戶搜索一個項目時,顯示,但我不知道如何將按鈕綁定到他們的功能。與Php新,所以任何幫助表示讚賞。這裏是我的搜索代碼...如何更新和刪除搜索PHP腳本?

<?php 
// Get the search variable from URL 
    $var = @$_GET['s'] ; 
    $trimmed = trim($var); //trim whitespace from the stored variable 

// rows to return 
$limit=15; 

// check for an empty string and display a message. 
if ($trimmed == "") 
    { 
    echo "<p>Please enter a search value...</p>"; 
    exit; 
    } 

// check for a search parameter 
if (!isset($var)) 
    { 
    echo "<p>We dont seem to have a search parameter!</p>"; 
    exit; 
    } 

//connect to database 
mysql_connect("localhost","root",""); //(host, username, password) 

//specify database ** 
mysql_select_db("archive_sys") or die("Unable to select database"); //select which database we're using 

// Build SQL Query 
$query = "select * from tbl_archivingdetails where archiveid like \"%$trimmed%\" or buildingid like \"%$trimmed%\" or branchid like \"%$trimmed%\" or study like \"%$trimmed%\" or batchnumber like \"%$trimmed%\" or quantity like \"%$trimmed%\" or archivedate like \"%$trimmed%\" or archivedby like \"%$trimmed%\" or archiveeemail like \"%$trimmed%\" or archiveephone like \"%$trimmed%\" or expecteddestructiondate like \"%$trimmed%\" or currarchholderproj like \"%$trimmed%\" or currexpretdate like \"%$trimmed%\" or returnedby like \"%$trimmed%\" or status like \"%$trimmed%\""; 

$numresults=mysql_query($query); 
$numrows=mysql_num_rows($numresults); 

// next determine if s has been passed to script, if not use 0 
    if (empty($s)) { 
    $s=0; 
    } 

// get results 
    $query .= " limit $s,$limit"; 
    $result = mysql_query($query) or die("Couldn't execute query"); 

// display what the person searched for 
echo "<h2>You searched for: &quot;" . $var . "&quot;</h2>"; 

// begin to show results set 
echo "Results: "; 
$count = 1 + $s ; 

//the begining of a table with a header 
echo " <table border=2>"; 
echo "<tr align=center>"; 
echo "<th> Check Code </th>"; 
echo "<th> Archive ID </th>"; 
echo "<th> Building ID </th>"; 
echo "<th> Branch ID </th>"; 
echo "<th> Study </th>"; 
echo "<th> Batch Number </th>"; 
echo "<th> Quantity </th>"; 
echo "<th> Archive Date </th>"; 
echo "<th> Archived By </th>"; 
echo "<th> Archivee Email </th>"; 
echo "<th> Archivee Phone </th>"; 
echo "<th> Expected Destruction Date </th>"; 
echo "<th> currArchHolderProj </th>"; 
echo "<th> Current Exp Return Date </th>"; 
echo "<th> Returned By </th>"; 
echo "<th> Status </th>"; 
//echo "<th> Action </th><tr><td><input type='submit' name='Submit' value='Delete' /> | <input type='submit' name='Submit' value='Update' /> </td></tr>"; 
echo " </tr>"; 

// now you can display the results returned 
    while ($row= mysql_fetch_array($result)) { 

    $title = $row["archiveid"]; 
    $title1 = $row["buildingid"]; 
    $title2 = $row["branchid"]; 
    $title3 = $row["study"]; 
    $title4 = $row["batchnumber"]; 
    $title5 = $row["quantity"]; 
    $title6 = $row["archivedate"]; 
    $title7 = $row["archivedby"]; 
    $title8 = $row["archiveeemail"]; 
    $title9 = $row["archiveephone"]; 
    $title10= $row["expecteddestructiondate"]; 
    $title11 = $row["currarchholderproj"]; 
    $title12 = $row["currexpretdate"]; 
    $title13 = $row["returnedby"]; 
    $title14 = $row["status"]; 

echo" <tr>"; 
echo "<td><input type='checkbox' name='checkbox' value='".$row['archiveid']."' id='checkbox'/> </td> <td>".$title."</td> <td>".$title1."</td><td>".$title2."</td><td>".$title3."</td><td>".$title4."</td><td>".$title5."</td> <td>".$title6."</td><td>".$title7."</td><td>".$title8."</td><td>".$title9."</td><td>".$title10."</td> <td>".$title11."</td><td>".$title12."</td><td>".$title13."</td><td>".$title14."</td>" ; 
echo " </tr>"; 
} 

    echo "<tr><tr> <td><input type='submit' name='Submit' value='Delete' /></td> | <td><input type='submit' name='Submit' value='Update' /> </td></tr>"; 
    echo " </tr>"; 

echo " </table>"; 

//break before paging 
    echo "<br />"; 

?> 
+2

我的眼睛。它燃燒。 –

+0

抱歉,Kyle,@ Damien感謝您的編輯。 –

回答

1

你能做到這一點,改變輸入按鈕鏈接和ARCHIVEID追加到的鏈接。現在

echo "<th> Action </th><tr><td><a href='delete.php?archiveid=" . title . '>Delete</a> | <a href='update.php?archiveid=" . title . '>Update</a> </td></tr>"; 

這些鏈接會送你到delete.phpupdate.php分別

下面的例子是爲了簡潔SANS安全,並假設您對數據庫的連接。

//delete.php 

$archiveId = $_GET['archiveid']; 

//now use your db connection to delete the record according to the archiveid 

update.php

//update.php 

$archiveId = $_GET['archiveid']; 

/** 
* Use your db connection to retrieve all the data that relates to this archiveid 
* Populate a form with all the archive details so you can modify them 
* Save the form details to the db when it has been submitted, validated and escaped 
* The query should use an UPDATE statement 
*/ 
+0

Ok Saber,馬上就開始工作...... thnks。 –

+0

一定要在觸及數據庫的任何東西上使用mysql_real_escape_string()。 –

+0

@sabre,如果可以,請刪除,但我如何加載預先填充的表單,以便在點擊更新鏈接時可以編輯並保存回數據庫? –