Matt Murphy Matt Murphy - 4 years ago 70
SQL Question

Searching through a database via search form in PHP

I've developed a page that a user can use to search a variety of data in my database, however nothing gets displayed.

No errors or messages are displays it simply just refreshes the page.

bind_param is new to me so I have a feeling it may be an error in the line -

$stmt->bind_param('s', $name);

I have compared to the database and all the table names and variable names are correct.


include 'searchform.php';
include 'header.php';

if ($stmt = $connection->prepare ("SELECT * FROM users WHERE Username = '$name'"))
$stmt->bind_param('s', $name);
echo "<p>Please enter a search query</p>";
echo "NOT SET!";


Errors Displayed

Warning: session_start(): Cannot send session cookie - headers already sent

searchform.php:2 which is displayed underneath:

ini_set('display_errors', -1);
include 'header.php';
include 'connection.php';
<br /><br />
<link rel="stylesheet" type="text/css" href="web.css" />

<link rel="stylesheet" type="text/css" href="/web.css" />
<h2>Search for people</h2>
<br />
Please enter the name you wish to search for<br /><br />
<form method="post" action="search.php" id="searchform">
<input type="text" name="name">
<input type="submit" name="searchsubmit" value="Submit">
</form><br /><br/>
Or search people by category

Answer Source

I think it should be like this:

    if ($stmt = $connection->prepare ("SELECT * FROM users WHERE Username = ?")) 

With a ? as placeholder.

Got it from the examples here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download