Forum Moderators: coopster

Message Too Old, No Replies

Help with editing mysql search results

         

jvance38

7:52 pm on Feb 28, 2011 (gmt 0)

10+ Year Member



Hello. I hope someone out there can help me with this as I have been trying all different ways to make this work but to no avail. I have a report.php page that allows a user to search 3 particular fields in a database to retrieve search results that displays itself on the same page. What I am looking to do is have both an EDIT and DELETE button/link for each queried result that is retrieved allowing the user to edit or delete the search result of their choice. Or, how to implement some inline editing on the results that are retrieved by the search query. Here is the code I have so far with the EDIT or DELETE options feature that I am wanting to utilize.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Budgets Report</title>

</head>
<body class="oneColFixCtr">

<div id="container">

<h2>Budget Report</h2>

<form name="search" method="post" style="background-color:#FFF" action="<?php $PHP_SELF?>">
<span id="search">Search for</span>:
<input type="text" name="find" /> in <Select NAME="field">
<Option VALUE="rundate">rundate</option>
<Option VALUE="section">section</option>
<Option VALUE="reporter">reporter</option> </Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?php

// check to see if anything is posted
if (isset($_POST['find'])) {$find = $_POST['find'];}
if (isset($_POST['searching'])) {$searching = $_POST['searching'];}
if (isset($_POST['field'])) {$field = $_POST['field'];}

//This is only displayed if they have submitted the form
if (isset($searching) && $searching=="yes") {

echo "<h2>Results</h2><p>";

// If they did not enter a search term we give them an error
if (empty($find)) {
echo "<p>You forgot to enter a search term"; exit;
}

// Otherwise we connect to our Database
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("budgets") or die(mysql_error());

// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find); $find = trim ($find);

// Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM daily_budget WHERE upper($field) LIKE'%$find%' ORDER BY section ASC");

// And we display the results
while($result = mysql_fetch_array( $data )) {
echo '<strong>Section:$nbsp</strong>'.$result['section'].'<br>';
echo $result['slug'].' : '.$result['budgetInfo'].' / '.$result['reporter'].' '.$result['notes'].' '.$result['art_photos'].' '.$result['artDesc'].' '.$result['multimedia'].' '.$result['multimediaDesc'].'<br>';
echo 'Pickup : '.$result['pickup'].'<br>';
echo 'Sidebar : '.$result['sidebar'].'<br>';
echo $result['sSlug'].' '.$result['sBudget'].' '.$result['sArt'].' <a href="dbEdit.php"> EDIT</a> | <a href="dbDelete.php">DELETE</a> <br>';
echo '<hr>';
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);

if ($anymatches == 0) {
echo "Sorry, but we can not find an entry to match your query<br><br>"; }

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}

?>
</div>
</div>
</body>
</html>

eelixduppy

4:17 am on Mar 9, 2011 (gmt 0)



Hello and Welcome to WebmasterWorld!

Have you had any luck with finding a solution?

Usually how these things are implemented is you include some type of identifier with each row in the edit/delete URI so that when it goes to that page, it knows which row to edit in the database table. For example,

<a href="dbEdit.php?id=1234"> EDIT</a>


This, of course, must mean something in the database; whatever your primary key is for the table may be a good place to start, so you can uniquely identify a row. Then on your edit page, you simple have a form that when submitted updates the identified row in the db table.

If you need further specifics please let me know, I should be able to find some examples. Please try to implement this yourself, then if you get stuck get back to us.

Happy coding :)