How to update multiple table row array in php mysql - php

it display the record but when I save it. it doesnt work. please have a check on my array query.
<form action="saveupdaterecord.php" class="form-horizontal" role="form" method="post">
<table>
<tr>
<?php
$result = $db->prepare("SELECT * FROM famcomp WHERE app_id='". mysql_real_escape_string($app_id) ."'");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<td><input type="hidden" name="app_id[]" value="<?php echo $row['app_id']; ?>" /></td>
<td><input type="text" name="fullname[]" value="<?php echo $row['fullname']; ?>" class="input" /></td>
<td><input type="text" name="fage[]" value="<?php echo $row['fage']; ?>" class="input" /></td>
<td><input type="text" name="frel[]" value="<?php echo $row['frel']; ?>" class="input" /></td>
<td><input type="text" name="fcivil[]" value="<?php echo $row['fcivil']; ?>" class="input" /></td>
<td><input type="text" name="fedu[]" value="<?php echo $row['fedu']; ?>" class="input" /></td>
<td><input type="text" name="foccup[]" value="<?php echo $row['foccup']; ?>" class="input" /></td>
<td><input type="text" name="finco[]" value="<?php echo $row['finco']; ?>" class="input" /></td>
</tr>
<?php
}
?>
<br></table></form>
saveupdaterecord.php
$fullname=$_POST['fullname'];
$N = count($fullname);
for($i=0; $i < $N; $i++)
mysql_query("UPDATE 'famcomp' SET 'fullname' = '".$_POST["fullname[$i]"]."', fage = '".$_POST["fage[$i]"]."', frel = '".$_POST["frel[$i]"]."', fcivil = '".$_POST["fcivil[$i]"]."', fedu = '".$_POST["fedu[$i]"]."', foccup = '".$_POST["foccup[$i]"]."', finco = '".$_POST["finco[$i]"]."' WHERE `app_id` = '".$_POST["app_id"]."'"); // Run the Mysql update query inside for loop
$message = 'Success Updating the record!!';
echo "<SCRIPT>alert('$message');</SCRIPT>";
echo "<script>windows: location='editrecord.php?name=$a'</script>";

Remove quotes to table name and Use foreach for looping the data.
foreach($fullname as $key){ mysql_query("UPDATE famcomp SET 'fullname' = '".$_POST[fullname[$key]]."', fage = '".$_POST["fage[$key]"]."', frel = '".$_POST["frel[$key]"]."', fcivil = '".$_POST["fcivil[$key]"]."', fedu = '".$_POST["fedu[$key]"]."', foccup = '".$_POST["foccup[$key]"]."', finco = '".$_POST["finco[$key]"]."' WHEREapp_id= '".$_POST["app_id"]."'"); }

Dont use 'famcomp' use famcomp
Also as raveenanigam said you havent used $_POST["app_id[$i]"]. If still gets the problem, try using
mysql_query("UPDATE famcomp SET
fullname = '".$_POST["fullname[$i]"]."', fage = '".$_POST["fage[$i]"]."',
frel = '".$_POST["frel[$i]"]."', fcivil = '".$_POST["fcivil[$i]"]."',
fedu = '".$_POST["fedu[$i]"]."', foccup = '".$_POST["foccup[$i]"]."',
finco = '".$_POST["finco[$i]"]."'
WHERE `app_id` = '".$_POST["app_id[$i]"]."'")
or die(mysql_error()); // Run the Mysql update query inside for loop
Edit : Try this
if(isset($_POST['fullname']))
{
$fullname = $_POST['fullname'];
echo "Got the Fullname";
foreach( $fullname as $key => $n ) {
mysql_query("UPDATE famcomp SET 'fullname' = '".$_POST[fullname[$key]]."', fage = '".$_POST["fage[$key]"]."', frel = '".$_POST["frel[$key]"]."', fcivil = '".$_POST["fcivil[$key]"]."', fedu = '".$_POST["fedu[$key]"]."', foccup = '".$_POST["foccup[$key]"]."', finco = '".$_POST["finco[$key]"]."' WHERE app_id= '".$_POST["app_id[$key]"]."'") or die(mysql_error());
$message = 'Success Updating the record for ' . $n;
}
}
else
echo "Cannot get Fullname";

Try this
$fullname=$_POST['fullname'];
$N = count($fullname);
for($i = 0 ;$i< count($fullname); $i++) {
$query = "UPDATE famcomp SET app_id = '".$_POST["app_id[$i]"]."', fullname = '".$_POST["fullname[$i]"]."', fage = '".$_POST["fage[$i]"]."', frel = '".$_POST["frel[$i]"]."', fcivil = '".$_POST["fcivil[$i]"]."', fedu = '".$_POST["fedu[$i]"]."', foccup = '".$_POST["foccup[$i]"]."', finco = '".$_POST["finco[$i]"]."' WHERE app_id = '".$_POST["app_id[$i]"]."'"; // Run the Mysql update query inside for loop
mysql_query($query, $con);
$message = 'Success Updating the record!!';
echo "<SCRIPT>alert('$message');</SCRIPT>";
echo "<script>windows: location='editrecord.php?name=$a'</script>";
}
Add this to your php
<?php
$result = $db->prepare("SELECT * FROM famcomp WHERE app_id='". mysql_real_escape_string($app_id) ."'");
$result->execute();
for($i=0; $row = $result->fetch(); $i++)
$con = mysql_connect("host","user","password","database"); //you can check this in priviledges. name of your database, if user has no password do not include password, delete "password",
if (!$con){
die("Can't connect".mysql_error());
}
mysql_select_db("database",$con); //the name of your database
?>

Related

Update multiple record with single query in database using php [duplicate]

This question already has an answer here:
Post form and update multiple rows with mysql
(1 answer)
Closed last year.
I am making an invoice in PHP where multiple products are inserted at once into one table and there grand total goes to another table. I am trying to UPDATE the invoice in MYSQL and PHP. When I press the submit button, the multiple records data from the form goes to the update.php but the query does not run.
database.php
<?php
$connect = mysqli_connect('localhost','root','','invoice');
if (!$connect){
die("Connection failed: " . mysqli_connect_error());
}
?>
edit_invoice.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
table,tr,td,th { border: 1px black solid;}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
Back to Invoice List<br><br>
<?php
include('database.php');
$invoice_number = $_GET['invoice_number'];
$sql = "SELECT * from invoice where invoice_number = '$invoice_number' ";
$query = mysqli_query($connect, $sql);
if ($query->num_rows > 0) {
// output data of each row
$fetch = $query->fetch_assoc();
}
?>
<form method="POST" action="update_invoice.php">
<table>
<thead>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Width</th>
<th>Height</th>
<th>Total</th>
<th>Action</th>
</thead>
<?php
$sql2 = "SELECT * from invoice_order where invoice_number = '$invoice_number' ";
$query2 = mysqli_query($connect, $sql2);
if ($query2->num_rows > 0) {
// output data of each row
$srno = 1;
$count = $query2->num_rows;
for ($i=0; $i < $count; $i++) {
while($row = $query2->fetch_assoc()) {
?>
<tbody id="product_table">
<tr>
<td><input type="text" name="product[]" value="<?php echo $row["product"]; ?>"></td>
<td><input type="text" name="price[]" value="<?php echo $row["price"]; ?>"></td>
<td><input type="text" name="quantity[]" value="<?php echo $row["quantity"]; ?>"></td>
<td><input type="text" name="width[]" value="<?php echo $row["width"]; ?>"></td>
<td><input type="text" name="height[]" value="<?php echo $row["height"]; ?>"></td>
<td><input type="text" name="total[]" value="<?php echo $row["total"]; ?>" class="totalPrice" readonly></td>
<td><input type="button" value="X" onclick="deleteRow(this)"/></td>
</tr>
</tbody>
<?php
}
}
} else {
echo "No Record Found";
}
?>
<input type="button" name="submit" value="Add Row" onclick="add_fields();">
<span>Invoice Date:<input type="date" value="<?php echo $fetch["invoice_date"]; ?>" name="invoice_date"></span>
<span>Invoice #:<input type="text" name="invoice_number" value="<?php echo $fetch["invoice_number"]; ?>" readonly></span>
<span>Select Customer:
<select name="to_user" class="form-control">
<option><?php echo $fetch["customer_id"]; ?></option>
<?php
include('database.php');
$sql = mysqli_query($connect, "SELECT * From customer");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['customer_id'] ."'>" .$row['customer_id'] ." - " .$row['customer_name'] ."</option>" ;
}
?>
</select>
</span>
</table>
<span>Grand Total<input type="text" name="grandtotal" id="grandtotal" value="<?php echo $fetch["grandtotal"]; ?>" readonly></span><br><br>
<span>Paid Amount<input type="text" name="paid" id="paid" value="<?php echo $fetch["paid"]; ?>"></span><br><br>
<span>Balance<input type="text" name="balance" id="balance" value="<?php echo $fetch["balance"]; ?>" readonly></span><br><br>
<input type="submit" name="send" value="Submit">
</form>
</body>
<script>
const table = document.getElementById('product_table');
table.addEventListener('input', ({ target }) => {
const tr = target.closest('tr');
const [product, price, quantity, width, height, total] = tr.querySelectorAll('input');
var size = width.value * height.value;
var rate = price.value * quantity.value;
if (size != "") {
total.value = size * rate;
}else{
total.value = rate;
}
totalPrice();
});
function add_fields() {
var row = document.createElement("tr");
row.innerHTML =
'<td><input type="text" name="product[]"></td>' +
'<td><input type="text" name="price[]"></td>' +
'<td><input type="text" name="quantity[]"></td>' +
'<td><input type="text" name="width[]" value="0"></td>' +
'<td><input type="text" name="height[]" value="0"></td>' +
'<td><input type="text" name="total[]" class="totalPrice" readonly></td>' +
'<td><input type="button" value="X" onclick="deleteRow(this)"/></td>';
table.appendChild(row);
}
function deleteRow(btn) {
var row = btn.parentNode.parentNode;
row.parentNode.removeChild(row);
totalPrice();
}
function totalPrice() {
var grandtotal = 0;
var paid = 0;
$(".totalPrice").each(function() {
grandtotal += parseFloat($(this).val());
paid = grandtotal;
});
$("#grandtotal").val(grandtotal);
$("#paid").val(paid);
}
$(document).ready(function() {
$('#paid').on('input', function() {
grandtotal = $("#grandtotal").val();
paid = $("#paid").val();
balance = parseFloat(grandtotal) - parseFloat(paid);
$("#balance").val(balance);
})
});
</script>
</html>
Update_invoice.php
<?php
include('database.php');
if (isset($_POST['send'])) {
$product = $_POST['product'];
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$width = $_POST['width'];
$height = $_POST['height'];
$total = $_POST['total'];
$customer_id = $_POST['to_user'];
$invoice_date = $_POST['invoice_date'];
$invoice_number = $_POST['invoice_number'];
$grandtotal = $_POST['grandtotal'];
$paid = $_POST['paid'];
$balance = $_POST['balance'];
$amount_status = "";
if ($grandtotal == $paid) {
$amount_status = "Paid";
} elseif ($grandtotal == $balance) {
$amount_status = "Due";
} else {
$amount_status = "Partial";
}
// Start of Updating data to invoice_order table
for ($i = 0; $i < count($_POST['total']); $i++) {
if ($i <> count($_POST['total'])) {
$sql = "UPDATE invoice_order SET invoice_number = '$invoice_number' , product = '$_POST['product'][$i]', price = '$_POST['price'][$i]' , quantity = '$_POST['quantity'][$i]', width = '$_POST['width'][$i]' , height = '$_POST['height'][$i]' , total = '$_POST['total'][$i]' WHERE invoice_number='$invoice_number' ";
$query = mysqli_query($connect, $sql);
if ($query) {
header('location: list_invoice.php');
} else {
echo "Unable to enter records in invoice_order table";
}
}
}
// End of updating data to invoice_order table
// Start of updating data to invoice table
$sql2 = "UPDATE invoice SET customer_id = '$customer_id', grandtotal = '$grandtotal', invoice_number = '$invoice_number', invoice_date = '$invoice_date', paid = '$paid', balance = '$balance', amount_status = '$amount_status' WHERE invoice_number='$invoice_number' ";
$query2 = mysqli_query($connect, $sql2);
if ($query2) {
header('location: list_invoice.php');
} else {
echo "Unable to enter record in invoice table";
}
// End of updating data to invoice table
}
?>
The approach you are using is not the preferred way of doing the job. Use mysql prepare statement. This will make the code clean, easy to read and secured. Here is the link you can refer Mysql Prepared Statement in PHP

how to use php retrieve data from postgresql and showing in html

I was trying to retrieve data from PostgreSQL database, In PHP.
Retrieving the data is fine but it does not work when it is linked with HTML.
When I click on find button in HTML interface, it did not really retrieve data from PostgrSQL.
Can anybody help?
PHP
$host = "host=sv4gis";
$dbname = "dbname=survey";
$credentials = "user=sde password=dfd54f";
$objectid = "";
$jobno = "";
$unit = "";
$coordinate = "";
$name = "";
$northing = "";
$easting = "";
$elev = "";
$db = pg_connect( "$host $dbname $credentials" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
function getPosts() {
$posts = array();
$posts[0] = $_POST['objectid'];
$posts[1] = $_POST['jobno'];
$posts[2] = $_POST['unit'];
$posts[3] = $_POST['coordinate'];
$posts[4] = $_POST['name'];
$posts[5] = $_POST['northing'];
$posts[6] = $_POST['easting'];
$posts[7] = $_POST['elev'];
return $posts;
}
// Selection Operation
if (isset($_POST['search'])) {
$data = getPosts();
$sql = "SELECT * FROM monu_from_Lst WHERE OBJECTID = $data[0]";
$ret = pg_query($db, $sql);
if($ret) {
if(pg_num_rows($ret)) {
while($row = pg_fetch_array($ret)) {
echo $objectid = $row['objectid'];
echo $jobno = $row['jobno'];
echo $unit = $row['unit'];
echo $coordinate = $row['coordinate'];
echo $name = $row['name'];
echo $northing = $row['northing'];
echo $easting = $row['easting'];
echo $elev = $row['elev'];
}
echo "Operation done successfully\n";
// pg_close($db);
}else{
echo 'No Data For This Id';
}
} else {
echo 'Result Error';
}
}
HTML
<form action="php_insert_update_delete_search.php" method="post">
<input type="number" name="objectid" placeholder="OBJECTID" value="<?php echo $objectid;?>">
<input type="text" name="jobno" placeholder="JOB#" value="<?php echo $jobno;?>">
<input type="text" name="unit" placeholder="UNIT" value="<?php echo $unit;?>">
<input type="text" name="coordinate" placeholder="COORDINATE" value="<?php echo $coordinate;?>">
<input type="text" name="name" placeholder="NAME" value="<?php echo $name;?>">
<input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>">
<input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>">
<input type="number" name="easting" placeholder="EASTING" value="<?php echo $easting;?>">
<input type="number" name="elev" placeholder="ElEVATION" value="<?php echo $elev;?>">
<div>
<input type="submit" name="search" value="Find">
</div>
</form>
Your code seems to be fine. The problem might be with
<form action="php_insert_update_delete_search.php" method="post">
If you are posting the file to itself try using
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
Make sure your query runs successfully. if your query does not run successfully
try following
$sql = "SELECT * FROM \"monu_from_Lst\" WHERE objectid= $data[0]";
I have working code as following
<?php
$host = "host=localhost"; // use your hostname
$dbname = "dbname=blogsitedb"; // use your dbname
$credentials = "user=postgres password=admin"; // use your credentials
$objectid = "";
$jobno = "";
$unit = "";
$coordinate = "";
$name = "";
$northing = "";
$easting = "";
$elev = "";
$db = pg_connect("$host $dbname $credentials");
if(!$db)
{
echo "Error : Unable to open database\n";
}
else
{
echo "Opened database successfully\n";
}
function getPosts()
{
$posts = array();
$posts[0] = $_POST['objectid'];
$posts[1] = $_POST['jobno'];
$posts[2] = $_POST['unit'];
/*
$posts[3] = $_POST['coordinate'];
$posts[4] = $_POST['name'];
$posts[5] = $_POST['northing'];
$posts[6] = $_POST['easting'];
$posts[7] = $_POST['elev'];
*/
return $posts;
}
// Selection Operation
if(isset($_POST['search']))
{
$data = getPosts();
$sql = "SELECT * FROM \"monu_from_Lst\" WHERE objectid= $data[0]";
$ret = pg_query($db, $sql);
if($ret)
{
if(pg_num_rows($ret))
{
while($row = pg_fetch_array($ret))
{
echo $objectid = $row['objectid'];
echo $jobno = $row['jobno'];
echo $unit = $row['unit'];
/*echo $coordinate = $row['coordinate'];
echo $name = $row['name'];
echo $northing = $row['northing'];
echo $easting = $row['easting'];
echo $elev = $row['elev'];*/
}
echo "Operation done successfully\n";
// pg_close($db);
}
else
{
echo 'No Data For This Id';
}
}
else
{
echo 'Result Error';
}
}
?>
<!DOCTYPE Html>
<html>
<head>
<title>PHP INSERT UPDATE DELETE SEARCH</title>
</head>
<body>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
<input type="number" name="objectid" placeholder="OBJECTID" value="<?php echo $objectid;?>"><br><br>
<input type="text" name="jobno" placeholder="JOB#" value="<?php echo $jobno;?>"><br><br>
<input type="text" name="unit" placeholder="UNIT" value="<?php echo $unit;?>"><br><br>
<input type="text" name="coordinate" placeholder="COORDINATE" value="<?php echo $coordinate;?>"><br><br>
<input type="text" name="name" placeholder="NAME" value="<?php echo $name;?>"><br><br>
<input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>"><br><br>
<input type="number" name="northing" placeholder="NORTHING" value="<?php echo $northing;?>"><br><br>
<input type="number" name="easting" placeholder="EASTING" value="<?php echo $easting;?>"><br><br>
<input type="number" name="elev" placeholder="ElEVATION" value="<?php echo $elev;?>"><br><br>
<div>
<!-- Input For Find Values With The given objectid -->
<input type="submit" name="search" value="Find">
</div>
</form>
</body>
</html>

Second form submit button seems to be skipped(?) PHP / HTML

I need help about this, I have a PHP page, which searches for records, based on their first and last names, if sql finds the data then the second form comes out, which has lots of textboxes, to update the 'searched'information. And when I click the submit button of the second form , it does nothing, and even if I have syntax or whatever errors I have put on condition if(isset($_POST['submit'])), they end up being disregarded (no error messages will come up), after clicking the submit button, it just goes back to the page's original state, when it has to update the record I have searched for that was just edited. What exactly is the mistake on this part?
class.php - .php file that contains the operations for sql
<? php
$months = array('January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December');
class EmployeeProfile {
public
function openConnection() {
$conn = mysqli_connect("localhost", "root", "", "db_employee");
if (mysqli_connect_errno()) {
echo "Failed to connect to database server";
}
return $conn;
}
public
function insert($query) {
if (mysqli_query($this - > openConnection(), $query) == 1) {
echo "Profile successfully registered!";
} else {
echo "Register failed";
}
}
public
function display($query) {
$result = mysqli_query($this - > openConnection(), $query);
echo "<br><br>";
if ($result - > num_rows == 1) {
while ($row = $result - > fetch_assoc()) {
echo "<table>";
echo "<tr><td><b>First Name</b>: ".$row["firstname"]."</td></tr>";
echo "<tr><td><b>Middle Name</b>: ".$row["middlename"]."</td></tr>";
echo "<tr><td><b>Last Name: </b>".$row["lastname"]."</td></tr>";
echo "<tr><td><b>Date of Birth: </b>".$row["dateofbirth"]."</td></tr>";
echo "<tr><td><b>Age: </b>".$row["age"]."</td></tr>";
echo "<tr><td><b>School: </b>".$row["school"]."</td></tr>";
echo "<tr><td><b>Highest Educational Attainment: </b>".$row["educ"]."</td></tr>";
echo "<tr><td><b>Year Last Attended: </b>".$row["yearattended"]."</td></tr>";
echo "<tr><td><b>Skills: </b>".$row["skills"]."</td></tr>";
echo "<tr><td><b>Previous Company: </b>".$row["prevcompany"]."</td></tr>";
echo "<tr><td><b>Position: </b>".$row["position"]."</td></tr>";
echo "<tr><td><b>Date of Employment:</b> ".$row["dateofemployment"]."</td></tr>";
echo "</table>";
}
} else
{
echo "Profile not found";
}
}
public
function edit($query) {
$result = mysqli_query($this - > openConnection(), $query);
}
}
?>
edit.php - the page itself.
<html>
<title> Edit Profile</title>
<body>
<form method="post" action="?" name="searchform">
<center>
<table>
<tr><td>Enter first or last name</td><td><input type = "text" name="search"><td><td><input type = "submit" value="Search" name="search2"></td></tr>
</form>
</table>
<?php
include("class.php");
if(isset($_POST['search2'])):
$status = "hidden";
$query = "select * from employee WHERE firstname='".$_POST['search']."' OR lastname='".$_POST['search']."' ";
$emp = new EmployeeProfile();
$emp->openConnection();
$result = mysqli_query($emp->openConnection(), $query);
if($result->num_rows == 1):
?>
<form method="post" action="?" enctype="multipart/form-data" name="updateform">
<table>
<tr></tr>
<tr><td></td><td>Edit your profile:</td></tr>
<tr></tr>
<tr><td>*Enter first name:</td><td><input type="text" name="firstname"></td></tr>
<tr><td>Enter middle name:</td><td><input type="text" name="middlename"></td></tr>
<tr><td>*Enter last name:</td><td><input type="text" name="lastname"></td></tr>
<tr>
<td>*Date of Birth:</td><td><select name="month"><?php for($i = 0; $i < count($months); $i++) { echo "<option value=".$months[$i]." >".$months[$i]."</option>" ; }?></select></td>
<td><select name="days"><?php for($i = 1; $i <= 31; $i++) { echo "<option value"."=".$i.">".$i."</option>"; } ?> </select></td>
<td><select name="year"><?php for($i = 1950; $i <= 2014; $i++) { echo "<option value"."=".$i.">".$i."</option>"; } ?> </select></td>
</tr>
<tr><td>*Age:</td><td><input type="text" name="age"></td></tr>
<tr><td>*School:</td><td><input type="text" name="school"></td></tr>
<tr><td>*Highest Educational Attainment:</td><td><input type="text" name="educ"></td></tr>
<tr><td>*Year Last Attended:</td><td><input type="text" name="yearattended"></td></tr>
<tr><td>*Skill(s):</td><td><input type="text" name="skills"></td></tr>
<tr><td>Previous Company:</td><td><input type="text" name="prevcompany"></td></tr>
<tr><td>Position:</td><td><input type="text" name="position"></td></tr>
<tr><td>*Date of Employment:</td><td><select name="empmonth"><?php for($i = 0; $i < count($months); $i++) { echo "<option value=".$months[$i]." >".$months[$i]."</option>" ; }?></select></td>
<td><select name="empyear"><?php for($i = 1950; $i <= 2014; $i++) { echo "<option value"."=".$i.">".$i."</option>"; } ?> </select></td>
</tr>
<tr><td></td><td><input type="submit" value="Register" name="submit"></td></tr>
<tr><td>* - Required</td></tr>
</form>
<?php
if(isset($_POST['submit'])):
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$dateofbirth = $_POST['month']. " ".$_POST['days']. ", ".$_POST['year'];
$age = $_POST['age'];
$school = $_POST['school'];
$educ = $_POST['educ'];
$yearattended = $_POST['yearattended'];
$skills = $_POST['skills'];
$prevcompany = $_POST['prevcompany'];
$position = $_POST['position'];
$dateofemployment = $_POST['empmonth']. " ".$_POST['empyear'];
$row = $result->fetch_assoc();
$usr = $row["firstname"];
$query2 = "UPDATE employee SET firstname='$firstname', middlename='$middlename', lastname='$lastname', dateofbirth='$dateofbirth', age='$age', school='$school',
educ='$educ', yearattended='$yearattended', skills='$skills', prevcompany='$prevcompany', position='$position', dateofemployment='$dateofemployment',
WHERE firstname='$usr'";
mysqli_query($emp->openConnection(), $query2);
endif;
else:
echo "Profile not found";
endif;
endif;
?>
</table>
</center>
</body>
</html>
and I do really think that this line and beyond gets ignored.
This is part of the edit.php file that is shown above.
<?php
if(isset($_POST['submit'])):
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$dateofbirth = $_POST['month']. " ".$_POST['days']. ", ".$_POST['year'];
$age = $_POST['age'];
$school = $_POST['school'];
$educ = $_POST['educ'];
$yearattended = $_POST['yearattended'];
$skills = $_POST['skills'];
$prevcompany = $_POST['prevcompany'];
$position = $_POST['position'];
$dateofemployment = $_POST['empmonth']. " ".$_POST['empyear'];
$row = $result->fetch_assoc();
$usr = $row["firstname"];
$query2 = "UPDATE employee SET firstname='$firstname', middlename='$middlename', lastname='$lastname', dateofbirth='$dateofbirth', age='$age', school='$school',
educ='$educ', yearattended='$yearattended', skills='$skills', prevcompany='$prevcompany', position='$position', dateofemployment='$dateofemployment',
WHERE firstname='$usr'";
mysqli_query($emp->openConnection(), $query2);
endif;
In general, there are two kinds of errors present.
HTML tag order errors, which are extensive
Syntax errors.
- > must be -> to be properly parsed
<? php must be <?php to be properly parsed
The syntax errors are present in both files.
Note: the following code contains some debug statements.
<html>
<head>
<?php
echo "<p>In myquery.php header </p>";
error_reporting(E_ALL);
//echo "<p>" . var_dump($_POST); . "</p>";
//echo "<p>" . var_dump($_GET); . "</p>";
?>
<?php
include("class.php");
?>
<title> Edit Profile</title>
</head>
<body>
<?php
echo "<p>In myquery.php body</p>";
?>
<form method="post" action="?" name="searchform">
<table>
<tr>
<td>Enter first or last name</td>
<td><input type = "text" name="search"><td>
<td><input type = "submit" value="Search" name="search2"></td>
</tr>
</table>
</form>
<?php
echo "<p>about to check _post for search2</p>";
if(isset($_POST['search2'])):
echo "<p>found _post for search2</p>";
$status = "hidden";
$query = "select * from employee WHERE firstname='".$_POST['search']."' OR lastname='".$_POST['search']."' ";
echo "<p>about to open DB</p>";
$emp = new EmployeeProfile();
$emp->openConnection();
echo "<p>about to place find query</p>";
$result = mysqli_query($emp->openConnection(), $query);
echo "<p>about to check for successful query</p>";
if($result->num_rows == 1):
echo "<p>successful search query</p>";
?>
<form method="post" action="?" enctype="multipart/form-data" name="updateform">
<table>
<tr></tr>
<tr><td></td><td>Edit your profile:</td></tr>
<tr></tr>
<tr><td>*Enter first name:</td><td><input type="text" name="firstname"></td></tr>
<tr><td>Enter middle name:</td><td><input type="text" name="middlename"></td></tr>
<tr><td>*Enter last name:</td><td><input type="text" name="lastname"></td></tr>
<tr>
<td>*Date of Birth:</td><td><select name="month"><?php for($i = 0; $i < count($months); $i++) { echo "<option value=".$months[$i]." >".$months[$i]."</option>" ; }?></select></td>
<td><select name="days"><?php for($i = 1; $i <= 31; $i++) { echo "<option value"."=".$i.">".$i."</option>"; } ?> </select></td>
<td><select name="year"><?php for($i = 1950; $i <= 2014; $i++) { echo "<option value"."=".$i.">".$i."</option>"; } ?> </select></td>
</tr>
<tr><td>*Age:</td><td><input type="text" name="age"></td></tr>
<tr><td>*School:</td><td><input type="text" name="school"></td></tr>
<tr><td>*Highest Educational Attainment:</td><td><input type="text" name="educ"></td></tr>
<tr><td>*Year Last Attended:</td><td><input type="text" name="yearattended"></td></tr>
<tr><td>*Skill(s):</td><td><input type="text" name="skills"></td></tr>
<tr><td>Previous Company:</td><td><input type="text" name="prevcompany"></td></tr>
<tr><td>Position:</td><td><input type="text" name="position"></td></tr>
<tr><td>*Date of Employment:</td><td><select name="empmonth"><?php for($i = 0; $i < count($months); $i++) { echo "<option value=".$months[$i]." >".$months[$i]."</option>" ; }?></select></td>
<td><select name="empyear"><?php for($i = 1950; $i <= 2014; $i++) { echo "<option value"."=".$i.">".$i."</option>"; } ?> </select></td>
</tr>
<tr><td></td><td><input type="submit" value="Register" name="submit"></td></tr>
<tr><td>* - Required</td></tr>
</table>
</form>
<?php
echo "<p>about to check for submit second form</p>";
if(isset($_POST['submit'])):
echo "<p>found submit for second form</p>";
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$dateofbirth = $_POST['month']. " ".$_POST['days']. ", ".$_POST['year'];
$age = $_POST['age'];
$school = $_POST['school'];
$educ = $_POST['educ'];
$yearattended = $_POST['yearattended'];
$skills = $_POST['skills'];
$prevcompany = $_POST['prevcompany'];
$position = $_POST['position'];
$dateofemployment = $_POST['empmonth']. " ".$_POST['empyear'];
$row = $result->fetch_assoc();
$usr = $row["firstname"];
$query2 =
"UPDATE employee
SET firstname='$firstname',
middlename='$middlename',
lastname='$lastname',
dateofbirth='$dateofbirth',
age='$age',
school='$school',
educ='$educ',
yearattended='$yearattended',
skills='$skills',
prevcompany='$prevcompany',
position='$position',
dateofemployment='$dateofemployment',
WHERE firstname='$usr'";
echo "<p>about to update DB</p>";
mysqli_query($emp->openConnection(), $query2);
endif;
else:
echo "<p>search query failed</p>";
echo "Profile not found";
endif;
endif;
?>
</body>
</html>

Only updating half the data to MySQL table

The code below is for updating data in a MySQL table. It was written by pulling all the data from one query but I have tried to adapt it to pull data from two queries to improve the ordering. Now only some of the records update when the submit button is clicked and I'm not sure how to fix it.
The original code was:
if(isset($_POST['submit'])){
$password = $_POST['password'];
$total = $_POST['total'];
$park_id = $_POST['park_id'];
if($password=="****"){
for($i =1; $i<=$total; $i++){
$ride_id = $_POST['ride_id'.$i];
$name = $_POST['ride_name'.$i];
$type = $_POST['type'.$i];
$topride = $_POST['topride'.$i];
$info = $_POST['info'.$i];
$speed = $_POST['speed'.$i];
$height = $_POST['height'.$i];
$length = $_POST['length'.$i];
$inversions = $_POST['inversions'.$i];
$query = "update tpf_rides set name='$name',type='$type'";
if($topride!=""){$query .= ",top_ride=$topride";}
if($info!=""){$query .= ",info='$info'";}
if($height!=""){$query .= ",height=$height";}
if($length!=""){$query .= ",length=$length";}
if($speed!=""){$query .= ",speed=$speed";}
if($inversions!=""){$query .= ",inversions=$inversions";}
$query .= " where ride_id=".$ride_id." and park_id=".$park_id;
mysql_query($query);
}
header('location:index.php?msg=Successfully Updated.');
}else{
echo "Enter Correct Password.";
}
}
if(isset($_GET['id'])){
$id = $_GET['id'];
$sql = "select name from tpf_parks where park_id=".$id;
$result = mysql_fetch_array(mysql_query($sql));
echo '<h2>'.$result['name'].'</h2>';
$qry = "select * from tpf_rides where park_id=".$id;
$res = mysql_query($qry);
$no = mysql_num_rows($res);
$x = 0;
if($no>0){ ?>
<form action="" method="post">
<input type="hidden" value="<?=$no?>" name="total">
<input type="hidden" value="<?=$id?>" name="park_id">
<table> <?php
while($row = mysql_fetch_array($res)){ $x++;
echo '<input type="hidden" value="'.$row['ride_id'].'" name="ride_id'.$x.'">';
echo '<tr><td>Name : </td><td><input type="text" name="ride_name'.$x.'" value="'.$row['name'].'"></td></tr>';
echo '<tr><td>Type : </td><td><input type="text" name="type'.$x.'" value="'.$row['type'].'"></td></tr>';
echo '<tr><td>Top Ride : </td><td><input type="text" name="topride'.$x.'" value="'.$row['top_ride'].'"></td></tr>';
echo '<tr><td>Info : </td><td><input type="text" name="info'.$x.'" value="'.$row['info'].'"></td></tr>';
if($row['type']!="Roller Coaster"){
echo '<tr><td>Speed : </td><td><input type="text" name="speed'.$x.'" value="'.$row['speed'].'"></td></tr>';
echo '<tr><td>Height : </td><td><input type="text" name="height'.$x.'" value="'.$row['height'].'"></td></tr>';
echo '<tr><td>Length : </td><td><input type="text" name="length'.$x.'" value="'.$row['length'].'"></td></tr>';
echo '<tr><td>Inversions : </td><td><input type="text" name="inversions'.$x.'" value="'.$row['inversions'].'"></td></tr>';
}
echo '<tr><td colspan="2"><hr></td></tr>';
} ?>
<tr><td>Password :</td><td><input type="password" value="" name="password" id="password"></td></tr>
<tr><td></td><td><input onclick="return check()" type="submit" value="Save" name="submit"></td></tr>
</table>
</form>
<?php
}else{
echo "No Rides in this park.";
}
}else{
if(isset($_GET['msg'])){echo $_GET['msg'].'<br>';}
$qry = "select * from tpf_parks order by name";
$res = mysql_query($qry);
?>
Select Park : <select name="park" onChange="getdata(this.options[this.selectedIndex].value)">
<option value="">Select Park</option>
<?php
while($row = mysql_fetch_array($res)) { ?>
<option value="<?=$row['park_id']?>"><?=$row['name']?></option>
<? } ?>
</select>
<?php } ?>
and the new code where I altered the queries is here:
if(isset($_POST['submit'])){
$password = $_POST['password'];
$total = $_POST['total'];
$park_id = $_POST['park_id'];
if($password=="*****"){
for($i =1; $i<=$total; $i++){
$ride_id = $_POST['ride_id'.$i];
$name = $_POST['ride_name'.$i];
$type = $_POST['type'.$i];
$topride = $_POST['topride'.$i];
$info = $_POST['info'.$i];
$speed = $_POST['speed'.$i];
$height = $_POST['height'.$i];
$length = $_POST['length'.$i];
$inversions = $_POST['inversions'.$i];
$query = "update tpf_rides set name='$name',type='$type'";
if($topride!=""){$query .= ",top_ride=$topride";}
$query .= ",info='$info'";
if($height!=""){$query .= ",height=$height";}
if($length!=""){$query .= ",length=$length";}
if($speed!=""){$query .= ",speed=$speed";}
if($inversions!=""){$query .= ",inversions=$inversions";}
$query .= " where ride_id=".$ride_id." and park_id=".$park_id;
mysql_query($query);
}
header('location:index.php?msg=Successfully Updated.');
}else{
echo "Enter Correct Password.";
}
}
if(isset($_GET['id'])){
$id = $_GET['id'];
$sql = "select name from tpf_parks where park_id=".$id;
$result = mysql_fetch_array(mysql_query($sql));
echo '<h2>'.$result['name'].'</h2>';
$qry = "SELECT * FROM tpf_rides
WHERE park_id = $id AND type LIKE '%Roller Coaster%' ORDER BY name ASC";
$res = mysql_query($qry);
$qry2 = "SELECT * FROM tpf_rides
WHERE park_id = $id AND type NOT LIKE '%Roller Coaster%' ORDER BY name ASC";
$res2 = mysql_query($qry2);
$qry3 = "SELECT * FROM tpf_rides WHERE park_id = $id";
$res3 = mysql_query($qry2);
$no = mysql_num_rows($res3);
$x = 0;
$xx = 0;
if($no>0){ ?>
<form action="" method="post">
<input type="hidden" value="<?=$no?>" name="total">
<input type="hidden" value="<?=$id?>" name="park_id">
<table> <?php
while($row = mysql_fetch_array($res)){ $x++;
echo '<input type="hidden" value="'.$row['ride_id'].'" name="ride_id'.$x.'">';
echo '<tr><td>Name : </td><td><input type="text" name="ride_name'.$x.'" value="'.$row['name'].'"></td></tr>';
echo '<tr><td>Type : </td><td><input type="text" name="type'.$x.'" value="'.$row['type'].'"></td></tr>';
echo '<tr><td>Top Ride : </td><td><input type="text" name="topride'.$x.'" value="'.$row['top_ride'].'"></td></tr>';
echo '<tr><td>Info : </td><td><input type="text" name="info'.$x.'" value="'.$row['info'].'"></td></tr>';
echo '<tr><td>Speed : </td><td><input type="text" name="speed'.$x.'" value="'.$row['speed'].'"></td></tr>';
echo '<tr><td>Height : </td><td><input type="text" name="height'.$x.'" value="'.$row['height'].'"></td></tr>';
echo '<tr><td>Length : </td><td><input type="text" name="length'.$x.'" value="'.$row['length'].'"></td></tr>';
echo '<tr><td>Inversions : </td><td><input type="text" name="inversions'.$x.'" value="'.$row['inversions'].'"></td></tr>';
echo '<tr><td colspan="2"><hr></td></tr>';
}
while($row2 = mysql_fetch_array($res2)){ $xx++;
echo '<input type="hidden" value="'.$row2['ride_id'].'" name="ride_id'.$xx.'">';
echo '<tr><td>Name : </td><td><input type="text" name="ride_name'.$xx.'" value="'.$row2['name'].'"></td></tr>';
echo '<tr><td>Type : </td><td><input type="text" name="type'.$xx.'" value="'.$row2['type'].'"></td></tr>';
echo '<tr><td>Top Ride : </td><td><input type="text" name="topride'.$xx.'" value="'.$row2['top_ride'].'"></td></tr>';
echo '<tr><td>Info : </td><td><input type="text" name="info'.$xx.'" value="'.$row2['info'].'"></td></tr>';
echo '<tr><td colspan="2"><hr></td></tr>';
}
?>
<tr><td>Password :</td><td><input type="password" value="" name="password" id="password"></td></tr>
<tr><td></td><td><input onclick="return check()" type="submit" value="Save" name="submit"></td></tr>
</table>
</form>
<?php
}else{
echo "No Rides in this park.";
}
}else{
if(isset($_GET['msg'])){echo $_GET['msg'].'<br>';}
$qry = "select * from tpf_parks order by name";
$res = mysql_query($qry);
?>
Select Park : <select name="park" onChange="getdata(this.options[this.selectedIndex].value)">
<option value="">Select Park</option>
<?php
while($row = mysql_fetch_array($res)) { ?>
<option value="<?=$row['park_id']?>"><?=$row['name']?></option>
<? } ?>
</select>
<?php } ?>
After testing what is not getting amended, it is data from both the LIKE and NOT LIKE queries being skipped so perhaps a record count problem?
Any ideas what I have done wrong?
Are you sure your column type has value 'Roller Coaster' or not?
Sorry to post as an answer as I have no rights no comment.

previous/next record button php/mysql woes

My question deals with my next/previous buttons. I can get my update/delete buttons to work, but I'm so ready to tear out my hair when dealing with the next/previous buttons. Any help would be spectacular! Here's my code. Also, I'm pretty new to PHP so if this is bad coding, please let me know and point me in the right direction so I can fix my mistakes. Thanks!!!
session_start();
include "connectionfile.php";
if (isset($_POST['fname']) &&
isset($_POST['lname']) &&
isset($_POST['email']) &&
isset($_POST['login']) &&
isset($_POST['password']) &&
isset($_POST['super']) &&
isset($_POST['foldername']))
{
$id = get_post('id');
$fname = get_post('fname');
$lname = get_post('lname');
$email = get_post('email');
$login = get_post('login');
$password = hash('sha256', get_post('password'));
$super = get_post('super');
$foldername = get_post('foldername');
if ($_POST['submit']==0){
$query = mysql_query("SELECT * FROM `Logins` WHERE ID < '".$id."' ORDER BY ID DESC LIMIT 1;");
while($row = mysql_fetch_array($query)){
$id = $row['ID'];
$fname = $row['fname'];
$lname = $row['lname'];
$email = $row['email'];
$login = $row['login'];
$password = $row['password'];
$super = $row['super'];
$foldername = $row['foldername'];
}
}else if ($_POST['submit']==1){
$query = "UPDATE Logins SET fname = '$fname', lname='$lname', email='$email".'#carouselclinical.com'."', login='$login', password='$password', super='$super', foldername='$foldername'";
$query .= "WHERE ID = '$id';";
if (!mysql_query($query, $connect))
echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}else if($_POST['submit']==2){
$delete_query = "DELETE FROM Logins WHERE ID = '".$id."';";
mysql_query($delete_query);
$rc = mysql_affected_rows();
echo "Rows Affected " . $rc;
}
if ($_POST['submit']==3){
$query = mysql_query("SELECT * FROM `Logins` WHERE ID= '". $id ."' ORDER BY ID ASC LIMIT 1;");
while($row = mysql_fetch_array($query)){
$id = $row['ID'];
$fname = $row['fname'];
$lname = $row['lname'];
$email = $row['email'];
$login = $row['login'];
$password = $row['password'];
$super = $row['super'];
$foldername = $row['foldername'];
}
}
}
mysql_close($connect);
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>
<form action="" method="post"><pre>
id <input type="text" readonly="readonly" name="id" value="<?php echo "$id"; ?>" />
First Name <input type="text" name="fname" value="<?php echo "$fname"; ?>" />
Last Name <input type="text" name="lname" value="<?php echo "$lname"; ?>" />
Email <input type="text" name="email" value="<?php echo "$email"; ?>" /> There's no need to put #carouselclinical.com.
Login <input type="text" name="login" value="<?php echo "$login"; ?>"/>
Password <input type="text" name="password" value="<?php echo "$password"; ?>"/>
Super? <input type="text" name="super" value="<?php echo "$super"; ?>" />
foldername <input type="text" name="foldername" value="<?php echo "$foldername"; ?>" />
<button name="submit" value="0">Previous</button>
<button name="submit" value="1">UPDATE</button>
<button name="submit" value="2">Delete</button>
<button name="submit" value="3">Next</button>
</pre>
Home <br />
Log out
</form>
Try adding an else right above mysql_close($connect);. My guess is that on the initial page load you are not posting any values, so no action is taken. This will create a default ID if none is defined in your top if.
else{
$query = mysql_query("SELECT * FROM `Logins` ORDER BY ID ASC LIMIT 1;");
while($row = mysql_fetch_array($query)){
$id = $row['ID'];
$fname = $row['fname'];
$lname = $row['lname'];
$email = $row['email'];
$login = $row['login'];
$password = $row['password'];
$super = $row['super'];
$foldername = $row['foldername'];
}
Also, on your if ($_POST['submit']==3), you need to change the = to > in your $query so you can get the next record. Currently you would be selecting the same ID, not the next higher.
$query = mysql_query("SELECT * FROM `Logins` WHERE ID > '". $id ."' ORDER BY ID ASC LIMIT 1;");
Finally, when doing Previous/Next you also need to take into consideration how you will deal with Previous when you are on the first ID, and Next when you are on the last id, as you will return an empty result set from MySQL.

Categories