Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/71806-php-help-need/
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/71806-php-help-need/#findComment-361626
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/71806-php-help-need/#findComment-361628
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/71806-php-help-need/#findComment-361632
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.