While loop finding the last iteration only - php

*How to solve this..??
My while loop is finding the last iteration only, If I select last attendance A then all the student's values will be 0, if I select P then all the values will be 1. So, how can I solve this?*
Index.php file
<?php
include'connection.php';
$sql = "SELECT * FROM `student_info`";
$result = mysqli_query($conn,$sql);
?>
<table border="1">
<tr>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Attendance</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result))
{
$id = $row['stu_id'];
?>
<tr>
<td><?php echo $row['stu_id'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td>
<form action="attendance.php" method="post">
<select name="atten" id="">
<option value="1">P</option>
<option value="0">A</option>
</select>
</td>
</tr>
<?php
}
?>
</table>
<br>
<button type="submit">Submit</button>
</form>
attendance.php
<?php
include'connection.php';
$sql = "SELECT * FROM `student_info`";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
if(isset($_POST['atten'])){
$atten = $_POST['atten'];
echo $atten.'<br>';
}
}
Here is output
Image for Index.php file
Image for attendance.php file

You're not submitting the student ID in the form. You can use a hidden input for this.
<?php
while($row=mysqli_fetch_assoc($result))
{
$id = $row['stu_id'];
?>
<tr>
<td><?php echo $row['stu_id'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td>
<form action="attendance.php" method="post">
<input name="stu_id" type="hidden" value="<?php echo $row['stu_id'];?>">
<select name="atten" id="">
<option value="1">P</option>
<option value="0">A</option>
</select>
</form>
</td>
</tr>
<?php
}
?>
Then in attendance.php you can update that student.
<?php
include'connection.php';
$stmt = $conn->prepare("UPDATE `student_info` set atten = ? WHERE stu_id = ?");
$stmt->bind_param("ii", $_POST['atten'], $_POST['stu_id']);
$stmt->execute();
echo "Attendance for student {$_POST['stu_id']} updated";

Related

PHP Fetches two rows but uploads file from last row only

Im trying to fetch records from database, then i have to upload a image which should be stored in folder and also link to be updated in table.. Everything working fine only with last row.. For first row its not updating.. Please help me where im goin on.. Below is my code
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td><input type="file" value="<?php echo $amt; ?>" name="fileToUpload" id="fileToUpload" ></td>
<td><input type="submit" value="Submit" name="submit" >
</button>
</td> </tr>
<?php } ?>
</tbody>
</table>
</form>
upload.php is taken from https://www.w3schools.com/php/php_file_upload.asp
on your form
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td><input type="file" name="fileToUpload []" class="fileToUpload" ></td>
<input type="hidden" value="<?php echo $amt; ?>" name="fileToUploadLink []" class="fileToUploadLink" >
<td><input type="submit" value="Submit" name="submit" >
</button>
</td> </tr>
<?php } ?>
</tbody>
</table>
</form>
on upload.php
<?php
if(isset($_FILES['fileToUpload']['tmp_name'])&&isset($_POST['fileToUploadLink'])){
for($i=0;$i<=(count($_FILES['fileToUpload']['tmp_name'])-1);$i++){
move_uploaded_file($_FILES['fileToUpload']['tmp_name'][$i],$_POST['fileToUploadLink'][$i]);
}
}
?>
I have a few suggestions:
In your "php.ini" file, search for the file_uploads directive, and set it to On: file_uploads = On
...</button><!-- <<-- How are you using this? -->
Could try to echo $amt; at the bottom of the loop to see if the path is correct.
May want to check if the $amt with IF Empty condition/statement and maybe check the condition of your other variables.
$email1 = $_SESSION['email'];
$Vendor_id="SELECT Vendor_id FROM vendors where email = '$email1' ";
$result=mysqli_query($conn,$Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid=$row['Vendor_wallet_id'];
$tid=$row['request'];
$type=$row['amount'];
$pays=$row['status'];
$amt=$row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid;?></td>
<td><?php echo $tid;?></td>
<td><?php echo $type;?></td>
<td><?php echo $pays;?></td>
<td><input type="file" value="<?php echo $amt;?>" name="fileToUpload" id="fileToUpload" ></td>
<td><input type="submit" value="Submit" name="submit" >
</button> <!-- WHY IS THIS HERE? -->
</td></tr>
<?php
echo $amt; //See if it is correct path
}//END WHILE ?>
</tbody>
</table>
</form>
There are a few ways in which you could do the file upload, one of which would be to make the form a child of a single table cell and have the file field as a direct child of that form. The submit button, to keep the layout, would not be a child of the form and would need to be changed to a simple button input type then use javascript to submit the form
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_array( $query ) ) {
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" value="<?php echo $amt; ?>" name="fileToUpload" />
</form>
</td>
<td><input type="button" value="Submit" name="submit" /></td>
</tr>
<?php
}
?>
</tbody>
</table>
<script>
var bttns=Array.prototype.slice.call(document.querySelectorAll('input[type="button"][name="submit"]'));
bttns.forEach(function(bttn){
bttn.addEventListener('click',function(evt){
this.parentNode.parentNode.querySelector('form').submit();
}.bind(bttn),false );
});
</script>
Seems like You want to make a form per row.
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td colspan="2">
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="Vendor_wallet_id" value="<?php echo $cpid;?>" />
<input type="file" value="<?php echo $amt; ?>" name="fileToUpload" />
<button>Submit</button>
</form>
</td>
</tr>
</form>
and remove <form> tag that wraps table

how to refresh tabs data when update table?

index.php
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Engineering')">Engineering</button>
<button class="tablinks" onclick="openCity(event, 'LAW')">LAW</button>
</div>
<div id="Engineering" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'engineering'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
<div id="Law" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'law'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
edit.php
<?php
if(isset($_POST['update']))
{
$college_name = $_POST['colleges'];
$state = $_POST['state'];
$sqli = "update college set college_name = '$college_name', state = '$state' where id = '$id'";
$results = mysqli_query($link,$sqli);
if($result == true)
{
$msg .= "<p style='color:green;'>Your data update successfully</p>";
}
else
{
$msg .= "<p style='color:red;'>Errror!</p>";
}
}
?>
<form method="POST" enctype="multipart/form-data" >
<select name="state" id="state">
<option value="<?php echo $stateid; ?>"><?php echo $statename; ?></option>
<option value="">Select State</option>
<?php
$sql = "select * from statemaster";
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<option value=".$row['stateid'].">".$row['statename']."</option>";
}
?>
</select>
<select name="colleges" id="colleges">
<option value="<?php echo $college_name; ?>"><?php echo $college_name; ?></option>
<option value="">Select College</option>
</select>
<button type="submit" name='update' id='update'>update</button>
</form>
In this code when I click on edit button then it will go to edit.php page where I get id from url and run update query after updating table college the data will update but when I move from edit page to index.php page the data will remain same but in database update data will be there. So, How can I fix this issue ?
Thank You
Check for caching. It could be that the browser is not going to the server in order to get the contents of index.php, as it thinks it has it.
Try calling index.php with a variable, like:
Home

SQL statement to populate more than one listbox at a time

I'm trying to allow a user to specify how many rows they would like to add to the order form for the customer's purchase. This allows the user to have as many rows as needed for purchasing products rather than having a set list. I have the functionality working properly, where if you type in 3 and submit, it will give you three rows to enter in product order information.
The problem I am running into is where I am populating a listbox with the product id and name for the user to select. It populates the first row's list box, but the following list boxes only get the " - " and not the $row[] values. It seems like it's not passing in the sql statement anymore, why is this?
This is the area in my code where I'm running into a problem with the functionality:
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
And this is my entire code:
<?
connectDB();
$sql = "SELECT * FROM product";
$sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee";
$sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc";
$sql4 = "SELECT * FROM salesorder ORDER BY order_id desc";
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
$result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error());
$result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error());
$result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error());
$row = mysqli_fetch_array($result);
$row2 = mysqli_fetch_array($result2);
$row3 = mysqli_fetch_array($result3);
$row4 = mysqli_fetch_array($result4);
?>
<div id="order-wrap">
<form method="post" action="order.php">
<table class="orderInfo"><br>
<tr>
<th class="textCol">Product Rows:</th>
<td class="inputCol"><input type="text" name="rows"></td>
<td><input class="update" type="submit" name="update" value="Update"></td>
<td class="inputCol"></td>
</tr>
</table>
</form><!-- Order Rows -->
<form class="orderform" action ="order-report.php" METHOD = "post">
<h2>Order Form</h2>
<h3>Piedmont Furnishings</h3>
<img id="couch-img" src="couch.jpg" alt="couch">
<table class="orderInfo">
<tr>
<th class="textCol">Order Number:</th>
<td class="inputCol"><input type="text" name="orderNumber" value="<?= $row4[0] + 1; ?>" disabled></td>
<th class="textCol">Order Date:</th>
<td class="inputCol"><input type="text" name="orderDate" value="<?= date("Y-m-d") ?>"></td>
</tr>
<tr>
<th class="textCol">Customer:</th>
<td class="inputCol"><input type="text" name="customer"></td>
<td class="textCol"></td>
<td class="inputCol"></td>
</tr>
<tr>
<th class="textCol">Sales Agent:</th>
<td class="inputCol">
<select name="salesAgent">
<option value="selectAgent">Select One</option>
<!-- Populate listbox with Sales Agents ID -->
<?
do { ?>
<option value="<?= $row2[0]; ?>"><?= $row2[1] . " " . $row2[2]; ?></option>
<? } while($row2 = mysqli_fetch_array($result2)) ?>
</select>
</td>
<th class="textCol">Order Status:</th>
<td class="inputCol">
<select name="orderStatus">
<option value="selectStatus">Select One</option>
<!-- Populate listbox with Status ID -->
<?
do { ?>
<option value="<?= $row3[0]; ?>"><?= $row3[0] ?></option>
<? } while($row3 = mysqli_fetch_array($result3)) ?>
</select>
</td>
</tr>
</table>
<!-- Where the product rows input show go ??? -->
<table class="bottomTable">
<tr>
<th class="textCol">Product</th>
<th class="textCol">Quantity</th>
<th class="textCol">Unit Price</th>
<th class="textCol">Total Price</th>
</tr>
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } else {?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } ?>
<?
mysqli_free_result($result);
mysqli_close($db);
?>
</div>
the problem with your code is for first iteration while($row = mysqli_fetch_array($result)) the internal pointer of $result reached at the end... so for next iteration $i=1 there is nothing in the $result but As you use do-while loop the loop must run at least one time and $row[0] & $row[2] is null so you get only "-" . to fix the problem you need to change code slightly.
remove this line $row = mysqli_fetch_array($result);
and add
$options = '<option value="selectProduct">Select Product</option>';
while($row = mysqli_fetch_array($result,MYSQLI_NUM)){
$options .= '<option value="'.$row[0].'">'.$row[0].' - '.$row[1].'</option>';
}
then change like this inside for loop :
<td class="inputCol2">
<select name="'product<?= $i ?>">
<?php
echo $options;
?>
</select>
</td>

Mysql UPDATE error that only affects first row?

When I use the sql UPDATE query, it only affects the first row of my members.
<?php
$query = mysqli_query($db, "SELECT * FROM `members` ORDER BY `siteRank`");
$result = mysqli_fetch_assoc($query);
?>
<?php
if($_POST['AccountUpdate'])
{
//mysqli_query($db, "UPDATE members SET username='$Username' WHERE id='$$IdentifcationNumber' ORDER BY id DESC LIMIT 1");
echo $result['id'];
echo $result['username'];
echo 'separeator';
echo $IdentifcationNumber;
}
?>
<form method="post" action="viewprofile.php">
<table border="1px">
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>Email</th>
<th>Browser</th>
<th>IP</th>
<th>Site Rank</th>
<th>PyroCoins</th>
<th>PIN</th>
<th>Status</th>
<th>Date Joined</th>
<th>Update Account</th>
</tr>
<?php
while($result = mysqli_fetch_assoc($query)){
?>
<form method="post" action="viewprofile.php">
<tr>
<td><input readonly="readonly" value="<?php echo $result['id'];?>" /></td>
<td><?php echo $result['username'];?></td>
<td><input text="text" name="ChangePassword" value="<?php echo $result['password'];?>" /></td>
<td><?php echo $result['email'];?></td>
<td><?php echo $result['browser'];?></td>
<td><?php echo $result['ip'];?></td>
<td><?php echo $result['siteRank'];?></td>
<td><input type="text" name="ChangePyroCoins" value="<?php echo $result['PyroCoins'];?>" /></td>
<td><?php echo $result['pin'];?></td>
<td>
Current Status:
<input readonly="readonly" value="<?php echo $result['status'];?>" />
<select name="ChangeStatus">
<option value="<?php echo $result['status'];?>"><?php echo $result['status'];?></option>
<option value="[ Content Deleted ]">[ Content Deleted ]</option>
</select>
</td>
<td><?php echo $result['date'];?></td>
<td><input type="submit" name="AccountUpdate" value="Update Account"></td>
</tr>
<?php
}
?>
</table>
</form>
My $_POST data should be self explanatory, but its only affecting the first row and not the row that Im trying to affect. When I click my html update button, it only displays only the first ID and not the ID or the account credentials that i'm trying to update. For example, When I try to edit somes details with an ID of 28, it affects the first table ID which is 1
'ORDER BY id DESC LIMIT 1' in the query is unnecessary.
UPDATE members SET username='$Username' WHERE id='$IdentifcationNumber'
#Isaak Markopoulos - this is not tested but there are no nested forms so hopefully there should be less confusion when posting the form. The id field in the html has been named so at least there should be an id available in the posted vars
<html>
<head>
<title>crazy little monkey</title>
</head>
<body>
<?php
/* This only gets executed if it is a POST request */
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['AccountUpdate'] ) && isset( $_POST['IdentifcationNumber'] ) ){
/* I assume this is the correct id - there was no name for the form element previously */
$IdentifcationNumber=$_POST['IdentifcationNumber'];
$sql="UPDATE members SET username='$Username' WHERE id='$IdentifcationNumber';";
mysqli_query( $db, $sql );
echo $result['id'];
echo $result['username'];
echo 'separeator';
echo $IdentifcationNumber;
}
/* This gets executed for any request */
$query = mysqli_query( $db, "SELECT * FROM `members` ORDER BY `siteRank`;" );
$result = mysqli_fetch_assoc( $query );
?>
<!-- parent table -->
<table border="1px">
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>Email</th>
<th>Browser</th>
<th>IP</th>
<th>Site Rank</th>
<th>PyroCoins</th>
<th>PIN</th>
<th>Status</th>
<th>Date Joined</th>
<th>Update Account</th>
</tr>
<tr>
<td colspan=12>
<!-- begin a form / table for each row in rs -->
<?php
$rc=0;
while( $result = mysqli_fetch_assoc( $query ) ){
$rc++;
echo "
<!-- unique name for each form - not essential -->
<form name='vpf_{$rc}' action='viewprofile.php' method='post'>
<!-- nested child table fully contained within it's parent form element -->
<table>
<td><input type='text' name='IdentifcationNumber' readonly='readonly' value='".$result['id']."' /></td>
<td>".$result['username']."</td>
<td><input text='text' name='ChangePassword' value='".$result['password']."' /></td>
<td>".$result['email']."</td>
<td>".$result['browser']."</td>
<td>".$result['ip']."</td>
<td>".$result['siteRank']."</td>
<td><input type='text' name='ChangePyroCoins' value='".$result['PyroCoins']."' /></td>
<td>".$result['pin']."</td>
<td>
Current Status:
<input readonly='readonly' value='".$result['status']."' />
<select name='ChangeStatus'>
<option value='".$result['status']."'>".$result['status']."
<option value='[ Content Deleted ]'>[ Content Deleted ]
</select>
</td>
<td>".$result['date']."></td>
<td><input type='submit' name='AccountUpdate' value='Update Account'></td>
</table>
</form>";
}/* Close the while loop - note that the form(s) is/are FULLY contained within the loop - NO nesting */
?>
</td>
</tr>
</table>
</body>
</html>

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