I have this database project due Monday and I am trying to create a PHP script to do a simple search query in the MySQL database. This is what I have...

search.html

<html>
<head>
	<title>NOMO Auto Group Inventory Search</title>
</head>

<body>
	<h2>NOMO Auto Group Inventory Search</h2>

	<form action="results.php" method="post">
	Choose Search Type:<br />

	<select name="searchtype">
		<option value="Make">Make</option>
		<option value="Model">Model</option>
		<option value="Year">Year</option>
		<option value="Color">Color</option>
	</select>
	<br />
	Enter Search Term:<br />

	<input name="searchterm" type="text" size="40"/>
	<br/>
	<input type="submit" name="submit" value="Search"/>
	</form>

</body>
</html>

results.php

<html>
<head>
	<title>NOMO Auto Group Search Results</title>
</head>
<body>
<h2>NOMO Auto Group Search Results</h2>
<?php
	//create short variable name
	$searchtype=$_POST['searchtype'];
	$searchterm=$_POST['searchterm'];

	if(!$searchtype || !$searchterm) {
		echo 'You have not entered any search details. Please go back and try again.';
		exit;
	}

	if (!get_magic_quotes_gpc()){
	$searchtype = addslashes($searchtype);
	$searchterm = addslashes($searchterm);
	}
	
	@ $db = new mysqli('(db IP)', '(db username)', '(db pw), 'database');

	if (mysqli_connect_errno()) {
		echo 'Error: Could not connect to database.';
		exit;
	}

	$query = "select * from Inventory where ".$searchtype." like '%".$searchterm."%'";
	$result = $db->query($query);

	$num_results = $result->num_rows;

	echo "<p>Number of cars found: ".$num_results." </p>";

	for ($i=0; i <$num_results; $i++) {
		$row = $result->fetch_assoc();
		echo "<p><strong>".($i+1).". Make: ";
		echo stripslashes($row['Make']);
		echo "</strong><br />Model: ";
		echo stripslashes($row['Model']);
		echo "<br />Color: ";
		echo stripslashes($row['Color']);
		echo "<br />Year: ";
		echo stripslashes($row['Year']);
		echo "</p>";

	}

	$result->free();
	$db->close();

?>
</body>
</html>

This is what I get when I search for the Make Acura...

NOMO Auto Group Search Results
query($query); $num_results = $result->num_rows; echo "

Number of cars found: ".$num_results."
"; for ($i=0; i <$num_results; $i++) { $row = $result->fetch_assoc(); echo "

".($i+1).". Make: "; echo stripslashes($row['Make']); echo "
Model: "; echo stripslashes($row['Model']); echo "
Color: "; echo stripslashes($row['Color']); echo "
Year: "; echo stripslashes($row['Year']); echo "
"; } $result->free(); $db->close(); ?>

line 23 you didn't close yer single quote

@ $db = new mysqli('(db IP)', '(db username)', '(db pw)', 'database');

there might be more, but that is what was causing the biggest error

I accidentally deleted the single quote while I was removing my login information. In my original code I have the quote closed and I am still receiving the error I posted originally.

I am still receiving the error I posted originally.

are you saying that the "result" you get is actually the PHP code you wrote to do the searching? If that is the case, then your server is not configured to execute php. You need to search online for tutorials to install php on whatever server you are using.

are you saying that the "result" you get is actually the PHP code you wrote to do the searching? If that is the case, then your server is not configured to execute php. You need to search online for tutorials to install php on whatever server you are using.

Thank you for trying to help. I was trying to do this all locally and connect to a MySQL DB that was online hosted by my university. I am guessing thats why I was getting those errors.

I posted my code to my web server and got a new error..

Notice: Undefined index: searchtype in /pass/home/9/c/**/www/NOMO/results.php on line 9

Notice: Undefined index: searchterm in /pass/home/9/c/**/www/NOMO/results.php on line 10
You have not entered any search details. Please go back and try again.

Does this still look like a server side error? Is there anything I can do?

Thanks again

Undefined index: searchtype

That would typically happen when the form field "does not exist", which is typically due to a spelling error. If your form field has <input type='text' name='searchType'...> (notice the uppercase 'T') then you will need the exact spelling on your results.php $searchtype=$_POST['searchType']; If you are still having problems put this at the top or results.php: echo "<pre>Server received:" . print_r( $_POST,true) . "</pre>"; it should reveal what exactly the server receives. If you don't see anything, then instead of $_POST, try $_REQUEST.

If you are still having problems put this at the top or results.php: echo "<pre>Server received:" . print_r( $_POST,true) . "</pre>"; it should reveal what exactly the server receives. If you don't see anything, then instead of $_POST, try $_REQUEST.

I made sure the code in my html matches the code being posted in the PHP. I added the line you suggested and I get

Server received:Array
(
)

what do you see when you change it to $_REQUEST: echo "<pre>Server received:" . print_r( $_REQUEST,true) . "</pre>";

what do you see when you change it to $_REQUEST: echo "<pre>Server received:" . print_r( $_REQUEST,true) . "</pre>";

same thing :(

I accidentally deleted the single quote while I was removing my login information. In my original code I have the quote closed and I am still receiving the error I posted originally.

not true at all or you wouldnt be getting this output

NOMO Auto Group Search Results
query($query); $num_results = $result->num_rows; echo "

Number of cars found: ".$num_results."
"; for ($i=0; i <$num_results; $i++) { $row = $result->fetch_assoc(); echo "

".($i+1).". Make: "; echo stripslashes($row['Make']); echo "
Model: "; echo stripslashes($row['Model']); echo "
Color: "; echo stripslashes($row['Color']); echo "
Year: "; echo stripslashes($row['Year']); echo "
"; } $result->free(); $db->close(); ?>

I was trying to do this all locally

I would expect your php.ini file to have the following defined within the Data Handling section:

variables_order = "EGPCS"

If it is commented out, uncomment it. Otherwise, I am not sure what's going on with your server, but if this is due Monday, my suggestion would be to use your school's server for the development.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.