PHP PDO Update with 2 tables - php

I have a little problem. This is what I want to achive:
I have 2 mysql tables (categories, channels), the channel table has a cat_id in it. I want to update/edit a product and place it in another category but the code that I've made shows just one category (id=1) even if the product has a parent id(cat_id) of 5.
try {
//prepare query
$query = "select channel_id, name, category_id from channels where channel_id = ? limit 0,1";
$stmt = $pdo->prepare( $query );
//this is the first question mark
$stmt->bindParam(1, $_REQUEST['id']);
//execute our query
$stmt->execute();
//store retrieved row to a variable
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//values to fill up our form
$channel_id = $row['channel_id'];
$name = $row['name'];
$category_id = $row['category_id'];
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
$query2 = "SELECT * FROM categories";
$stmt2 = $pdo->prepare( $query2 );
$stmt2->execute();
$results = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
<!--we have our html form here where new user information will be entered-->
<form action='#' method='post' border='0'>
<table>
<tr>
<td>Channel Name</td>
<td><input type='text' name='name' value='<?php echo $name; ?>' /></td>
</tr>
<tr>
<td>Category</td>
<td>
<?php foreach($results as $rows) {?>
<select name="fileselect">
<option name='cat_id' value=" <?php echo $rows['category_id']; ?>"> <?php echo $rows['name']; ?></option>
<!-- <input type='text' name='category_id' value='<?php //echo $category_id; ?>' /> -->
<?php } ?>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<!-- so that we could identify what record is to be updated -->
<input type='hidden' name='channel_id' value='<?php echo $channel_id ?>' />
<!-- we will set the action to edit -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Edit' />
</td>
</tr>
</table>
</form>

Instead of
<?php foreach($results as $rows) {?>
<select name="fileselect">
<option name='cat_id' value=" <?php echo $rows['category_id']; ?>"> <?php echo $rows['name']; ?></option>
<!-- <input type='text' name='category_id' value='<?php //echo $category_id; ?>' /> -->
<?php } ?>
</select>
Try:
<select name="fileselect">
<?php foreach($results as $rows) {?>
<option name='cat_id' value=" <?php echo $rows['category_id']; ?>"> <?php echo $rows['name']; ?></option>
<!-- <input type='text' name='category_id' value='<?php //echo $category_id; ?>' /> -->
<?php } ?>
</select>

Related

Updating a Membership Form with Dropdowns in PHP

I'm learning PHP and I struggle to find solutions to my issue. I've created a page where membership data can be edited. All my 'type=text' fields display the current value of the member correctly. But the values selected on the 2 drop down fields (Language and Interest) do not display in the edit field. They do update though to MySql but the 'Select One...' option display when I want to edit the members 'Language' and 'Interest' fields.
What should I do so that the current value of the 2 drop downs that is stored in the db, displays on the ui when a member needs to get edited?
Here is my PHP code:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not Found');
include('dbconnect.php');
try{
$sql = "SELECT id, firstName, lastName, idNumber, mobileNumber, email, birthDate, languageType, interest FROM members WHERE id = ? LIMIT 0,1";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$firstName = $row['firstName'];
$lastName = $row['lastName'];
$idNumber = $row['idNumber'];
$mobileNumber = $row['mobileNumber'];
$email = $row['email'];
$birthDate = $row['birthDate'];
$languageType = $row['languageType'];
$interest = $row['interest'];
}
catch(PDOException $exception){
die('ERROR: '.$exception->getMessage());
}
?>
<?php
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not found.');
include 'dbconnect.php';
if($_POST){
try{
$sql = "UPDATE members SET
firstName=:firstName,
lastName=:lastName,
idNumber=:idNumber,
mobileNumber=:mobileNumber,
email=:email,
birthDate=:birthDate,
languageType=:languageType,
interest=:interest
WHERE id=:id";
$stmt = $conn->prepare($sql);
$firstName = htmlspecialchars(strip_tags($_POST['firstName']));
$lastName = htmlspecialchars(strip_tags($_POST['lastName']));
$idNumber = htmlspecialchars(strip_tags($_POST['idNumber']));
$mobileNumber = htmlspecialchars(strip_tags($_POST['mobileNumber']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$birthDate = htmlspecialchars(strip_tags($_POST['birthDate']));
$languageType = $_POST['languageType'];
$interest = $_POST['interest'];
$stmt->bindParam(':firstName', $firstName);
$stmt->bindParam(':lastName', $lastName);
$stmt->bindParam(':idNumber', $idNumber);
$stmt->bindParam(':mobileNumber', $mobileNumber);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':birthDate', $birthDate);
$stmt->bindParam(':languageType', $languageType);
$stmt->bindParam(':interest', $interest);
$stmt->bindParam(':id', $id);
if($stmt->execute()){
echo "<div class='alert alert-success'>Member was updated.</div>";
}else{
echo "<div class='alert alert-danger'>Unable to update member. Please try again.</div>";
}
}
catch(PDOException $exception){
die('ERROR: ' . $exception->getMessage());
}
}
?>
And here is the html:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?id={$id}");?>" method="post">
<table class='table table-hover table-responsive table-bordered'>
<tr>
<td>First Name</td>
<td><input type='text' name='firstName' value="<?php echo htmlspecialchars($firstName, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type='text' name='lastName' value="<?php echo htmlspecialchars($lastName, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>ID Number</td>
<td><input type='text' name='idNumber' value="<?php echo htmlspecialchars($idNumber, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input type='text' name='mobileNumber' value="<?php echo htmlspecialchars($mobileNumber, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Email</td>
<td><input type='text' name='email' value="<?php echo htmlspecialchars($email, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Birth Date</td>
<td><input type='date' name='birthDate' value="<?php echo htmlspecialchars($birthDate, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Language</td>
<td>
<select name='languageType' class='form-control' value="<?php echo $languageType; ?>" />
<option>Select One...</option>
<option>Afrikaans</option>
<option>English</option>
<option>Zulu</option>
<option>Xhosa</option>
<option>Venda</option>
<option>French</option>
</td>
</tr>
<tr>
<td>Interest</td>
<td>
<select name='interest' class='form-control' value="<?php echo htmlspecialchars($interest, ENT_QUOTES); ?>" />
<option>Select One...</option>
<option>Golf</option>
<option>Rugby</option>
<option>Tennis</option>
<option>Cricket</option>
<option>Swimming</option>
<option>Hiking</option>
<option>Surfing</option>
<option>Movies</option>
<option>Swords</option>
</td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' value='Save Changes' class='btn btn-primary' />
<a href='index.php' class='btn btn-danger'>Back to read members</a>
</td>
</tr>
</table>
</form>
This is all wrong :
<select name='languageType' class='form-control' value="<?php echo $languageType; ?>" />
<option>Select One...</option>
<option>Afrikaans</option>
<option>English</option>
<option>Zulu</option>
<option>Xhosa</option>
<option>Venda</option>
<option>French</option>
<select name='interest' class='form-control' value="<?php echo htmlspecialchars($interest, ENT_QUOTES); ?>" />
<option>Select One...</option>
<option>Golf</option>
<option>Rugby</option>
<option>Tennis</option>
<option>Cricket</option>
<option>Swimming</option>
<option>Hiking</option>
<option>Surfing</option>
<option>Movies</option>
<option>Swords</option>
The Select does not have a value attribute, the value attribute belong to option.
this is how your select should look :
<select name='languageType' class='form-control' />
<option value="Afrikaans">Afrikaans</option>
... <!-- Other options just like I did the first one -->
</select>
if you want the value from the database to be selected then you will need to check if the option is not equal to the db value then select it with the selected attribute of option.
like :
<select name='languageType' class='form-control' />
<option value="">Select One...</option>
<option value="Afrikaans"<?php if($languageType == "Afrikaans"){echo "selected='selected'";?>>Afrikaans</option>
<option value="English" <?php if($languageType == "English"){echo "selected='selected'";?>>English</option>
<option value="Zulu" <?php if($languageType == "English"){echo "selected='selected'";?>>Zulu</option>
<option value="Xhosa" <?php if($languageType == "Xhosa"){echo "selected='selected'";?>>Xhosa</option>
<option value="Venda" <?php if($languageType == "Venda"){echo "selected='selected'";?>>Venda</option>
<option value="French" <?php if($languageType == "French"){echo "selected='selected'";?>>French</option>
</select>
Then do your second dropdown following the above as a guide, also don't forget to close the select option </select>

Update 1 column of all rows in the database table

Hopefully someone can help me with this "easy" question.
I have data in a table that has been gathered from a mysql database. I want the user to be able to edit the "PRICE" column to any and all rows, then press the update button which will send the data to the table and update all the rows in the "PRICE" column.
For the life of me I can't get the database to update. I know it has to be something that I'm missing. And it has to be something so easy it's laughable.
Please help?
if (isset($_POST['update']))
{
echo '<pre>';
print_r($_POST);
echo '</pre>';
if (is_array($ID))
{
foreach($_POST['hidden'] AS $ID)
{
echo "ID is: " . $ID . "</br>";
echo "Price is: " . $pricing . "</br>";
$ID = mysqli_real_escape_string($conn, $_POST['hidden'][$ID]);
$pricing = mysqli_real_escape_string($conn, $_POST['price'][$ID]);
$updateQuery = 'UPDATE `bathroom_price` SET `price` ="' . $pricing . '" WHERE `ID`=' . $ID;
mysqli_query($conn, $updateQuery) or die(mysql_error());
}
}
}
?>
</head>
<?php
mysqli_select_db($conn, "table_name");
?>
<div class="row center-xs">
<div style="margin-top:100px;" class="col-xs-12 col-sm-12 col-md-12 col-lg-8">
<div class="box">
<form method=POST>
<h1>Price List for Bathroom Form</h1>
<table>
<?
$secondSQL = "SELECT question, question_ID, ID, form_ID, form_name FROM bathroom_price GROUP BY question_ID, form_name ORDER BY ID ";
$result1 = mysqli_query($conn, $secondSQL);
while ($row = mysqli_fetch_assoc($result1))
{
$question_ID = $row['question_ID'];
$question = $row['question'];
$formID = $row['form_ID'];
$form_name = $row['form_name'];
?>
<input type=hidden value="<? echo $question_ID ?>">
<tr class='questionHeading'>
<td colspan='3'>
<h2><? echo $question ?></h2>
<h3>Questions for the <? echo $form_name ?> Form</h3></td>
</tr>
<tr>
<th>Options:</th>
<th>Price:</th>
<th>Update:</th>
</tr>
<?
$thirdSQL = "SELECT question_ID, options, price, ID FROM bathroom_price WHERE question_ID = $question_ID";
$replies = mysqli_query($conn, $thirdSQL);
while ($rows = mysqli_fetch_assoc($replies))
{
$price = $rows['price'];
$options = $rows['options'];
$ID = $rows['ID'];
?>
<input type=hidden name="hidden[]<?echo $ID ?>" value="<?echo $ID ?>" />
<input type=hidden value="<? echo $question_ID ?> " />
<tr>
<td style='width:60%;'>
<input readonly type=text value="<? echo $options ?>">
</td>
<td style='width:10%;'>
<input type=text name="price[]<?echo $ID ?>" value="<?echo $price ?>">
</td>
</tr>
<?}?>
<?}?>
</table>
<div class="start-xs" style="margin: 0 0 50px 0;">
<button type=submit name="update" class="admin-style" value="Update Price Form">
<i class="fa fa-save"></i> Update Price Form
</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
<?php mysqli_close($conn);?>
Maybe this is a problem:
<input type=hidden name="hidden[]<?echo $ID ?>" value="<?echo $ID ?>" />
Your input name is "hidden[]1" etc.

some combobox reset each other after selected

I have some combobox with onchange event, and they're reset each other when selected the orther one of them, does any suggest how to retain the value on the page? this my script :
<form method="POST" name="form1" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="0">
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td>
<select name="select_petugas1" style="width:18px;" onchange="this.form.submit('select_petugas1');"> //first combobox
<option></option>
<?php include 'dbconn.php';
$sql_peg1="SELECT * FROM users"; $result_peg1=$conn->query($sql_peg1);
while( $row_peg1=$result_peg1->fetch_assoc() ){
echo "<option>".$row_peg1['nama']."</option>";
}
?>
</select>
</td>
<td>
<?php
if(isset($_POST['select_petugas1'])){
$select_petugas1=$_POST['select_petugas1'];
echo "<input type='text' name='select_petugas1' value='".$select_petugas1."'>"; // Throw 1st result into the text box
$sql_NIP1="SELECT NIP FROM users WHERE nama='$select_petugas1'";
$result_NIP1=$conn->query($sql_NIP1);
$row_NIP1=$result_NIP1->fetch_assoc();
$NIP1=$row_NIP1['NIP'];
?>
</td>
<td> NIP</td>
<td>:</td>
<td><input type="text" name='NIP1' value="<?php echo $NIP1; ?>"></td>
</tr> <!-- child of first result -->
<tr>
<td colspan="5" bgcolor="blue"></td>
</tr>
<tr>
<td>
<select name="peg_2" style="width:18px;" onchange="submit(this)"><!--2nd combobox-->
<option></option>
<?php
$sql_peg2="SELECT nama FROM users";
$result_peg2=$conn->query( $sql_peg2 );
while ($row_peg2=$result_peg2->fetch_assoc()){
echo "<option value='".$row_peg2['nama']."'>".$row_peg2['nama']."</option>";
}
?>
</select>
</td>
<td>
<?php
if( isset($_POST['peg_2']) ){
$peg_2=$_POST['peg_2'];
echo "<input type='text' name='peg2' value='".$peg_2."'>"; // 2nd result throw into 2nd texbox
$sql_NIP2="SELECT NIP FROM users WHERE nama='$peg_2'";
$result_NIP2=$conn->query($sql_NIP2);
$row_NIP2=$result_NIP2->fetch_assoc();
?>
</td>
<td> NIP</td>
<td>:</td>
<td><input type='text' name='NIP2' value="<?php echo $row_NIP2['NIP'];?>"> <!--2nd child of result-->
<?php
}
}
if(isset($_POST['NIP2'])){
$NIP2=$_POST['NIP2'];
echo "<br /> NIP2 :".$NIP2."<br />";
}
mysqli_close($conn);
?>
</td>
</tr>
</table>
</form>
<form method="POST" name="wilayah" id="wilayah" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="1">
<tr>
<td>
<select name="select_provinsi" onchange="submit(this)" style="width:18;">
<option selected>PROVINSI</option>
<?php
include 'dbconn.php';
$sql_prov="SELECT * FROM wilayah GROUP BY provinsi";
$result_prov=$conn->query($sql_prov);
echo "";
while($row_prov=$result_prov->fetch_assoc()){
$provinsi=$row_prov['provinsi'];
echo "<option value='".$provinsi."'>".$provinsi."</option>";
}
?>
</select>
<?php
if(isset($_POST['select_provinsi'])){
$select_provinsi=$_POST['select_provinsi'];
echo "
<input type='text' name='select_provinsi' value='".$select_provinsi."' placeholder='PROVINSI'>
</td>
</tr>";
$sql_kabkota="SELECT * FROM wilayah WHERE provinsi='$select_provinsi' GROUP BY kab_kota";
$result_kabkota=$conn->query($sql_kabkota);
?>
<tr>
<td>
<select name="select_kabkota" style="width:18px;" onchange="submit(this)"><option>KAB/KOTA</option>
<?php
while($row_kabkota=$result_kabkota->fetch_assoc()){
echo "<option>".$row_kabkota['kab_kota']."</option>";
}
?>
</select>
<?php
}
if(isset($_POST['select_kabkota'])){
$select_kabkota=$_POST['select_kabkota'];
?>
<input type="text" name="kab_kota" value="<?php echo $select_kabkota;?>">
<?php
}
mysqli_close($conn);
?>
</td>
</tr>
</table>
</form>
hope any suggestion for resolved of my problem with them,,
onchange="submit(this)" means that you want to submit the form when the value of the combobox changes. So, when the form is sent, the page reloads and you get the default value of your form.
To restore the chosen value, I would do something like :
<select name="select_kabkota" style="width:18px;" onchange="submit(this)">
<option>KAB/KOTA</option>
<?php
if(isset($_POST['select_kabkota']))
$select_kabkota=$_POST['select_kabkota'];
while($row_kabkota=$result_kabkota->fetch_assoc())
{
$selected = $select_kabkota == $row_kabkota['kab_kota'] ? 'selected="selected"' : '';
echo "<option ".$selected." >".$row_kabkota['kab_kota']."</option>";
}
?>
</select>

How do I make my drop down select menu persistently show the same data after a query is done using the menu?

So I am having an issue with a drop down selection box. What I am doing is having someone log into a database, and then the database shows all the tables available in the selection box. The user can then select the table they wish to see, hit select and bam! There's the table information.
I am, however having an issue getting the data in the selection box to persist after they hit select. For some reason, it just makes it empty. I'm using session variables, and maybe that effects it? I'm just now beginning to learn how that works too. Take a look at let me know what you think:
<?php
session_start();
if(!isset($_SESSION['session_level'])):
$_SESSION['session_level'] = 0; ?>
<? endif ?>
<?php
if(isset($_POST['host'])):
$_SESSION['host'] = $_POST['host'];
$_SESSION['dbname'] = $_POST['dbname'];
$_SESSION['username'] = $_POST['username'];
$_SESSION['pw'] = $_POST['pw'];
?>
<?php endif ?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Login Test</title>
</head>
<body>
<?
if (isset($_POST['return']))
{
$_SESSION['session_level'] = 0;
}
else if (isset($_POST['submit']))
{
try
{
$db = new PDO("mysql:host=".$_POST['host'].";dbname=".$_POST['dbname'], $_POST['username'], $_POST['pw']);
}
catch(Exception $error)
{
$_SESSION['session_level'] = 0;?>
<a href='<?= $_SERVER['PHP_SELF'] ?>'>Click here to return.</a>
<? echo "\n"; ?>
<?die("Connection to user database failed: " . $error->getMessage());
}
try
{
$db->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);
$query = "SHOW TABLES";
$results = $db->query($query)->fetchAll();
$_SESSION['session_level'] = 1;
}
catch(Exception $error)
{
echo "Problem with query!";
$_SESSION['session_level'] = 0;?>
<a href='<?= $_SERVER['PHP_SELF'] ?>'>Click here to return.</a>
<? }
}
?>
<?php if($_SESSION['session_level'] == 0){?>
<h1>Database Practice</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='initialentry'>
<table border='0' style='text-align: center'>
<tr>
<td style='text-align: right;'>Enter host name:</td>
<td style='text-align: left;'>
<input type='text' name='host' value='localhost'>
</td>
</tr>
<tr>
<td style='text-align: right;'>Enter database name:</td>
<td style='text-align: left;'>
<input type='text' name='dbname' value='zxyx999'>
</td>
</tr>
<tr>
<td style='text-align: right;'>Enter user name:</td>
<td style='text-align: left;'>
<input type='text' name='username' value='zxyx999'>
</td>
</tr>
<tr>
<td style='text-align: right;'>Enter password:</td>
<td style='text-align: left;'>
<input type='password' name='pw' width='15' value='12345'>
</td>
</tr>
<tr>
<td style='text-align: right;'><input type="reset" name="reset" value="Reset"></td>
<td style='text-align: left;'><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
<?php }
else if ($_SESSION['session_level'] == 1)
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='getForm'>
<select name='select'>
<? foreach($results as $row)
echo "<option value=" . $row[0] .">" .$row[0]. "</option>"; ?>
</select>
<input type="submit" name="selected" value="Select">
<input type="submit" name="return" value="Return to Main Screen">
</form>
<?php
if(isset($_POST['selected']))
{
try
{
$db = new PDO("mysql:host=".$_SESSION['host'].";dbname=".$_SESSION['dbname'], $_SESSION['username'], $_SESSION['pw']);
}
catch(Exception $error)
{
die("Connection to user database failed: " . $error->getMessage());
}
try
{
$query = $db->prepare("SELECT * FROM " . $_POST['select']);
$query->execute();
$header = true;
}
catch(Exception $error)
{
echo "Query failed.";
}
echo "</br>";
?>
<?php
echo "<table border='1'>";
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
if($header == 'true')
{
foreach($row as $index => $fieldValue)
{
echo "<td>";
echo $index;
echo"</td>";
}
echo "</tr>";
$header = 'false';
}
echo "<tr>";
foreach($row as $index => $fieldValue)
{
echo "<td>";
echo $fieldValue;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
?>
</body>
</html>
If I well understand your problem then my advice 'll be to change your form and do it this way :
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='getForm'>
<input type="hidden" name="selected" value="true">
<select name='select'>
<? foreach($results as $row)
echo "<option value=" . $row[0] .">" .$row[0]. "</option>"; ?>
</select>
<input type="submit" value="Select">
</form>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='getForm'>
<input type="hidden" name="return" value="true">
<input type="submit" value="Return to Main Screen">
</form>
It means you must create 2 forms instead of 1 with because the name value on a submit button is not intrepreted the same way on all browser. So by removing and replacing it by an input type hidden it 'll assure the field 'll exist after submitting forms.
When you repopulate the select list, if the option selected matches the option being populated, then you can make that bit of code <option selected value=" and that option will now be the default selected item at the moment.
I realize the below code isn't PHP, my point is that it should be a simple If statement, to include the selected tag into the option.
/*
Adding the "selected" tag to an option, makes it the default.
*/
<select>
<option value="" style="display:none;">Select a Value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<br /><br />
<select>
<option value="" style="display:none;">Select a Value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
</select>

SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '{search_term}''

I can't do the live search table thing. Can someone help me please?
Here is my code. I want to show only the data I've search.........................................................................................................................................................................................................................................................................
<?php
//include the connection file
include "conn.php";
$sql = "SELECT * FROM tblreservation";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE Name = '{search_term}'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<form name="search_form" method="POST" action="trys.php" align="center">
Search: <input type="text" name="search_box" value="" />
<input type="submit" name="search" value="Search the table...">
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<td>ID</td>
<td>Name</td>
<td>Email</td>
<td>Packages</td>
<td><select name="Packages" class="fieldsize">
<option value="">select package</option>
<option value="budget" <?php if($valid_Packages=='budget') echo "selected='selected'";?>>Budget</option>
<option value="standard" <?php if($valid_Packages=='standard') echo "selected='selected'";?>>Standard</option>
<option value="super" <?php if($valid_Packages=='super') echo "selected='selected'";?>>Super</option>
<option value="mega" <?php if($valid_Packages=='mega') echo "selected='selected'";?>>Mega</option>
</select>
<span class="err"><?php echo $error["Packages"];?></span></td>
</tr>
<td>Contactno</td>
<td>Gender</td>
<td><input type="radio" name="gender" value="male" <?php if($valid_gender=='male') echo "checked='checked'";?> />
Male
<input type="radio" name="gender" value="female" <?php if($valid_gender=='female') echo "checked='checked'";?>/>
Female <span class="err"><?php echo $error["gender"];?></span></td>
<td>file</td>
<td><input type="file" name="file" value="upload" />
<span class="err"><?php echo $error["file"];?></span></td>
<td>Address</td>
</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<td><?php echo $row['id']; ?> </td>
<td><?php echo $row['Name']; ?> </td>
<td><?php echo $row['Email']; ?> </td>
<td><?php echo $row['Packages']; ?> </td>
<td><?php echo $row['Contactno']; ?> </td>
<td><?php echo $row['Gender']; ?> </td>
<td><?php echo $row['file']; ?> </td>
<td><?php echo $row['Address']; ?> </td>
</tr>
<?php } ?>
</table>
You are missing a $ and a space in this line:
$sql .= "WHERE Name = '{search_term}'";
The correct line should be as follows:
$sql .= " WHERE Name = '{$search_term}' ";
The SQL statement you are currently generating is exactly this:
SELECT * FROM tblreservationWHERE Name = '{search_term}'
Additionally, I would recommend checking for the existence of $_POST['search_box'] rather than $_POST['search'] in your if-statement and that it actually has a value before appending it as this is what you actually want to use in your query:
if (isset($_POST['search_box']) && $_POST['search_box']) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= " WHERE Name = '{$search_term}' ";
}

Categories