I am trying to filter use a form to filter a database then provide a way to update the filtered data. I may not being doing it the best way, but it is close to working. The problem seems to be that when I press the "Update" button, a few variables are cleared; count and ID.
I have fixed count by making it a session variable, but there isn't a way to do it with ID. I am not sure why count gets erased, so I would appreciate if someone could tell me.
The update part of my code works perfectly when used without the filter part, again, the problem is that the id variable is empty. I have tested all other variables and put a constant in for ID for testing in my update statement. Here is my code.
<?php
session_start();
?>
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="inventory"; // Database name
$tbl_name="computers"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$school=$_POST['school'];
$make=$_POST['make'];
$model=$_POST['model'];
$barcode=$_POST['barcode'];
$location=$_POST['location'];
?>
<?php
include 'nav-bar.php';
?>
<h2 align="center">Filter Computers</h2>
<form name="form" method="post" style="margin: 0; text-align: center;">
<p><label>School Name:</label><input type="text" name="school" size="8" id="school" tabindex="1"</p>
<p><label>Make:</label><input type="text" name="make" size="25" id="make" tabindex="1"</p>
<p><label>Model:</label><input type="text" name="model" size="25" id="model" tabindex="1"</p>
<p><label>Barcode:</label><input type="text" name="barcode" size="12" id="barcode" tabindex="1"</p>
<p><label>Location:</label><input type="text" name="location" size="25" id="location" tabindex="1"</p>
<p><label>Serial:</label><input type="text" name="serial" size="25" id="location" tabindex="1"</p>
<p><label>Date Acquired yyyy-dd-mm:</label><input type="text" name="date" size="8" id="location" tabindex="1"</p>
<p><label>Processor:</label><input type="text" name="processor" size="25" id="location" tabindex="1"</p>
<p><label>RAM:</label><input type="text" name="ram" size="25" id="location" tabindex="1"</p>
<p><input align="center" type="submit" name="Filter" value="Filter">
</form>
<?php
if($_POST['Filter']){
$sql = "SELECT * FROM $tbl_name WHERE school like '%$school' AND make like '%$make' AND model like '%$model' AND location like '%$location'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
$_SESSION['count']=$count;
?>
<strong>Update Multiple Computers</strong><br>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>School</strong></td>
<td align="center"><strong>Make</strong></td>
<td align="center"><strong>Model</strong></td>
<td align="center"><strong>Barcode</strong></td>
<td align="center"><strong>Location</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="center"><input name="school[]" type="text" id="school" value="<?php echo $rows['school']; ?>"></td>
<td align="center"><input name="make[]" type="text" id="make" value="<?php echo $rows['make']; ?>"></td>
<td align="center"><input name="model[]" type="text" id="model" value="<?php echo $rows['model']; ?>"></td>
<td align="center"><input name="barcode[]" type="text" id="barcode" value="<?php echo $rows['barcode']; ?>"></td>
<td align="center"><input name="location[]" type="text" id="location" value="<?php echo $rows['location']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Update" value="Update"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
}
// Check if button name "Update" is active, do this
if(isset($_POST['Update'])){
for($i=0;$i<$_SESSION['count'];$i++){
$sql1="UPDATE $tbl_name SET school='$school[$i]', make='$make[$i]', model='$model[$i]' , barcode='$barcode[$i]' , location='$location[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
session_destroy();
}
if(isset($result1)){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=update_multiple.php\">";
}
?>`
Any help is surely appreciated.