BetaArchive Logo
Navigation Home Screenshots Image Uploader Server Info FTP Servers Wiki Forum RSS Feed Rules Please Donate
UP: 51d, 11h, 57m | CPU: 12% | MEM: 5277MB of 10559MB used
{The community for beta collectors}

Forum rules


Any off topic discussions should go in this forum. Post count is not increased by posting here.
FTP Access status is required to post in this forum. Find out how to get it


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPost subject: MySQL search with PHP        Posted: Mon Jul 20, 2009 5:44 am 
Reply with quote
Donator
User avatar
Offline

Joined
Fri Aug 18, 2006 4:30 pm

Posts
1524

Favourite OS
Mac OS 9.2.2
So yeah, I've got this database set up where I can enter in details about my refurbished systems, which I'm required to do under the Community MAR program.

The data entry part works fine, as you can see in the screenshot here...
Image

Those details are entered into a MySQL database, which although messy, works fine.
Image


The part that I can't get to work at all is the search form. It is comprised of 2 parts, an html frontend, and the php backend.

The search function is supposed to look for a 5 character number located in "field_1" and then display that column .
html code]
<form method="post" action="search.php">
<input type="text" name="search" size=25 maxlength=25>
<input type="Submit" name="Submit" value="Submit">
</form>
[/quote]

For example, I want a search for "C5001" to return the first result. These number won't be in a logical order eventually, thats why I want to put in a search function.


and here is the php code that I cannot get to work at all. It just <?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","mypassword");

//select which database you want to edit
mysql_select_db("assets1");

$search=$_POST["search wrote:
;

//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * FROM field_1 WHERE message LIKE '%$search%'");

//grab all the content
while($r=mysql_fetch_array($result))
{
//the format is $variable = $r["nameofmysqlcolumn"];
//modify these to match your mysql table columns

$fieldBox_1=$r["Fireware computers Asset Tag"];
$fieldBox_2=$r["System Manufacturer"];
$fieldBox_3=$r["Model"];
$fieldBox_4=$r["System Specifications"];
$fieldBox_5=$r["Condition"];
$fieldBox_6=$r["System Serial"];
$fieldBox_7=$r["Corporate Asset Tag"];
$fieldBox_8=$r["Acquired Date"];
$fieldBox_9=$r["Current Owner"];
$id=$r["id"];

//display the row
echo "$$fieldBox_1 <br> $fieldBox_2 <br> $fieldBox_3 <br> $fieldBox_4 | $fieldBox_5 <br> $fieldBox_6 <br> $fieldBox_7 <br> $fieldBox_8 <br> $fieldBox_9 <br>";
}
?>

[quote="php code" ]
[/quote][/quote][/quote]


and the error I get when trying to search for C5001 is

"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\search_orig.php on line 21"

What am I suppposed to do to make this work? I've tried googling the error, and it leads to nothing. I have no clue what I'm doing wrong :S.

EDIT: I've removed the "Box" parts from the php file, which is supposedly correct, as field_1 is my table column label, and not fieldBox_1, and it still doesnt work. I don't get any errors anymore, just a blank screen. The source for the blank screen is just <br> <br> <br> <br> etc etc. That makes it look like something worked, but I still can't figure out what's wrong.

EDIT 2: here's where I got the search code : http://www.spoono.com/php/tutorials/tutorial.php?id=12

_________________
Image
Mozilla/5.0 (Macintosh; U; PPC; en-US; mimic; rv:9.3.2) Clecko/20120101 Classilla/CFM
"Stupid can opener! You killed my father, and now you've come back for me!"


Top  Profile
 PostPost subject: Re: MySQL search with PHP        Posted: Tue Jul 21, 2009 2:54 pm 
Reply with quote
FTP Access
User avatar
Offline

Joined
Sun Oct 05, 2008 3:11 pm

Posts
105
as far as i can see your query is wrong.

You table is named "assets" and not "field_1".

$result = mysql_query("SELECT * FROM assets WHERE message LIKE '%$search%'");

I also can not found any column named "message" in your screenshot - just those coloumns called "field_1", "field_2", ... no "message".

Furthermore I guess you have to the $fieldBox_# lines are wrong, too.
Because you have to use ... = $r["columnName"]; - so e.g.
$fieldBox_1=$r["field_1"];
$fieldBox_2=$r["field_2"];
and so on.

I hope I can help you with this.

_________________
GreetZz, Sibbl

Image


Top  Profile
 PostPost subject: Re: MySQL search with PHP        Posted: Tue Jul 21, 2009 7:51 pm 
Reply with quote
Donator
User avatar
Offline

Joined
Fri Aug 18, 2006 4:30 pm

Posts
1524

Favourite OS
Mac OS 9.2.2
OK, so I've fixed the first problem. the top part of the script works fine.

Now I can't get anything to display on the result page. Entering C5001 shows nothing except <br> and | . This makes me think that the echo command isn't parsed correctly.

When I replaced
Quote:
//display the row
echo "$$fieldBox_1 <br> $fieldBox_2 <br> $fieldBox_3 <br> $fieldBox_4 | $fieldBox_5 <br> $fieldBox_6 <br> $fieldBox_7 <br> $fieldBox_8 <br> $fieldBox_9 <br>";

with
Quote:
echo "Success";

and searched for c5001, it shows Success, which means the script is working.

I'm willing to bet the "display the row" part isn't correct, but I don't know how to display them right...

_________________
Image
Mozilla/5.0 (Macintosh; U; PPC; en-US; mimic; rv:9.3.2) Clecko/20120101 Classilla/CFM
"Stupid can opener! You killed my father, and now you've come back for me!"


Top  Profile
 PostPost subject: Re: MySQL search with PHP        Posted: Tue Jul 21, 2009 8:40 pm 
Reply with quote
Administrator
User avatar
Offline

Joined
Fri Aug 18, 2006 11:47 am

Posts
12473

Location
Merseyside, United Kingdom

Favourite OS
Microsoft Windows 7 Ultimate x64
Split it into several echo's so you can see which one causes the problem. For one thing I've never understood when PHP likes to output variables inside an echo and when it doesn't, so when you do the echo make sure you keep the variables seperate from the text. I bet thats what is causing it.

_________________
Image

BetaArchive Discord: https://discord.gg/epK3r6A


Top  Profile  WWW
 PostPost subject: Re: MySQL search with PHP        Posted: Tue Jul 21, 2009 9:02 pm 
Reply with quote
Donator
User avatar
Offline

Joined
Sat May 12, 2007 1:05 pm

Posts
5271

Location
The Collection Book

Favourite OS
Windows & Phone
Use something like this.

Code:
'<br>' .$fieldBox_2 . '<br>' . $fieldBox_3 . '<br>'
and so on.
This seperated PHP code and HTML code.

This is the only this what I get from PHP.

_________________
Image
http://www.thecollectionbook.info
Subscribe to our Image for updates and like us on Image.

Reading Mode only, PM's possible.


Top  Profile  WWW
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 




Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

All views expressed in these forums are those of the author and do not necessarily represent the views of the BetaArchive site owner.

Powered by phpBB® Forum Software © phpBB Group

Copyright © 2006-2018

 

Sitemap | XML | RSS