Tinny Posted October 4, 2007 Share Posted October 4, 2007 I have been learning sql/php for about 3 weeks now and have started my first project which is a database to track assets with a web front end. i have completed the most part of it and now looking to introduce some enhancements to it. A few problems im having at the moment is : 1. i have an if statement for searching entries, Basically the user has an input form where they can type in search critera. at the moment there is an if statement that if the input box is left blank an error message pops up but i want to introduce another if statement so if the user inputs somehting that isnt in the database the same error message appears. This is how i have done it, but it doesnt work example of the code <.? $query = "SELECT * FROM table WHERE column LIKE '%".$searchitem."%'"; If (!empty($searchitem) OR ($searchitem) { search results: Else Error Message ?> the if did look like this: If (!empty($searchitem) OR (!$searchitem) { but it didnt make sense as ! = not. so it couldnt be if searchitem not empty or not in database, search results. 2. the other thing that i need help on is when i do a search results i want each result to have its own edit button so you click it and it goes staight to the edit page for that particular result, the way i have it at the moment it you have to type what you want to edit. i have been using sessions for this but havnt got it to work. If anyone could help me on these problems, it would be truly appriciated. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/71806-php-help-need/ Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 useful command mysql-num-rows example <?php $query = "SELECT * FROM table WHERE column LIKE '%$searchitem%'"; $result = mysql_query($query); $num_rows = mysql_num_rows($result); //number of records found If ($num_rows > 0) { // search results: }else{ // Error Message } ?> and welcome to the board as a side note, a better subject helps (ie display error if nothing is found) Quote Link to comment https://forums.phpfreaks.com/topic/71806-php-help-need/#findComment-361626 Share on other sites More sharing options...
HuggieBear Posted October 4, 2007 Share Posted October 4, 2007 This should answer the first part of your post... <?php // If statement to make sure the search criteria isn't empty if (empty($searchitem)) { // Error message goes here } // SQL query to run $sql = "SELECT * FROM table WHERE column LIKE '%" . $searchitem . "%'"; $result = mysql_query($sql) or die ("Can't execute query:" . mysql_error()); // Make sure there were rows returned if (mysql_num_rows($result) == 0) { // Error message here } else { // List found data here } ?> As for the second part, you just need to retrieve the unique ID from the database and use that to link through to your edit page (via $_GET). Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/71806-php-help-need/#findComment-361628 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 extra info.. in addition to HuggieBear's message in the displayed list (search results) you can add a hyperlink with the ID ie $ID = $row['ID']; echo "<a href='edit.php?id=$ID'>edit</a>"; then in edit.php have something like $ID = (int)$_GET['id']; $sql = "SELECT * FROM table WHERE ID =$ID"; $result = mysql_query($sql) or die ("Can't execute query:" . mysql_error()); EDIT: updated (messed up with a Caps LOL) Quote Link to comment https://forums.phpfreaks.com/topic/71806-php-help-need/#findComment-361632 Share on other sites More sharing options...
Tinny Posted October 4, 2007 Author Share Posted October 4, 2007 cheers guys, i will just grab some lunch, then i will try what you guys have put and let you know what happens. wish me luck lol Quote Link to comment https://forums.phpfreaks.com/topic/71806-php-help-need/#findComment-361639 Share on other sites More sharing options...
Tinny Posted October 5, 2007 Author Share Posted October 5, 2007 Hi, i have almost got it working(i used huggiebears example) - well it works but its it messing up my page by moving my divs round. i think this could be my echo doing it but not sure i also tried using pring instead of echo but it didnt doing anything. any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/71806-php-help-need/#findComment-362277 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.