Jump to content

Searching with Get


underknown

Recommended Posts

I'm having some trouble with a GET search code; everything works until the stringresult but then it dies to Error Querying Database

<html>
<body>

<?php

//Get sort setting and search terms from URL with GET
$usersearch = $_GET['usersearch'];
echo 'Search word: ' . $usersearch;
echo '<br>';
require_once("dbvars.php");

//Connect to Database
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PW, DB_NAME) or die ("Error connecting to database.");

//Build the Query
$query = "SELECT * FROM customers" . 'WHERE customers.given_name=\'' . $usersearch . '\'';
echo $query;

//Execute the Query
$result = mysqli_query($dbc, $query) or die ("Error querying database.");

//Create the Table Headings
echo '<table border="1"><tr>';
echo '<td><b>First Name</b></td>';
echo '<td><b>Last Name</b></td>';
echo '<td><b>Address</b></td>';
echo '<td><b>City</b></td>';
echo '<td><b>Province</b></td>';
echo '<td><b>Postal Code</b></td>';
echo '<td><b>Phone Number</b></td>';
echo '</tr>';

//Display Results through Array Loop
While ($row = mysqli_fetch_array($result)) {
echo '<tr><td>' . $row['given_name'] . '</td>';
echo '<td>' . $row['surname'] . '</td>';
echo '<td>' . $row['address'] . '</td>';
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['province'] . '</td>';
echo '<td>' . $row['postal_code'] . '</td>';
echo '<td>' . $row['phone_number'] . '</td></tr>';
}
'</table>';

// Close the database connection
mysqli_close($dbc);

?>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<input name="usersearch" type="text" size="30" />
<input name="submit" type="submit" value="go" />
</form>

</body>
</html>

 

Link to comment
https://forums.phpfreaks.com/topic/217319-searching-with-get/
Share on other sites

Sorry, I've never been familiarized with that die code before.  Here's the error.

 

"Warning: mysqli_error() expects exactly 1 parameter, 0 given in Z:\www\website\search_get.php on line 20"

 

The Query:

 

"SELECT * FROM customersWHERE customers.given_name='charles'"

Link to comment
https://forums.phpfreaks.com/topic/217319-searching-with-get/#findComment-1128464
Share on other sites

oops, my bad. i'm used to mysql_error(). apparently you need the link:

 

//Execute the Query
$result = mysqli_query($dbc, $query) or die (mysqli_error($dbc));

 

see something a little odd in there?

 

The Query:

 

"SELECT * FROM customersWHERE customers.given_name='charles'"

Link to comment
https://forums.phpfreaks.com/topic/217319-searching-with-get/#findComment-1128465
Share on other sites

The exact result:

 

Search word: charles

SELECT * FROM customersWHERE customers.given_name='charles'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.given_name='charles'' at line 1

Link to comment
https://forums.phpfreaks.com/topic/217319-searching-with-get/#findComment-1128466
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.