Jump to content

Search


underknown

Recommended Posts

Alright, I'm supposed to be creating a search bar, but I keep running into errors, and I'm sure there must be something wrong here that I'm missing.  I don't know whether it's the PHP or the HTML that's messing up, so I apologize for that, but given the nature of the errors, I'd assume the former.

 

Here's the code:

<?php include("header.php"); ?>
<?php include("topmenu.php"); ?>
<?php include("menu.php"); ?>
<?php include("news.php"); ?>
<?php include("links.php"); ?>
<?php include("info.php"); ?>
<!-- **** php include news.php, links.php and info.php here to return side menu -->
<div id="column2">
<h1>introduction</h1>
<!-- **** INSERT PAGE CONTENT HERE **** -->

<div id="content">
    <li><a id="various3" href="http://localhost/website/center.php">Add Customers</a></li>
    </ul>
</div>

<br />

<?php

echo "Customer Results from Database";

require_once("dbvars.php");

//Search
if (isset($_POST['search'])) {
$name = $_POST['name'];
$name_trimmed = trim($name);

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME) or die('Error connecting to database.');
$query = "SELECT * FROM customers WHERE name LIKE '%$name_trimmed%'";
$result = mysqli_query($dbc, $query) or die('Error querying database.');
mysqli_close($dbc);

echo '<h1>Search Results</h1>';
echo '<table border="0\"><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>';

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>';
}
}

// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME) or die('Error connecting to MySQL server.');

// Build the query
$query = "SELECT * FROM customers";

$result = mysqli_query($dbc, $query) or die('Error querying database.');

//Table Headers
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>';


//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>';
}
'</table>';

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


?>

<!-- Search Form -->
<div class="form" />
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" />
    	<input name="name" id="name" type="text" value="enter name" size="30" maxlength="50" />
    <div class="button">
       	<input name="search" type="submit" value="go" />
    </div>
</form>
</div>
<div style="clear:both"> </div>

</body>
</html>

 

 

 

Link to comment
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.