pagination on php limit 50 - php

Can anyone help me with the pagination? I am trying this code on pagination and the pages are showing but when clicked on the pages like Next or the number with links it gives a syntax error.
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?page=2' at line 1"
My tables data is around 250 and I wanted to limit it to 50 data per page.
Here is my code: (pagination section - is the problem)
<link rel="stylesheet" type="text/css" href="css/navbar.css">
<?php include 'navbar.php';?>
<br>
<?php
if(!isset($_GET['table'])){
echo 'You must assign a table to view.';
exit;
}
session_start();
//Connect here
$conn = mysqli_connect("localhost", "root", "", "dkusers");
$table = mysqli_real_escape_string($conn, $_GET['table']);
$fields = array();
$q = mysqli_query($conn, "SHOW COLUMNS FROM " . $table) or die(mysqli_error($conn));
while($r = mysqli_fetch_assoc($q)) $fields[count($fields)] = $r['Field'];
echo '<b><font size="4">Table: </b>', $table, '</font><br>';
// -----------------INSERT-----------------
if(isset($_POST['action']) && $_POST['action'] == "Insert"){
if(!isset($_POST['insert'])){
echo '<h3>Insert Row</h3>';
echo '<form method="post"><input type="hidden" name="action" value="' . $_POST['action'] . '" />';
echo '<table border="1" cellpadding="7"><tr><th>Field</th><th>Value</th><th>MD5</th></tr>';
foreach($fields as $key => $value){
echo '<tr><td>' . $value . ':</td><td><input type="text" name="field_' . $value . '" /></td><td><input type="checkbox" name="md5_' . $value . '" /></td></tr>';
}
echo '<tr><td><input type="submit" name="insert" value="Submit" /></td><td colspan="2">Back</td></tr></table></form>';
exit;
}else{
$first = true;
$query = "INSERT INTO " . $table;
foreach($_POST as $key => $value){
if(strrpos($key, "field_", -strlen($key)) !== false){
$key = substr($key, 6);
$query .= sprintf("%s%s", ($first) ? " (" : ",", $key);
$first = false;
}
}
$query .= ") VALUES";
$first = true;
foreach($_POST as $key => $value){
if(strrpos($key, "field_", -strlen($key)) !== false){
$key = substr($key, 6);
$query .= sprintf("%s'%s'", ($first) ? " (" : ",", (isset($_POST['md5_' . $key])) ? md5($value) : $value);
$first = false;
}
}
$q = mysqli_query($conn, $query . ")");
if($q) echo 'Successfully inserted row into table!<br/><br/>'; else echo mysqli_error($conn) . '<br/><br/>';
}
}
// -----------------DELETE-----------------
if(isset($_POST['action']) && $_POST['action'] == "Delete"){
if(!isset($_POST['rows'])){
echo 'You didn\'t send any rows to delete.<br/><br/>';
}else{
$count = 0;
for($i = 0;$i < count($_POST['rows']);$i++){
if($_POST['rows'][$i] >= count($_SESSION['store'])) continue;
$query = "DELETE FROM " . $table . "";
$row = $_SESSION['store'][$_POST['rows'][$i]];
$first = true;
foreach($row as $key => $value){
$query .= sprintf(" %s %s = '%s'", ($first) ? "WHERE" : "AND", $key, $value);
$first = false;
}
$q = mysqli_query($conn, $query . " LIMIT 1");
if(!$q) echo mysqli_error($conn) . '<br/>';
$count += mysqli_affected_rows($conn);
}
echo 'Successfully deleted ' . $count . ' row(s)!<br/><br/>';
}
}
// -----------------MODIFY-----------------
if(isset($_POST['action']) && $_POST['action'] == "Modify"){
if(!isset($_POST['rows'])){
echo 'You didn\'t send any rows to modify.<br/><br/>';
}else if(isset($_POST['modify'])){
$count = 0;
for($i = 0;$i < count($_POST['rows']);$i++){
if($_POST['rows'][$i] >= count($_SESSION['store'])) continue;
$first = true;
$query = "UPDATE " . $table . " SET";
foreach($_POST as $key => $value){
if(strrpos($key, "field_", -strlen($key)) !== false){
$key = explode("_", $key, 3);
if($key[1] == $i){
$query .= sprintf(((!$first) ? "," : "") . " %s = '%s'", $key[2], (isset($_POST['md5_' . $key[1] . '_' . $key[2]])) ? md5($value) : $value);
$first = false;
}
}
}
$row = $_SESSION['store'][$_POST['rows'][$i]];
$first = true;
foreach($row as $key => $value){
$query .= sprintf(" %s %s = '%s'", ($first) ? "WHERE" : "AND", $key, $value);
$first = false;
}
$q = mysqli_query($conn, $query . " LIMIT 1");
if(!$q) echo mysqli_error($conn) . '<br/>';
$count += mysqli_affected_rows($conn);
}
echo 'Successfully updated ' . $count . ' row(s)!<br/><br/>';
}else{
echo '<h3>Modify Row</h3>';
echo '<form method="post"><input type="hidden" name="action" value="' . $_POST['action'] . '" />';
for($i = 0;$i < count($_POST['rows']);$i++) if($_POST['rows'][$i] < count($_SESSION['store'])) echo '<input type="hidden" name="rows[]" value="' . $_POST['rows'][$i] . '" />';
echo '<table border="1" cellpadding="7"><tr><th>Field</th><th>Value</th><th>MD5</th></tr>';
for($i = 0;$i < count($_POST['rows']);$i++){
if($_POST['rows'][$i] >= count($_SESSION['store'])) continue;
if($i != 0) echo '<tr><td colspan="3"><hr/></td></tr>';
$row = $_SESSION['store'][$_POST['rows'][$i]];
foreach($row as $key => $value){
echo '<tr><td>' . $key . ':</td><td><input type="text" name="field_' . $i . '_' . $key . '" value="' . $value . '" /></td><td><input type="checkbox" name="md5_' . $i . '_' . $key . '" /></td></tr>';
}
}
echo '<tr><td><input type="submit" name="modify" value="Submit" /></td><td colspan="2">Back</td></tr></table></form>';
exit;
}
}
// -----------------SEARCH-----------------
echo '<br><form method="post">Search: <input type="text" name="filter" value="' . ((isset($_POST['filter'])) ? $_POST['filter'] : '') . '"/><br/>Filter by: <br/>';
for($i = 0;$i < count($fields);$i++) echo '<input type="checkbox" name="' . $fields[$i] . '"' . ((isset($_POST['filter']) && isset($_POST[$fields[$i]])) ? ' checked' : '') . '/>' . $fields[$i] . ' ';
echo '</form><form method="post"><table border="1" cellpadding="7"><tr>';
for($i = 0;$i < count($fields);$i++) echo '<th>' . $fields[$i] . '</th>';
echo '<th>-</th></tr>';
$sql = "SELECT * FROM " . $table;
if(isset($_POST['filter'])){
$filter = mysqli_real_escape_string($conn, $_POST['filter']);
foreach($fields as $key => $value) if(!isset($_POST[$fields[$key]])) unset($fields[$key]);
if(count($fields) > 0){
$first = true;
foreach($fields as $key => $value){
$sql .= " " . (($first) ? "WHERE" : "OR") . " " . $value . " LIKE '%" . $filter . "%'";
$first = false;
}
}
}
// -----------------Bottoms (Above table)-----------------
echo '<input type="submit" name="action" value="Modify" /> <input onclick="return confirm(\'Are you sure you want to delete these rows?\')" type="submit" name="action" value="Delete" /> <input type="submit" name="action" value="Insert" /><br><br></form>';
$_SESSION['store'] = array();
$q = mysqli_query($conn, $sql) or die(mysqli_error($conn));
while($r = mysqli_fetch_assoc($q)){
echo '<tr>';
foreach($r as $key => $value) echo '<td>' . $value. '</td>';
echo '<td><input type="checkbox" name="rows[]" value="' . count($_SESSION['store']) . '" /></td></tr>';
$_SESSION['store'][count($_SESSION['store'])] = $r;
}
// -----------------Bottoms (Below table)-----------------
echo '</table>';
//echo '<br><input type="submit" name="action" value="Modify" /> <input onclick="return confirm(\'Are you sure you want to delete these rows?\')" type="submit" name="action" value="Delete" /> <input type="submit" name="action" value="Insert" /></form>';
?>
<br>
// -----------------Pagination-----------------
<br>
<?php
$start=0;
$limit=50;
if(isset($_GET['page']))
{
$page=$_GET['page'];
$start=($page-1)*$limit;
}
else{
$page=1;
}
//Fetch from database first 10 items which is its limit. For that when page open you can see first 10 items.
$query=mysqli_query($conn,"select * from $table LIMIT $start, $limit");
?>
<?php
//print 10 items
while($result=mysqli_fetch_array($query))
{
//echo "<li>".$result['username']."</li>";
}
?>
<?php
//fetch all the data from database.
$rows=mysqli_num_rows(mysqli_query($conn,"select * from $table"));
//calculate total page number for the given table in the database
$total=ceil($rows/$limit);
if($page>1)
{
//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
echo 'PREVIOUS';
}
if($page!=$total)
{
////Go to previous page to show next 10 items.
echo 'NEXT';
}
?>
<?php
//show all the page link with page number. When click on these numbers go to particular page.
for($i=1;$i<=$total;$i++)
{
if($i==$page) { echo "<li class='current'>".$i."</li>"; }
else { echo '<li>'.$i.'</li>'; }
}
?>

You have error on html URL syntax
echo 'NEXT';
The correct would be
echo 'NEXT';
You have to use ? only for the first URL parameter, & for the following, if any.

Related

How can I execute multiple prepared SQL statements from a single input button press in php?

Context: The page is for menu item entry into an order. When a menu item button is pressed, data from 'theProducts' table is queried and inserted into 'theOrderItems' table. I also want the same button press to take the serving size value (theProducts.dbProdServing) and subtract that amount from the product's inventory (theInventory.dbInventoryAmt), and update that table accordingly.
The problem I'm having is trying to figure out how to have one button press execute two prepared statements with bound values at the same time, those being the statement that INSERTS product data into 'theOrderItems' table and the statement that UPDATEs 'theInventory' table.
Currently, the page runs the INSERT statement fine, but the UPDATE statement doesn't run at all.
There are three pages that handle the entire order system, 'insertOrder.php' opens the order, 'insertOrderItem.php' allows the adding of items to a specific order, and 'completeorder.php' just displays the total order. I'll only post the first two pages.
insertOrder.php
<?php
$pagetitle = 'Insert Order';
require_once 'header.php';
require_once 'connect.php';
$errormsg = "";
$showform = 1;
$sqlselectt = "SELECT * from theTables";
$resultt = $db->prepare($sqlselectt);
$resultt->execute();
$sqlselectc = "SELECT * from theCustomers";
$resultc = $db->prepare($sqlselectc);
$resultc->execute();
$sqlselects = "SELECT * from theStaff";
$results = $db->prepare($sqlselects);
$results->execute();
$sqlselectl = "SELECT * from theLocations";
$resultl = $db->prepare($sqlselectl);
$resultl->execute();
if( isset($_POST['thesubmit']) )
{
$formfield['ffOrderPickup'] = $_POST['orderPickup'];
$formfield['ffCustKey'] = $_POST['custKey'];
$formfield['ffTableKey'] = $_POST['tableKey'];
$formfield['ffStaffKey'] = $_POST['staffKey'];
$formfield['ffLocationKey'] = $_POST['locationKey'];
$formfield['ffOrderDate'] = $_POST['orderDate'];
$formfield['ffOrderTime'] = $_POST['orderTime'];
if(empty($formfield['ffCustKey'])){$errormsg .= "<p>The customer field is empty.</p>";}
if(empty($formfield['ffTableKey'])){$errormsg .= "<p>The table field is empty.</p>";}
if(empty($formfield['ffStaffKey'])){$errormsg .= "<p>The employee field is empty.</p>";}
if(empty($formfield['ffLocationKey'])){$errormsg .= "<p>The location field is empty.</p>";}
if(empty($formfield['ffOrderDate'])) {$errormsg .= "<p>The order entry date is not selected.</p>"; }
if(empty($formfield['ffOrderTime'])) {$errormsg .= "<p>The order entry time is not selected.</p>"; }
if($errormsg != "")
{
echo "<div class='error'><p>THERE ARE ERRORS!</p>";
echo $errormsg;
echo "</div>";
}
else
{
$sqlmax = "SELECT MAX(dbOrderKey) AS maxKey FROM theOrders";
$resultmax = $db->prepare($sqlmax);
$resultmax->execute();
$rowmax = $resultmax->fetch();
$maxKey = $rowmax['maxKey'];
$maxKey = $maxKey + 1;
try
{
$sqlinsert = 'INSERT INTO theOrders (dbOrderKey, dbCustKey, dbTableKey, dbStaffKey, dbLocationKey, dbOrderComplete, dbOrderDate, dbOrderTime, dbOrderMade, dbOrderPickup)
VALUES (:bvOrderKey, :bvCustKey, :bvTableKey, :bvStaffKey, :bvLocationKey, 0, :bvOrderDate, :bvOrderTime, 0, :bvOrderPickup)';
$stmtinsert = $db->prepare($sqlinsert);
$stmtinsert->bindvalue(':bvOrderKey', $maxKey);
$stmtinsert->bindvalue(':bvCustKey', $formfield['ffCustKey']);
$stmtinsert->bindvalue(':bvTableKey', $formfield['ffTableKey']);
$stmtinsert->bindvalue(':bvStaffKey', $formfield['ffStaffKey']);
$stmtinsert->bindvalue(':bvLocationKey', $formfield['ffLocationKey']);
$stmtinsert->bindvalue(':bvOrderDate', $formfield['ffOrderDate']);
$stmtinsert->bindvalue(':bvOrderTime', $formfield['ffOrderTime']);
$stmtinsert->bindvalue(':bvOrderPickup', $formfield['ffOrderPickup']);
$stmtinsert->execute();
echo "<p>Order Number: " . $maxKey . "</p>";
echo "<p>Location: " . $formfield['ffLocationKey'] . "</p>";
echo '<br><br><form action="insertOrderItem.php" method="post">';
echo '<input type="hidden" name="orderKey" value="' . $maxKey . '">';
echo '<input type="hidden" name="locationKey" value="' . $formfield['ffLocationKey'] . '">';
echo '<input type="submit" name="submit" value="Enter Order Items">';
echo '</form>';
$showform = 0;
}
catch(PDOException $e)
{
echo 'ERROR!!!' .$e->getMessage();
exit();
}
}
}
if ($visible == 1 && $showform == 1)
{
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="theform">
<fieldset><legend>Order Information</legend>
<table border>
<tr>
<th><label for="custKey">Customer:</label></th>
<td><select name="custKey" id="custKey">
<option value = "">Please Select a Customer</option>
<?php while ($rowc = $resultc->fetch() )
{
echo '<option value="'. $rowc['dbCustKey'] . '">' . $rowc['dbCustLast'] . '</option>';
}
?>
</select>
</td>
</tr>
<tr>
<th><label for="tableKey">Table:</label></th>
<td><select name="tableKey" id="tableKey">
<option value = "">Please Select a Table</option>
<?php while ($rowt = $resultt->fetch() )
{
echo '<option value="'. $rowt['dbTableKey'] . '">' . $rowt['dbTableKey'] . '</option>';
}
?>
</select>
</td>
</tr>
<tr>
<th><label for="staffKey">Employee:</label></th>
<td><select name="staffKey" id="staffKey">
<option value = "">Please Select an Employee</option>
<?php
while ($rows = $results->fetch() )
{
if($_SESSION['userid'] == $rows['dbStaffKey']) {
$selected = 'selected';
} else {
$selected = '';
}
echo '<option value="'. $rows['dbStaffKey'] . '" ' . $selected . '>' . $rows['dbStaffLast'] . '</option>';
}
?>
</select>
</td>
</tr>
<tr>
<th><label for="locationKey">Location:</label></th>
<td><select name="locationKey" id="locationKey">
<option value = "">Please Select an Location</option>
<?php
while ($rowl = $resultl->fetch() )
{
echo '<option value="'. $rowl['dbLocationKey'] . '" ' . $selected . '>' . $rowl['dbLocationCity'] . '</option>';
}
?>
</select>
</td>
</tr>
<tr>
<th>Pick a Delivery Type:</th>
<td><input type="radio" name="orderPickup" id="orderPickup"
value=1 <?php echo ' checked';?> />
<label for="pickup">Pickup</label>
<input type="radio" name="orderPickup" id="orderPickup"
value=0 />
<label for="inHouse">In-House</label>
</td>
</tr>
<tr>
<th><label for="orderDate">Entry Date:</label></th>
<td><input type="date" name="orderDate" id="orderDate" value="<?php if( isset($formfield['ffOrderDate'])){echo $formfield['ffOrderDate'];}?>" /></td>
</tr>
<tr>
<th><label for="orderTime">Entry Time:</label></th>
<td><input type="time" name="orderTime" id="orderTime" value="<?php if( isset($formfield['ffOrderTime'])){echo $formfield['ffOrderTime'];}?>" /></td>
</tr>
</table>
<input type="submit" name = "thesubmit" value="Enter">
</fieldset>
</form>
<br><br>
<?php
}
include_once 'footer.php';
?>
insertOrderItem.php
<?php
$pagetitle = 'Insert Order Items';
require_once 'header.php';
require_once 'connect.php';
$formfield['ffOrderKey'] = $_POST['orderKey'];
$formfield['ffProdKey'] = $_POST['prodKey'];
$formfield['ffProdPrice'] = $_POST['prodPrice'];
$formfield['ffProdServing'] = $_POST['prodServing'];
$formfield['ffLocationKey'] = $_POST['locationKey'];
$sqlselectc = "SELECT * FROM theCategories";
$resultc = $db->prepare($sqlselectc);
$resultc->execute();
$sqlselecti = "SELECT * FROM theInventory";
$resulti = $db->prepare($sqlselecti);
$resulti->execute();
if (isset($_POST['OIEnter'])) {
$rowi = $resulti->fetch();
$invRem = $rowi['dbInventoryAmt'] - $formfield['ffProdServing'];
$sqlinsert = 'INSERT INTO theOrderItems (dbOrderKey, dbProdKey, dbProdPrice)
VALUES (:bvOrderKey, :bvProdKey, :bvProdPrice)';
$stmtinsert = $db->prepare($sqlinsert);
$stmtinsert->bindValue(':bvOrderKey', $formfield['ffOrderKey']);
$stmtinsert->bindValue(':bvProdKey', $formfield['ffProdKey']);
$stmtinsert->bindValue(':bvProdPrice', $formfield['ffProdPrice']);
$stmtinsert->execute();
$sqlupdate = "UPDATE theInventory
SET dbInventoryAmt = :bvInventoryAmt
WHERE dbLocationKey = :bvLocationKey
AND dbProdKey = :bvProdKey";
$stmtupdate = $db->prepare($sqlupdate);
$stmtupdate->bindValue(':bvInventoryAmt', $invRem);
$stmtupdate->bindValue(':bvLocationKey', $formfield['ffLocationKey']);
$stmtupdate->bindValue(':bvProdKey', $formfield['ffProdKey']);
$stmtupdate->execute();
}
if (isset($_POST['DeleteItem'])) {
$sqldelete = "DELETE FROM theOrderItems WHERE dbOrderItemKey = :bvOrderItemKey";
$stmtdelete = $db->prepare($sqldelete);
$stmtdelete->bindValue(':bvOrderItemKey', $_POST['orderItemKey']);
$stmtdelete->execute();
}
if (isset($_POST['UpdateItem'])) {
$formfield['ffProdPrice'] = trim($_POST['newProdPrice']);
$formfield['ffOrderNotes'] = trim($_POST['newOrderNote']);
$sqlupdateoi = 'UPDATE theOrderItems
SET dbProdPrice = :bvProdPrice,
dbOrderNotes = :bvOrderNotes
WHERE dbOrderItemKey = :bvOrderItemKey';
$stmtupdateoi = $db->prepare($sqlupdateoi);
$stmtupdateoi->bindValue(':bvOrderItemKey', $_POST['orderItemKey']);
$stmtupdateoi->bindValue(':bvProdPrice', $formfield['ffProdPrice']);
$stmtupdateoi->bindValue(':bvOrderNotes', $formfield['ffOrderNotes']);
$stmtupdateoi->execute();
}
$sqlselecto = 'SELECT theOrderItems.*, theProducts.dbProdName, theProducts.dbProdServing, theCategories.*
FROM theOrderItems, theProducts, theCategories
WHERE theOrderItems.dbProdKey = theProducts.dbProdKey
AND theProducts.dbCatKey = theCategories.dbCatKey
AND theOrderItems.dbOrderKey = :bvOrderKey';
$resulto = $db->prepare($sqlselecto);
$resulto->bindValue(':bvOrderKey', $formfield['ffOrderKey']);
$resulto->execute();
if($visible == 1 && ($_SESSION['userpermit'] == 1 || $_SESSION['userpermit'] == 3 || $_SESSION['userpermit'] == 4))
{
?>
<fieldset><legend><b>Enter Items for Order Number: <?php echo $formfield['ffOrderKey']; ?></b></legend>
<table border>
<?php
$counter = 0;
echo '<tr><b>';
while ($rowc = $resultc->fetch()){
if ($counter == 2){
echo '</tr><tr>';
$counter = 0;
}
$counter++;
echo '<th valign = "middle" align = "center">' . $rowc['dbCatName'] . '<br> <table border>';
$sqlselectp = "SELECT * FROM theProducts WHERE dbCatKey = :bvCatKey";
$resultp = $db->prepare($sqlselectp);
$resultp->bindValue(':bvCatKey', $rowc['dbCatKey']);
$resultp->execute();
while ($rowp = $resultp->fetch()){
echo '<td>';
echo '<form action = "' . $_SERVER['PHP_SELF'] . '" method = "post">';
echo '<input type = "hidden" name = "orderKey" value = "' . $formfield['ffOrderKey'] . '">';
echo '<input type = "hidden" name = "prodKey" value = "' . $rowp['dbProdKey'] . '">';
echo '<input type = "hidden" name = "prodPrice" value = "' . $rowp['dbProdPrice'] . '">';
echo '<input type = "hidden" name = "prodServing" value = "'. $rowp['dbProdServing'] . '">';
echo '<input type = "submit" id="order" name = "OIEnter" value = "' . $rowp['dbProdName'] . '">';
echo '</form>';
echo '</td>';
}
echo '</table></th>';
}
echo '</tr>';
?>
</table>
</table>
</fieldset>
<br><br>
<table>
<tr>
<td>
<table border>
<tr>
<th>Item</th>
<th>Category</th>
<th>Description</th>
<th>Serving Size</th>
<th>Price</th>
<th>Notes</th>
<th></th>
<th></th>
</tr>
<?php
$ordertotal = 0;
while ($rowo = $resulto->fetch()){
$ordertotal = $ordertotal + $rowo['dbProdPrice'];
echo '<tr><td>' . $rowo['dbProdName']
. '</td><td>' . $rowo['dbCatName']
. '</td><td>' . $rowo['dbProdDesc']
. '</td><td>' . $rowo['dbProdServing']
. '</td><td>' . $rowo['dbProdPrice']
. '</td><td>' . $rowo['dbOrderNotes']
. '<td>';
echo '<form action = "' . $_SERVER['PHP_SELF'] . '" method = "post">';
echo '<input type = "hidden" name = "orderKey" value = "' . $formfield['ffOrderKey'] . '">';
echo '<input type = "hidden" name = "orderItemKey" value = "' . $rowo['dbOrderItemKey'] . '">';
echo '<input type = "submit" name = "NoteEntry" value = "Update">';
echo '</form></td><td>';
echo '<form action = "' . $_SERVER['PHP_SELF'] . '" method = "post">';
echo '<input type = "hidden" name = "orderKey" value = "' . $formfield['ffOrderKey'] . '">';
echo '<input type = "hidden" name = "orderItemKey" value = "' . $rowo['dbOrderItemKey'] . '">';
echo '<input type = "submit" name = "DeleteItem" value = "Delete">';
echo '</form></td></tr>';
}
echo '<tr><th></th><th></th><th>Total</th><th>' . $ordertotal . '</th><th></th><th></th><th></th></tr>';
?>
</table>
<?php
if(isset($_POST['NoteEntry'])){
$sqlselectoi = 'SELECT theOrderItems.*, theProducts.dbProdName
FROM theOrderItems, theProducts
WHERE theOrderItems.dbProdKey = theProducts.dbProdKey
AND theOrderItems.dbOrderItemKey = :bvOrderItemKey';
$resultoi = $db->prepare($sqlselectoi);
$resultoi->bindValue(':bvOrderItemKey', $_POST['orderItemKey']);
$resultoi->execute();
$rowoi = $resultoi->fetch();
echo '</td><td>';
echo '<form action = "' . $_SERVER['PHP_SELF'] . '" method = "post">';
echo '<table>';
echo '<tr><td>Price: <input type="text" name = "newProdPrice" value = "' .
$rowoi['dbProdPrice'] . '"></td></tr>';
echo '<tr><td>Notes: <input type="text" name = "newOrderNote" value = "' .
$rowoi['dbOrderNotes'] . '"></td></tr>';
echo '<tr><td>';
echo '<input type = "hidden" name = "orderKey" value = "' . $formfield['ffOrderKey'] . '">';
echo '<input type = "hidden" name = "orderItemKey" value = "' . $rowoi['dbOrderItemKey'] . '">';
echo '<input type = "submit" name = "UpdateItem" value = Update Item">';
echo '</td></tr></table>';
}
?>
</td></tr>
</table>
<br><br>
<?php
echo '<form action = "completeorder.php" method = "post">';
echo '<input type = "hidden" name = "orderKey" value = "' . $formfield['ffOrderKey'] . '">';
echo '<input type = "submit" name = "CompleteOrder" value = "Complete Order">';
echo '</form>';
}
include_once 'footer.php';
?>

how to update an array from input box in php mysql

This is my code:
$uid=$_POST['uid'];
$fields = '';
if($uid==1){
//having some error in this line
foreach($query as $key => $value) {
if ($i++ != 0) $fields .= ', ';
$key = mysql_real_escape_string($key);
$value = mysql_real_escape_string($value);
$fields .= "$key = $value";
}
$query = mysql_query("UPDATE 2mcom SET $fields");
}
echo "<table>" ;
$result = mysql_query("select * from 2mcom where roll_no='138218600004'");
$i=1;
while (false != ($data = mysql_fetch_array($result, MYSQL_ASSOC)))
foreach ($data as $key => $value)
//echo "$key: $value <br />";
{
echo "<tr><td>";
echo "$key: ";
echo "</td>";
echo '<form method="post" action="">';
echo "<td>";
echo '<input type="text" name="fld'.$i.'" value="'.$value.'"/>';
echo '<input type="hidden" name="uid" value="1"/>';
$i++;
echo "</td></tr>";
}
echo "<tr><td>";
echo'<input type="submit" name="submit" value="submit"/>';
echo "</tr></td>";
echo'</form></table>';
I want to update this table 2mcom from the input text-box. Here i am fetching the field-name along with their values in input field from table "2mcom" using array.. now i want to update the field after editing.it is for result correction .
This should give your text field a name, then you can do separate UPDATE queries to update each field individually.
UPDATE, consolidated version of previous code:
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
foreach($row as $key => $value) {
$name = 'field';
$id = $row['id']; //rename this with your row id, this will give you identifiable input names
echo '<tr><td>';
echo $key . ': ';
echo '</td>';
echo '<form method="POST">';
echo '<td>';
echo '<input type="hidden" name="id[]" value="'. $row['id'] .'"/>';
echo '<input type="hidden" name="column[]" value="'. $key .'"/>';
echo '<input type="text" name="field[]" value="'. $value .'"/>';
echo '</td></tr>';
}
$i++;
}
UPDATE QUERY:
function update_table($table, $col, $value, $id) {
global $link; //mysqli object
//escape values
$value = mysqli_real_escape_string($link, $value);
$col = mysqli_real_escape_string($link, $col);
//update the database field
$query = "UPDATE `$table` SET `$col` = '$value' WHERE `roll_id` = '$id'";
mysqli_query($link, $query) or die(mysqli_error($link));
return true;
}
CONTROLLER:
if(isset($_POST['submit'])) {
$col_count = count($_POST['column']);
$field_count = count($_POST['field']);
if($field_count == $col_count) {
} else {
trigger_error("Col - Field mismatch", E_USER_ERROR);
}
for($i=0; $i<count($_POST['column']); $i++) {
if(!update_table($table, $_POST['column'][$i], $_POST['field'][$i], $_POST['id'][$i])) {
trigger_error("Error updating field", E_USER_ERROR);
}
}
}
Try that.

php file takes long time loading then returns blank page mysqli

I'm trying to display the elements of an event in a form for the user to edit. I want the current values to be entered or selected, but I want it in a way so that I don't have to validate all sorts of info. I have a calendar picker and want to use dropdowns for the times. This code hangs for a few minutes and then returns a completely blank page. Am I just overloading the server with all the queries or is there something wrong with my code?
$id = $_POST['id'];
/* Edit Event Form */
echo '<form action="edit-event-process.php" method="post">';
echo '<center><table style="text-align:center">';
/* Set up queries individually to allow for dropdowns for hour, minute, am/pm, category */
/************* Query for Name *******************/
$queryName = mysqli_query($link, "SELECT Name FROM events WHERE id = " . $id);
while($rowName = mysqli_fetch_assoc($queryName)){
foreach($rowName as $keyName => $valName){
echo '<tr><td>Name: </td><td><input type="text" id="Name" name="Name" value="' . $valName . '"></td></tr>';
}
}
/************* Query for Description **************/
$queryDesc = mysqli_query($link, "SELECT Description FROM events WHERE id = " . $id);
while($rowDesc = mysqli_fetch_assoc($queryDesc)){
foreach($rowDesc as $keyDesc => $valDesc){
echo '<tr><td>Description: </td><td><input type="text" id="Description" name="Description" value="' . $valDesc . '"></td></tr>';
}
}
/************* Query for Start Date ***************/
$queryStDt = mysqli_query($link, "SELECT StartDate FROM events WHERE id = " . $id);
while($rowStDt = mysqli_fetch_assoc($queryStDt)){
foreach($rowStDt as $keyStDt => $valStDt){
echo '<tr><td>Start Date: </td><td><input type="text" id="StartDate" name="StartDate" value="' . $valStDt . '"></td></tr>';
}
}
/************* Query for All Day **************/
$queryAllDay = mysqli_query($link, "SELECT AllDay FROM events WHERE id = " . $id);
while($rowAllDay = mysqli_fetch_assoc($queryAllDay)){
foreach($rowAllDay as $keyAllDay => $valAllDay){
if ($valAllDay == '1'){
echo '<tr><td>All Day? </td><td><input type="checkbox" id="AllDay" name="AllDay" checked="checked"></td></tr>';
}
else {
echo '<tr><td>All Day? </td><td><input type="checkbox" id="AllDay" name="AllDay"></td></tr>';
}
}
}
/************/
echo '<div id="dates">';
/************/
/************* Query for Start Hour ****************/
echo '<tr><td>Start Hour</td><td><select name="StartHour" id="StartHour">';
$queryStHr = mysqli_query($link, "SELECT TIME_FORMAT(StartTime, '%h') AS StartHour WHERE id = " . $id);
while ($rowStHr = mysqli_fetch_assoc($resultStHr)){
foreach($rowStHr as $keyStHr => $valStHr){
$selectedStHr = $valStHr;
}
}
if ($valStHr == "" || $valStHr == "null"){
echo '<option value="null">--</option>';
}
else {
echo '<option value="'.$valStHr.'">' . $valStHr . '</option>';
echo '<option value="null">--</option>';
}
$sthr = 0;
while($sthr < 13){
echo '<option value="'.$sthr.'">' . $sthr . '</option>';
$sthr = $sthr++;
}
echo '</select></td></tr>';
/************* Query for Start Min *****************/
echo '<tr><td>Start Minute</td><td><select name="StartMin" id="StartMin">';
$queryStMin = mysqli_query($link, "SELECT TIME_FORMAT(StartTime, '%m') AS StartMin WHERE id = " . $id);
while ($rowStMin = mysqli_fetch_assoc($resultStMin)){
foreach($rowStMin as $keyStMin => $valStMin){
$selectedStMin = $valStMin;
}
}
if ($valStMin == "" || $valStMin == "null"){
echo '<option value="null">--</option>';
}
else{
if ($valStMin < 10){
echo '<option value="0'.$valStMin.'">0' . $valStMin . '</option>';
echo '<option value="null">--</option>';
}
else {
echo '<option value="'.$valStMin.'">' . $valStMin . '</option>';
echo '<option value="null">--</option>';
}
}
$stmin = 0;
while($stmin < 60){
if ($stmin < 10){
echo '<option value="0'.$stmin.'">0' . $stmin . '</option>';
}
else {
echo '<option value="'.$stmin.'">' . $stmin . '</option>';
}
$stmin = $stmin +5;
}
echo '</select></td></tr>';
/************* Query for Start AMPM ****************/
echo '<tr><td>Start AM/PM</td><td><select name="StAP" id="StAP">';
$queryStAP = mysqli_query($link, "SELECT TIME_FORMAT(StartTime, '%p') AS StAP WHERE id = " . $id);
while ($rowStAP = mysqli_fetch_assoc($resultStAP)){
foreach($rowStAP as $keyStAP => $valStAP){
$selected = $valStAP;
}
}
if ($valStAP != ""){
echo '<option selected name="StAP" value="' . $valStAP . '">' . $valStAP . '</option>';
}
echo '<option value="--">--</option>';
echo '<option value="am">am</option>';
echo '<option value="pm">pm</option>';
echo '</select></td></tr>';
/************* Query for End Date *****************/
$queryEndDt = mysqli_query($link, "SELECT EndDate FROM events WHERE id = " . $id);
while($rowEndDt = mysqli_fetch_assoc($queryEndDt)){
foreach($rowEndDt as $keyDesc => $valEndDt){
echo '<tr><td>Start Date: </td><td><input type="text" id="StartDate" name="StartDate" value="' . $valEndDt . '"></td></tr>';
}
}
/************* Query for End Hour *****************/
echo '<tr><td>End Hour</td><td><select name="EndHour" id="EndHour">';
$queryEndHr = mysqli_query($link, "SELECT TIME_FORMAT(EndTime, '%h') AS EndHour WHERE id = " . $id);
while ($rowEndHr = mysqli_fetch_assoc($resultEndHr)){
foreach($rowEndHr as $keyEndHr => $valEndHr){
$selectedEndHr = $valSEndHr;
}
}
if ($valEndHr == "" || $valEndHr == "null"){
echo '<option value="null">--</option>';
}
else {
echo '<option value="'.$valEndHr.'">' . $valEndHr . '</option>';
echo '<option value="null">--</option>';
}
$endmin = 0;
while($endmin < 13){
echo '<option value="'.$endmin.'">' . $endmin . '</option>';
$endmin = $endmin++;
}
echo '</select></td></tr>';
/************* Query for End Min ******************/
echo '<tr><td>Start Minute</td><td><select name="EndMin" id="EndMin">';
$queryEndMin = mysqli_query($link, "SELECT TIME_FORMAT(EndTime, '%m') AS EndMin WHERE id = " . $id);
while ($rowEndMin = mysqli_fetch_assoc($resultEndMin)){
foreach($rowEndMin as $keyEndMin => $valEndMin){
$selectedEndMin = $valEndMin;
}
}
if ($valEndMin == "" || $valEndMin == "null"){
echo '<option value="null">--</option>';
}
else{
if ($valEndMin < 10){
echo '<option value="0'.$valEndMin.'">0' . $valEndMin . '</option>';
echo '<option value="null">--</option>';
}
else {
echo '<option value="'.$valEndMin.'">' . $valEndMin . '</option>';
echo '<option value="null">--</option>';
}
}
$endmin = 0;
while($endmin < 60){
if ($endmin < 10){
echo '<option value="0'.$endmin.'">0' . $endmin . '</option>';
}
else {
echo '<option value="'.$endmin.'">' . $endmin . '</option>';
}
$endmin = $endmin +5;
}
echo '</select></td></tr>';
/************* Query for End AMPM *****************/
echo '<tr><td>End AM/PM</td><td><select name="EndAP" id="EndAP">';
$queryStAP = mysqli_query($link, "SELECT TIME_FORMAT(StartTime, '%p') AS EndAP WHERE id = " . $id);
while ($rowEndAP = mysqli_fetch_assoc($resultEndAP)){
foreach($rowEndAP as $keyEndAP => $valEndAP){
$selected = $valEndAP;
}
}
if ($valEndAP != ""){
echo '<option selected name="StAP" value="' . $valEndAP . '">' . $valEndAP . '</option>';
}
echo '<option name="EndAP" value="--">--</option>';
echo '<option name="EndAP" value="am">am</option>';
echo '<option name="EndAP" value="pm">pm</option>';
echo '</select></td></tr>';
/*************/
echo '</div>';
/************/
/************* Query for Place ********************/
$queryPlace = mysqli_query($link, "SELECT Place FROM events WHERE id = " . $id);
while($rowPlace = mysqli_fetch_assoc($queryPlace)){
foreach($rowPlace as $keyPlace => $valPlace){
echo '<tr><td>Place: </td><td><input type="text" id="Place" name="Place" value="' . $valPlace . '"></td></tr>';
}
}
/************** Query for Category *****************/
echo '<tr><td>Category</td><td><select name="category" id="category">';
$query2 = "SELECT Category FROM events WHERE id = " . $id;
$result2 = mysqli_query($link, $query2);
while ($row2 = mysqli_fetch_assoc($result2)){
foreach($row2 as $key2 => $val2){
$selected = $val2;
}
}
echo '<option name="none" value="">none</option>';
$queryCategory = "SELECT name FROM categories";
$result = mysqli_query($link, $queryCategory);
while($row1 = mysqli_fetch_assoc($result)){
foreach($row1 as $key1 => $val1){
if ($val1 != ""){
if ($val1 == $val2){
echo '<option selected name="' . $key . '" value="' . $val1 . '">' . $val1 . '</option>';
}
else {
echo '<option name="' . $key . '" value="' . $val1 . '">' . $val1 . '</option>';
}
}
}
}
echo '</select></td></tr>';
echo '<input type="hidden" name="id" value="' . $id . '" />';
echo '<tr><td><input type="submit" value="Save Changes" /></td></tr>';
echo "</table>";
echo "</form>";
This isn't a complete answer, but I'd recommend you have a look at this and revise your code. You're running unnecessary queries and there is a security error too.
//This needs to be casted here (assuming it is an int)
$id = (int) $_POST['id'];
/* Edit Event Form */
echo '<form action="edit-event-process.php" method="post">';
echo '<center><table style="text-align:center">';
/* Don't do queries individually */
$queryEvent = mysqli_query($link, "SELECT Name,Description,StartDate,AllDay FROM events WHERE id = " . $id);
if($row = mysqli_fetch_assoc($queryEvent)){
?>
<tr>
<td>Name: </td>
<td><input type="text" id="Name" name="Name" value="<?php echo $row['Name']; ?>"></td>
</tr>
<tr>
<td>Description: </td>
<td><input type="text" id="Description" name="Description" value="<?php echo $row['Description']; ?>"></td>
</tr>
<tr>
<td>Start Date: </td>
<td><input type="text" id="StartDate" name="StartDate" value="<?php echo $row['StartDate']; ?>"></td>
</tr>
<tr>
<td>All Day? </td>
<td><input type="checkbox" id="AllDay" name="AllDay"<?php if($row['AllDay'] == '1') echo 'checked="checked"'; ?>></td></tr>';
</tr>
<?php
}

Invalid argument supplied for foreach() - Yet output is showing

I am working to show editable fields based on query results. I know the query is functioning properly, and it is returning an array. The array is populating the form fields properly, however, I am getting the "Invalid argument supplied for foreach()" warning. I am new at this, and at a loss as to what is happening. I appreciate any suggestions.
Here is the code:
// Grab the profile data from the database
$query8 = "SELECT * FROM EDUCATION WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' ORDER BY RECORD";
$data = mysqli_query($dbc, $query8);
echo '<pre>' . print_r($data, true) . '</pre>';
$rowcount = 1;
while ($row = mysqli_fetch_assoc($data))
{
if (is_array($row))
{
echo '<p> It is an Array</p>';
}
foreach($row as &$item)
{
$record = $row['RECORD'];
$school = $row['SCHOOL'];
$type = $row['TYPE'];
$degree = $row['DEGREE'];
$major = $row['MAJOR'];
$grad = $row['GRAD'];
?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Education History </legend>
<?php
echo '<input type="hidden" id="record" name="record" value="' . $record . '">';
echo 'Rowcount' . $rowcount. '</br>';
// Insert Listbox here
$queryschool = "SELECT * FROM SCHOOL";
$list = mysqli_query($dbc, $queryschool);
if($list)
{
echo 'School Type? ';
echo '<select name="school_code">';
while($row = mysqli_fetch_assoc($list))
{
echo "<option value={$row['CODE']}>{$row['TYPE']}" ;
echo '</option>';
}
echo '</select>';
}
echo '<br />';
echo '<label for="school">School Name:</label>';
echo '<input type="text" id="school" name="school" size="40" maxlength="40" value="' . ( (!empty($school)) ? $school : "") . '" /><br />';
// Insert Listbox here
$querydegree = "SELECT * FROM DEGREE";
$list = mysqli_query($dbc, $querydegree);
if($list)
{
echo 'Degree Type? ';
echo '<select name="degree_code">';
while($row = mysqli_fetch_assoc($list))
{
echo "<option value={$row['CODE']}>{$row['DEGREE']}";
echo '</option>';
}
echo '</select>';
}
echo '<br />';
echo '<label for="major">Field of study:</label>';
echo '<input type="text" id="major" name="major" size="40" maxlength="40" value="' . ( (!empty($major)) ? $major : "") . '" /><br />';
echo '<label for="grad">Did you graduate?:</label>';
echo '<input type="radio" id="grad" name="grad" value="Y" ' . ($grad == "Y" ? 'checked="checked"':'') . '/>Yes ';
echo '<input type="radio" id="grad" name="grad" value="N" ' . ($grad == "N" ? 'checked="checked"':'') . '/>No<br />';
?>
</fieldset>
<?php
$rowcount++;
}
}
;
echo '<label for="another">Do you need to enter more educational experience?:</label>';
echo '<input type="radio" id="another" name="another" value="Y" ' . ($another == "Y" ? 'checked="checked"':'') . '/>Yes ';
echo '<input type="radio" id="another" name="another" value="N" ' . ($another == "N" ? 'checked="checked"':'') . '/>No<br />';
?>
<input type="submit" value="Save Profile" name="submit" />
</form>
foreach ($row as &$item)
replace this with:
foreach ($row as $item)
And then for each variable you should probably change
$record = $row['RECORD'];
to
$record = $item['RECORD'];
foreach($row as &$item) should be
foreach($row as $item)
there is no need to use the foreach here you can just do this like like
while ($row = mysqli_fetch_assoc($data))
{
$record = $row['RECORD'];
$school = $row['SCHOOL'];
$type = $row['TYPE'];
$degree = $row['DEGREE'];
$major = $row['MAJOR'];
$grad = $row['GRAD'];
}
You're not changing the row item, so don't pass by reference to the foreach. Also, shouldn't you be using $item instead of $row? Do this:
foreach($row as $item)
{
$record = $item['RECORD'];
$school = $item['SCHOOL'];
....
Don't do This:
foreach($row as &$item)
{
$record = $row['RECORD'];
$school = $row['SCHOOL'];
....

WIll foreach loop accomplish this? How?

I am trying to create a form page that will allow the end-user the ability to update multiple entries in a table. The user is tagged by an ID_NUM and the entries by RECORD. I want to display each row in the form, with each row stacked on the page in separate instances. As below:
School Name:
School Type:
Degree:
Major:
Graduate:
School Name:
School Type:
Degree:
Major:
Graduate:
I want the submit to trigger an update to any changes in any row. Here is the code I have for the basic form. What do I need to do to integrate the foreach loop, if that is the best way to solve the problem?
<?php
// Start the session
require_once('startsession.php');
// Insert Page Header
$page_title = 'Edit Profile';
require_once('header.php');
// Make sure the user is logged in before going any further.
if (!isset($_SESSION['email'])) {
echo '<p class="login">Please log in to access this page.</p>';
exit();
}
// Insert navmenu
require_once('navmenu.php');
require_once('vary.php');
require_once('appvars.php');
require_once('connectvars.php');
// Connect to the database using vary.php
if (isset($_POST['submit']))
{
// Grab the profile data from the POST
$record2 = $_POST['record'];
$school2 = $_POST['school'];
$type2 = $_POST['school_code'];
$degree2 = $_POST['degree_code'];
$desc2 = $_POST['desc'];
$grad2 = $_POST['grad'];
$another2 = $_POST['another'];
// Update the profile data in the database
if (!empty($school2)) {
$query3 = "UPDATE EDUCATION SET SCHOOL = '$school2' WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 . "'";
mysqli_query($dbc, $query3);
}
if (!empty($type2)) {
$query4 = "UPDATE EDUCATION SET TYPE = '$type2' WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 . "'";
mysqli_query($dbc, $query4);
}
if (!empty($degree2)) {
$query5 = "UPDATE EDUCATION SET DEGREE = '$degree2' WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 . "'";
mysqli_query($dbc, $query5);
}
if (!empty($desc2)) {
$query6 = "UPDATE EDUCATION SET MAJOR = '$desc2' WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 . "'";
mysqli_query($dbc, $query6);
}
if (!empty($grad2)) {
$query7 = "UPDATE EDUCATION SET GRAD = '$grad2' WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 ."'";
mysqli_query($dbc, $query7);
}
// Confirm success with the user
if ($another2=="Y")
{
// Clear the variables and reload the page for new submit
$record2 = "";
$school2 = "";
$type2 = "";
$degree2 = "";
$major2 = "";
$grad2 = "";
$another2 = "";
echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.avant.jobs/portal/addeducation.php">';
}
else
{
echo '<p>The education section of your profile has been successfully updated. Would you like to continue??</p>';
echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.avant.jobs/portal/workcheck.php">';
}
mysqli_close($dbc);
exit();
}
else
{
echo '<p class="error">You must enter all of the profile data.</p>';
}
// End of check for form submission
// Grab the profile data from the database
$query8 = "SELECT * FROM EDUCATION WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "'";
$data = mysqli_query($dbc, $query8);
$row = mysqli_fetch_array($data);
if ($row != NULL)
{
$record = $row['RECORD'];
$school = $row['SCHOOL'];
$type = $row['TYPE'];
$degree = $row['DEGREE'];
$desc = $row['MAJOR'];
$grad = $row['GRAD'];
}
else
{
echo '<p class="error">There was a problem accessing your profile.</p>';
}
;
?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Education History </legend>
<?php
echo '<input type="hidden" id="record" name="record" value="' . $record . '">';
// Insert Listbox here
$queryschool = "SELECT * FROM SCHOOL";
$list = mysqli_query($dbc, $queryschool);
if($list)
{
echo 'School Type? ';
echo '<select name="school_code">';
while($row = mysqli_fetch_assoc($list))
{
echo "<option value={$row['CODE']}>{$row['TYPE']}" ;
echo '</option>';
}
echo '</select>';
}
echo '<br />';
echo '<label for="school">School Name:</label>';
echo '<input type="text" id="school" name="school" size="40" maxlength="40" value="' . ((!empty($school)) ? $school : "") . '" /><br />';
// Insert Listbox here
$querydegree = "SELECT * FROM DEGREE";
$list = mysqli_query($dbc, $querydegree);
if($list)
{
echo 'Degree Type? ';
echo '<select name="degree_code">';
while($row = mysqli_fetch_assoc($list))
{
echo "<option value={$row['CODE']}>{$row['DEGREE']}";
echo '</option>';
}
echo '</select>';
}
echo '<br />';
echo '<label for="desc">Field of study:</label>';
echo '<input type="text" id="desc" name="desc" size="40" maxlength="40" value="' . ( (!empty($desc)) ? $desc : "") . '" /><br />';
echo '<label for="grad">Did you graduate?:</label>';
echo '<input type="radio" id="grad" name="grad" value="Y" ' . ($grad == "Y" ? 'checked="checked"':'') . '/>Yes ';
echo '<input type="radio" id="grad" name="grad" value="N" ' . ($grad == "N" ? 'checked="checked"':'') . '/>No<br />';
?>
</fieldset>
<?php
echo '<label for="another">Do you need to enter more educational experience?:</label>';
echo '<input type="radio" id="another" name="another" value="Y" ' . ($another == "Y" ? 'checked="checked"':'') . '/>Yes ';
echo '<input type="radio" id="another" name="another" value="N" ' . ($another == "N" ? 'checked="checked"':'') . '/>No<br />';
?>
<input type="submit" value="Save Profile" name="submit" />
</form>
<?php
// Insert Page Footer
require_once('footer.php');
?>
As I am new to this and trying to teach my self, any help is appreciated! Thank you.
Instead of having multiple UPDATE queries, you can integrate them to 1 query,
$comma = FALSE;
$query = "UPDATE EDUCATION SET ";
// Update the profile data in the database
if (!empty($school2)) {
$query .= "SCHOOL = '$school2'";
$comma = TRUE;
}
if (!empty($type2)) {
if($comma === TRUE)
$query .= ", ";
$query .= "TYPE = '$type2' ";
$comma = TRUE;
}
if (!empty($degree2)) {
if($comma === TRUE)
$query .= ", ";
$query5 = "DEGREE = '$degree2'";
$comma = TRUE;
}
if (!empty($desc2)) {
if($comma === TRUE)
$query .= ", ";
$query .= "MAJOR = '$desc2'";
$comma = TRUE;
}
if (!empty($grad2)) {
if($comma === TRUE)
$query .= ", ";
$query .= "GRAD = '$grad2'";
}
$query .= "WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 ."'";
if (!empty($school2) || !empty($type2) || !empty($degree2) || !empty($desc2) || !empty($grad2)) {
mysqli_query($dbc, $query);

Categories