how can i update and delete from a Search php script?
am developing an online application and i have a php search script that fetches required info from the database.I have managed to include a delete and update buttons to the script such that when a user searches for an item a table display of required data is displayed but then i dont know how to bind the buttons to their functionality. Am new with Php so any help is appreciated. Here is my search code...
<?php
// Get the search variable from URL
$var = @$_GET['s'] ;
$trimmed = trim($var); //trim whitespace from the s开发者_运维百科tored 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: "" . $var . ""</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 />";
?>
You could do this, change input buttons to links and append the archiveid to the link.
echo "<th> Action </th><tr><td><a href='delete.php?archiveid=" . title . '>Delete</a> | <a href='update.php?archiveid=" . title . '>Update</a> </td></tr>";
Now these links will send you to delete.php
and update.php
respectively
The examples below are sans security for brevity and will assume that you make connections to the db.
//delete.php
$archiveId = $_GET['archiveid'];
//now use your db connection to delete the record according to the archiveid
and in 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
*/
精彩评论