I have an HTML table which is getting data from MySql.
existingbankproducts table is below:
I have 2 calculated fields which need to sum up the numbers that I get. The fields that are needed to calculate is Balance and MonthlyCommitment.
The code I use to get the result from MySql is below:
<?php
$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
. "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
. "WHERE apd.AccountID =:accountId AND apd.applicantType ='main';");
$stmt2->bindParam(':accountId', $accountId, PDO::PARAM_INT);
//if account id data type is varchar change the last parameter to `PDO::PARAM_str`
$stmt2->execute();
if ($stmt2->rowCount() > 0) {
?>
<table>
<tr>
<th>Financial Institution</th>
<th>Product Type</th>
<th>Balance</th>
<th>Monthly Commitment</th>
</tr>
<?php
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><input type = "text" name = "finanIns1" id = "finanIns1" value="<?php echo $row['FinanicalInstituion']; ?>" readonly></td>
<td>
<input list = "proTypeList" name = "proType1" id = "proType1" value="<?php echo $row['ProductType']; ?>"readonly >
</td>
<td id = "balance"><input type = "number" name = "balance1" id = "balance1" value="<?php echo $row['Balance']; ?>"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value="<?php echo $row['MonthlyCommitment']; ?>"readonly></td>
</tr>
<?php
}
} else {
?>
<div class="">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> No Data Found ...
</div>
</div>
<?php
}
?>
<?php
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$totalBalance = 0;
$totalBalance += $row['Balance'];
$totalMonthlyComm = 0;
$totalMonthlyComm +=$row['MonthlyCommitment'];
?>
<tr>
<td>Total:</td>
<td><input readonly></td>
<td id="balance"><input type="number" name="totalBalance" id="totalBalance" value="<?php echo $totalBalance; ?>" min="0" readonly></td>
<td id="MonthyComm"><input type="number" name="totalMonthlyComm" id="totalMonthlyComm" value="<?php echo $totalMonthlyComm; ?>" min="0" readonly></td>
</tr>
<?php
}
?>
</table>
I am able to retrieve the data from MySql. However, it is not able to sum up the totalBalance and totalMonthlyComm at the end of my table.
You only need to loop through the database result only once:
<?php
$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
. "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
. "WHERE apd.AccountID =:accountId AND apd.applicantType ='main';");
$stmt2->bindParam(':accountId', $accountId, PDO::PARAM_INT);
//if account id data type is varchar change the last parameter to `PDO::PARAM_str`
$stmt2->execute();
$totalBalance = 0;
$totalMonthlyComm = 0;
if ($stmt2->rowCount() > 0) {
?>
<table>
<tr>
<th>Financial Institution</th>
<th>Product Type</th>
<th>Balance</th>
<th>Monthly Commitment</th>
</tr>
<?php
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$totalBalance += $row['Balance'];
$totalMonthlyComm +=$row['MonthlyCommitment'];
?>
<tr>
<td><input type="text" name="finanIns1" id = "finanIns1" value="<?php echo $row['FinanicalInstituion']; ?>" readonly></td>
<td><input list="proTypeList" name="proType1" id="proType1" value="<?php echo $row['ProductType']; ?>" readonly></td>
<td id="balance"><input type="number" name="balance1" id="balance1" value="<?php echo $row['Balance']; ?>" readonly></td>
<td id="MonthyComm"><input type="number" name="monthlyComm1" id="monthlyComm1" value="<?php echo $row['MonthlyCommitment']; ?>" readonly></td>
</tr>
<?php
}
?><tr>
<td>Total:</td>
<td><input readonly></td>
<td id="balance"><input type="number" name="totalBalance" id="totalBalance" value="<?php echo $totalBalance; ?>" min="0" readonly></td>
<td id="MonthyComm"><input type="number" name="totalMonthlyComm" id="totalMonthlyComm" value="<?php echo $totalMonthlyComm; ?>" min="0" readonly></td>
</tr>
</table>
<?php
} else {
?>
<div class="">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> No Data Found ...
</div>
</div>
<?php
} ?>
Change your lines from while as :
<?php
$totalBalance = 0;
$totalMonthlyComm = 0;
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$totalBalance += $row['Balance'];
$totalMonthlyComm +=$row['MonthlyCommitment'];
}
?>
<tr>
<td>Total:</td>
<td><input readonly></td>
<td id="balance"><input type="number" name="totalBalance" id="totalBalance" value="<?php echo $totalBalance; ?>" min="0" readonly></td>
<td id="MonthyComm"><input type="number" name="totalMonthlyComm" id="totalMonthlyComm" value="<?php echo $totalMonthlyComm; ?>" min="0" readonly></td>
</tr>
Related
I have a table of products (data pulled from SQL database) with this fields: ID, Name, Price.
After i pulled the data i have a QTY cell for each product...
I want to make a cell at the bottom to sum all the qty*prodcut price to get total price of the order...
That's my table:
<table>
<tr>
<td>Name</td>
<td>Price</td>
<td>Qty</td>
</tr>
<form action="sendorder.php?supid=<?php echo $supid; ?>" method="post">
<?php
while($row = mysqli_fetch_array($result)) {
$prodname = $row['name'];
$supid = $row['supplier'];
$prodid = $row['id'];
$prodprice = $row['price'];
?>
<tr>
<td>
<input type="checkbox" name="prod[]" id="prod[<?php echo $prodid; ?>]" value="<?php echo $prodid; ?>">
<label for="prod[<?php echo $prodid; ?>]"><?php echo $prodname; ?></label>
</td>
<td>
<?php echo number_format($prodprice, 2); ?><label for="prod[<?php echo $prodid; ?>]"> NIS</label>
</td>
<td>
<label for="prod[<?php echo $prodid; ?>]"><input style="font-size: 12px;" type="text" name="qty_<?php echo $prodid; ?>" placeholder="Qty" minlength="1" maxlength="3" size="2"></label>
</td>
</tr>
<?php
}
?>
<tr>
<td><b>Total Price:</b></td>
<td rowspan="1" colspan="2">XXX NIS</td> \\ HERE I WANT TO MAKE THE LIVE CALCLUTION
</tr>
<tr>
<td rowspan="1" colspan="3">
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</form>
tnx for everyone, i found this LINK
and that's excatly what i was looking for.
function getTotal(){
var total = 0;
$('.sum').each(function(){
total += parseFloat(this.innerHTML)
});
$('#total').text(total);
}
getTotal();
$('.qty').keyup(function(){
var parent = $(this).parents('tr');
var price = $('.price', parent);
var sum = $('.sum', parent);
var value = parseInt(this.value) * parseFloat(price.get(0).innerHTML||0);
sum.text(value);
getTotal();
})
add $sum = 0; variable before while and put $sum += $row['price']; inside your while you can access $sum in Total Price <td>.
maybe you need int cast for $row['price'].
<table>
<tr>
<td>Name</td>
<td>Price</td>
<td>Qty</td>
</tr>
<form action="sendorder.php?supid=<?php echo $supid; ?>" method="post">
<?php
$sum = 0;
while($row = mysqli_fetch_array($result)) {
$prodname = $row['name'];
$supid = $row['supplier'];
$prodid = $row['id'];
$prodprice = $row['price'];
$sum += $row['price'];
?>
<tr>
<td>
<input type="checkbox" name="prod[]" id="prod[<?php echo $prodid; ?>]" value="<?php echo $prodid; ?>">
<label for="prod[<?php echo $prodid; ?>]"><?php echo $prodname; ?></label>
</td>
<td>
<?php echo number_format($prodprice, 2); ?><label for="prod[<?php echo $prodid; ?>]"> NIS</label>
</td>
<td>
<label for="prod[<?php echo $prodid; ?>]"><input style="font-size: 12px;" type="text" name="qty_<?php echo $prodid; ?>" placeholder="Qty" minlength="1" maxlength="3" size="2"></label>
</td>
</tr>
<tr>
<td><b>Total Price:</b></td>
<td rowspan="1" colspan="2"><?php echo $sum; ?></td> \\ HERE I WANT TO MAKE THE LIVE CALCLUTION
</tr>
<?php
}
?>
<tr>
<td rowspan="1" colspan="3">
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</form>
UPDATE:
I have managed to correct some things. But only the first row is inserted. If the first row is not selected, nothing gets inserted.
I want only selected checkboxes from the populated table inserted into the a new table.
My table is as follows:
<table id="simple-table" class="table table-striped table-condensed responsive">
<thead>
<tr>
<th style="width:5%" class="center">
<label class="pos-rel">
<input type="checkbox" name="checked" class="ace" />
<span class="lbl"></span>
</label>
</th>
<th style="width:32%">Student Name</th>
<th style="width:13%">Adm. No</th>
<th style="width:10%" class="center">CA1 (10%)</th>
<th style="width:10%" class="center">CA2 (10%)</th>
<th style="width:10%" class="center">CA3 (10%)</th>
<th style="width:10%" class="center">Exam (70%)</th>
<th style="width:10%" class="center">Total (100%)</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_POST['loadStudents'])){
$session = clean($_POST["session"]);
$term = clean($_POST["term"]);
$c_taught = clean($_POST["c_taught"]);
$s_taught = clean($_POST["s_taught"]);
$process_limit = clean($_POST["process_limit"]);
$session_phrase = "Session";
$sql = "SELECT `id`, `StudentID`, `StudentName` FROM tbl_students WHERE `StudentClass` = '".$c_taught."' ORDER BY `StudentName` ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$cnt=1;
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$student_name = $row['StudentName'];
$student_id = $row['StudentID'];
?>
<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace" name="checked[]" value="<?php echo $row['id'];?>" />
<span class="lbl"></span>
</label>
</td>
<td><?php echo $student_name; ?><input type="hidden" name="student_name[]" value="<?php echo $row['StudentName']; ?>"/></td>
<td><?php echo $student_id; ?><input type="hidden" name="student_id[]" value="<?php echo $row['StudentID']; ?>"/></td>
<td class="center"><?php echo '<input type="text" maxlength="2" size="6" name="CA1[]" autofocus>'; ?></td>
<td class="center"><?php echo '<input type="text" maxlength="2" size="6" name="CA2[]">'; ?></td>
<td class="center"><?php echo '<input type="text" maxlength="2" size="6" name="CA3[]">'; ?></td>
<td class="center"><?php echo '<input type="text" maxlength="2" size="6" name="Exam[]">'; ?></td>
<td class="center"><?php echo '<input type="text" maxlength="2" size="6" name="Total[]">'; ?></td>
</tr>
<?php $cnt=$cnt+1;}}
else {
$msg = "<span class='red'><h4> No data available for your selection. </h4></span>";
}
}
?>
</tbody>
</table>
My inset script is as follows: (On the same page with the form)
<?php
$enroll_sql = "";
if(isset($_POST['add_assessment'])) {
if(empty($_POST['checked'])){
echo '<script>alertify.alert("No Student is selected.",function(e){if(e){document.location.href = "cass_entry.php";}}).set("labels", {ok:"OK!"}).set("defaultFocus", "ok").set("title", "Assessment")
</script>';
exit();
}
foreach($_POST['checked'] as $id=>$value) {
$session = $_POST['session'];
$term = $_POST['term'];
$c_taught = $_POST['c_taught'];
$s_taught = $_POST['s_taught'];
$student_id = $_POST['student_id'][$id];
$student_name = $_POST['student_name'][$id];
$ca_1 = $_POST['CA1'][$id];
$ca_2 = $_POST['CA2'][$id];
$ca_3 = $_POST['CA3'][$id];
$exam = $_POST['Exam'][$id];
$total = $_POST['Total'][$id];
$enroll_sql .= '
INSERT INTO tbl_subjects_enrollment (`Session`,`Term`,`Student_Class`,`Subject_Name`,`Student_ID`,`Student_Name`,`CA_1`,`CA_2`,`CA_3`,`Exam`,`Total`)
VALUES("'.$session.'", "'.$term.'", "'.$c_taught.'", "'.$s_taught.'", "'.$student_id.'", "'.$student_name.'", "'.$ca_1.'", "'.$ca_2.'", "'.$ca_3.'", "'.$exam.'", "'.$total.'")';
echo $enroll_sql;
if (mysqli_multi_query($conn, $enroll_sql)) {
$success_msg = '<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">
<i class="ace-icon fa fa-times"></i>
</button>
<h4><i class="ace-icon fa fa-check"> Success</i></h4>
<p>Assessment scores were successfully saved.</p></div>';
} else {
echo "Error saving Assessment: " . $conn->error;
}
}
}
?>
Watch, your first checkbox isn't named as an array and has no value :
<input type="checkbox" name="checked" class="ace" />
It may result in a simple var $_POST['checked'] which isn't an array (nor a string since it hasn't value) and won't be passed in your foreach.
Look :
foreach($_POST['checked'] as $id=>$value) {
$enroll_sql .= '
INSERT INTO tbl_subjects_enrollment (`Session`,`Term`,`Student_Class`,`Subject_Name`,`Student_ID`,`Student_Name`,`CA_1`,`CA_2`,`CA_3`,`Exam`,`Total`)
VALUES("'.$session.'", "'.$term.'", "'.$c_taught.'", "'.$s_taught.'", "'.$student_id.'", "'.$student_name.'", "'.$ca_1.'", "'.$ca_2.'", "'.$ca_3.'", "'.$exam.'", "'.$total.'")';
echo $enroll_sql;
if (mysqli_multi_query($conn, $enroll_sql)) {
$success_msg = '<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">
<i class="ace-icon fa fa-times"></i>
</button>
<h4><i class="ace-icon fa fa-check"> Success</i></h4>
<p>Assessment scores were successfully saved.</p></div>';
} else {
echo "Error saving Assessment: " . $conn->error;
}
}
You concatenate your SQL query each time => and execute it each time adding the previous queries.
If you didn't forget your semi-colon it would have ended inserting too many time the same line (and actually that's why only the first Insert is working).
The problem here is the concatenation of the query AND the missing semi-colon of your query.
In order to make your code work fine, you juste have to not concatenate your query :
$enroll_sql = '...'; // = not .=
OR
let it concatenated BUT execute the query OUTSIDE your foreach loop (and adding a semi-colon at the end of the SQL query :
, "'.$total.'");'; // ; at the end of the query
I wanna thank all contributors especially #WizardNx.
I eventually solved it (Necessity is the mother of creativity):
It appears the checkbox was not assigning unique id to each row. In fact, if I check row_3 and fill all the inputs, it will insert for row_1 but assign row_3 id.
Here is what I did:
<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace" name="studentSelect[<?php echo $row['id']; ?>]" value="<?php echo $row['StudentID']; ?>" />
<span class="lbl"></span>
</label>
</td>
<td><?php echo $row['StudentName']; ?><input type="hidden" name="student_name[<?php echo $row['id']; ?>]" value="<?php echo $row['StudentName']; ?>"/></td>
<td><?php echo $row['StudentID']; ?></td>
<td class="center"><input type="text" maxlength="2" size="6" name="CA1[<?php echo $row['id']; ?>]" autofocus></td>
<td class="center"><input type="text" maxlength="2" size="6" name="CA2[<?php echo $row['id']; ?>]"></td>
<td class="center"><input type="text" maxlength="2" size="6" name="CA3[<?php echo $row['id']; ?>]"></td>
<td class="center"><input type="text" maxlength="2" size="6" name="Exam[<?php echo $row['id']; ?>]"></td>
<td class="center"><input type="text" maxlength="2" size="6" name="Total[<?php echo $row['id']; ?>]"></td>
</tr>
I added id to each input on each row.
Also, how can I access the <td> for each value?
This is my table in HTML:
<table class="inventory" style="float:left; position:relative;">
<tbody>
<tr>
<th>Name</th>
<th>Rate</th>
<th>OT</th>
<th>Leaves</th>
<th>Total</th>
</tr>
<tr>
<td>
<input type="text" placeholder="Name"/>
</td>
<td>
<input type="text" placeholder="Rate"/>
</td>
<td>
<input type="text" placeholder="OT"/>
</td>
<td>
<input type="text" placeholder="Leaves"/>
</td>
<td>
<input type="text" placeholder="Total"/>
</td>
</tr>
<tr>
<td>
<input type="text" placeholder="Name"/>
</td>
<td>
<input type="text" placeholder="Rate"/>
</td>
<td>
<input type="text" placeholder="OT"/>
</td>
<td>
<input type="text" placeholder="Leaves"/>
</td>
<td>
<input type="text" placeholder="Total"/>
</td>
</tr>
</tbody>
</table>
How can I add rows in my HTML table equal to the number of entries I have in Mysql DB?
I am assuming your database connection as $con. Place your database table name and column name in appropriate place.
<table class="inventory" style="float:left; position:relative;">
<tbody>
<tr>
<th>Name</th>
<th>Rate</th>
<th>OT</th>
<th>Leaves</th>
<th>Total</th>
</tr>
<?php
$query = mysqli_query($con,"SELECT * FROM tableName");
while($row = mysqli_fetch_array($query,MYSQLI_ASSOC))
{?>
<tr>
<td>
<input type="text" placeholder="Name" value="<?php echo $row['Name_Column_name']?>"/>
</td>
<td>
<input type="text" placeholder="Rate" value="<?php echo $row['Rate_Column_name']?>"/>
</td>
<td>
<input type="text" placeholder="OT" value="<?php echo $row['OT_Column_name']?>"/>
</td>
<td>
<input type="text" placeholder="Leaves" value="<?php echo $row['Leaves_Column_name']?>"/>
</td>
<td>
<input type="text" placeholder="Total" value="<?php echo $row['Total_Column_name']?>"/>
</td>
</tr>
<?php }?>
</tbody>
</table>
You can use this coding adn edit it as per your requirement
<table>
<tr>
<td>Name</td>
<td>Rate</td>
<td>OT</td>
<td>Leaves</td>
<td>Total</td>
</tr>
<?php
$total_row=mysql_num_rows(mysql_query("SELECT * FROM table_name"));
$q="SELECT * FROM table_name";
$run=mysql_query($q);
if($total_row > 0)
{
while($row1=mysql_fetch_assoc($run))
{
$Name=$row1['Name'];
$Rate=$row1['Rate'];
$OT=$row1['OT'];
$Leaves=$row1['Leaves'];
$Total=$row1['Total'];
?>
</tr>
<tr>
<td><?php echo $Name;?></td>
<td><?php echo $Rate;?></td>
<td><?php echo $OT;?></td>
<td><?php echo $Leaves;?></td>
<td><?php echo $Total;?></td>
</tr>
<?php }
}?>
</table>
Try with something like this:
$sql = "SELECT..."; //Add your query. Presumed you have 'name' column
$result = mysql_query($sql);
echo '<table class="inventory" style="float:left; position:relative;">';
echo '<thead>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<th>'.$row["name"].'</th>';
echo '</tr>';
}
echo '</thead>';
echo '<tbody>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>';
echo '<input type="text" placeholder="'.$row["name"].'"/>';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
I am currently trying to write a system where I can add editable fields from the database onto a webpage then submit the information and change the data that's in the database.
I have got the data to be shown in the text fields but I cannot seem to get the update function to work...
<?php $mysqli = new mysqli('localhost', 'adwawdaw', 'awdawdaw', 'awdawdaw');
$result = $mysqli->query('SELECT * FROM information WHERE id = 1');
if (is_object($result)) {
if ($result->num_rows) {
while ($row = $result->fetch_assoc()) {
$banner = $row['banner'];
$embed = $row['embed'];
$title = $row['title'];
$date = $row['date'];
$time = $row['time'];
$description = $row['description'];
$region = $row['region'];
$sponsors = $row['sponsors'];
}
}
}
?>
<form method="post" action="edit1.php">
<table>
<br />
<tr>
<td>Banner: </td>
<td><input type="text" name="banner" value="<?php echo htmlspecialchars($banner) ?>"></td>
</tr>
<tr>
<td>Embed: </td>
<td><input type="text" name="embed" value="<?php echo htmlspecialchars($embed) ?>"></td>
</tr>
<tr>
<td>Event Title: </td>
<td><input type="text" name="title" value="<?php echo htmlspecialchars($title) ?>"></td>
</tr>
<tr>
<td>Date: </td>
<td><input type="text" name="date" value="<?php echo htmlspecialchars($date) ?>"></td>
</tr>
<tr>
<td>Time: </td>
<td><input type="text" name="time" value="<?php echo htmlspecialchars($time) ?>"></td>
</tr>
<tr>
<td>Description: </td>
<td><input type="text" name="description" value="<?php echo htmlspecialchars($description) ?>"></td>
</tr>
<tr>
<td>Region: </td>
<td><input type="text" name="region" value="<?php echo htmlspecialchars($region) ?>"></td>
</tr>
<tr>
<td>Sponsors: </td>
<td><input type="text" name="sponsors" value="<?php echo htmlspecialchars($sponsors) ?>"></td>
</tr>
</table>
<input class="button" type="submit" value="Update!">
</form>
<?php
$banner = $_POST['banner'];
$embed = $row['embed'];
$title = $_POST['title'];
$date = $row['date'];
$time = $row['time'];
$description = $row['description'];
$region = $row['region'];
$sponsors = $row['sponsors'];
$mysqli->query('UPDATE livestreamadmin SET title = $title WHERE id=1');
?>
Please look into this code unable to find out the actual error:
This is PHP upload code:
<?php
include_once("config.php");
if(isset($_GET['pro_id']))
{
$id=$_GET['pro_id'];
if(isset($_POST['submitBtn'])) {
$dept_id = $_POST['dept_id'];
$cat_id = $_POST['cat_id'];
/*$pro_id = $_POST['pro_id'];*/
$pro_name = $_POST['pro_name'];
$pro_desc = $_POST['pro_desc'];
$pro_spec = $_POST['pro_spec'];
$pro_price = $_POST['pro_price'];
$status = 'on';
$pro_keywords = $_POST['pro_keywords'];
//image names
$pro_image = $_FILES['pro_image']['name'];
//temp images names
$temp_image = $_FILES['pro_image']['tmp_name'];
if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR $pro_image=='' OR $pro_keywords=='')
{
echo "<script>alert('All the fields are mandatory')</script>";
exit();
}
else
{
//upload image to folder
move_uploaded_file($temp_image,"images/product_images/$pro_image");
$run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values ( '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'");
if($run_query1)
{
echo "<script>alert('Product updated successfully')</script>";
exit();
}
else
{
echo "<script>alert('Errors')</script>";
}
} }
$query1 = mysqli_query($login, "select * from products1 where pro_id='$id'");
$query2 = mysqli_fetch_array($query1);
?>
This the form Part where data retrieve from table and when click on the update button nothing happened and page is redirected to view data page and showing the old data:
<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="650" border="0">
<tr>
<td width="183" align="right">Department:</td>
<th width="231" align="left">
<select name="dept_id" id="dept_id">
<option>Select Department</option>
<?php
$result=dept_show();
while($row=mysqli_fetch_assoc($result))
{
echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="183" align="right">Catagory</td>
<th width="231" align="left">
<select name="cat_id" id="cat_id">
<option>Select Catagory</option>
<?php
$result1=cat_show();
while($row=mysqli_fetch_assoc($result1))
{
echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
}
?>
</select></th></tr>
<tr>
<!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>-->
</tr>
<tr>
<td align="right">Product Name/Model:</td>
<td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Description:</td>
<td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td>
</tr>
<tr>
<td align="right">Products Specification:</td>
<td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td>
</tr>
<tr>
<td align="right">Product Price:</td>
<td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Image:</td>
<td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td></td>
<td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td align="right">Keywords:</td>
<td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td>
</tr>
</table>
</form>
</div> <?php } ?>
</td>
</tr>
</table>
</div>
Form method is POST and you are using GET method in if loop
if(isset($_GET['pro_id']))
Use POST here.
I have made the changes in you complete code. Use this code and if required made the changes (if issues arrived)
PHP code
<?php
include_once("config.php");
if(isset($_POST['submitBtn']))
{
$dept_id = $_POST['dept_id'];
$cat_id = $_POST['cat_id'];
$pro_id = $_POST['pro_id'];*/
$pro_name = $_POST['pro_name'];
$pro_desc = $_POST['pro_desc'];
$pro_spec = $_POST['pro_spec'];
$pro_price = $_POST['pro_price'];
$status = 'on';
$pro_keywords = $_POST['pro_keywords'];
//image names
$pro_image = $_FILES['pro_image']['name'];
//temp images names
$temp_image = $_FILES['pro_image']['tmp_name'];
if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR $pro_image=='' OR $pro_keywords=='')
{
echo "<script>alert('All the fields are mandatory')</script>";
exit();
}
else
{
//upload image to folder
move_uploaded_file($temp_image,"images/product_images/$pro_image");
$run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values ( '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'");
if($run_query1)
{
echo "<script>alert('Product updated successfully')</script>";
exit();
}
else
{
echo "<script>alert('Errors')</script>";
}
}
}
$query2 = array();
if(isset($_GET['pro_id']))
{
$id=$_GET['pro_id'];
$query1 = mysqli_query($login, "select * from products1 where pro_id='$id'");
$query2 = mysqli_fetch_array($query1);
}
?>
HTML Code
<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="650" border="0">
<tr>
<td width="183" align="right">Department:</td>
<th width="231" align="left">
<select name="dept_id" id="dept_id">
<option>Select Department</option>
<?php
$result=dept_show();
while($row=mysqli_fetch_assoc($result))
{
echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="183" align="right">Catagory</td>
<th width="231" align="left">
<select name="cat_id" id="cat_id">
<option>Select Catagory</option>
<?php
$result1=cat_show();
while($row=mysqli_fetch_assoc($result1))
{
echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>
</tr>
<tr>
<td align="right">Product Name/Model:</td>
<td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Description:</td>
<td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td>
</tr>
<tr>
<td align="right">Products Specification:</td>
<td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td>
</tr>
<tr>
<td align="right">Product Price:</td>
<td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Image:</td>
<td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td></td>
<td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td align="right">Keywords:</td>
<td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td>
</tr>
</table>
</form>
</div> <?php } ?>
</td>
</tr>
</table>
Your pro_id field is commented out in your HTML using <!-- and -->, so the following never is true:
if(isset($_GET['pro_id']))
Also, you have a mismatch between your form method POST and $_GET that you are looking for.
Your query of update is correct? I think you must use
UPDATE products1 SET dept_id='$dept_id',cat_id ='$cat_id'... the rest of values
WHERE pro_id='$id'
And verify if your dept_id is INT as well cat_id, so if they are INT you don't need ''
UPDATE products1 SET dept_id=$dept_id,cat_id =$cat_id
Try to do this steps:
First thing comment out this line,
<!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>-->
next step,
you are sending data using POST i.e. (form method="post"), so use this
if(isset($_POST['pro_id'])) , then comment out $pro_id = $_POST['pro_id'];
you will get $pro_id value.