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>
Related
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
I have set a counter in php code to increment the id value in mysql on every next click but when I refresh or reload the page the value is increasing automatically is there any solution for this problem or any other substitute.
<?php
$db = mysqli_connect('localhost','root','root','rahul');
$questions ="";
$msg2 ="";
$o1 ="" ;
$o2 ="" ;
$o3 ="" ;
$o4 ="" ;
$disable = "";
$disable2 = "";
session_start();
if(empty($_SESSION['count']))
$_SESSION['count'] = 0;
if(isset($_POST['sub1'])){
$ans = $_POST['ans'];
$email = "rahul#gmail.com";
$order = $_SESSION['count']+1;
echo $order;
$_SESSION['count'] = $order;
$sql = (" SELECT * FROM qna WHERE id = $order ");
$query = mysqli_query($db, $sql);
$row=mysqli_fetch_array($query, MYSQLI_ASSOC);
$questions = $row['questions'];
$o1 = $row['o1'];
$o2 = $row['o2'];
$o3 = $row['o3'];
$o4 = $row['o4'];
$disable="";
if($_SESSION['count']>5)
{
$disable = "disabled";
}
$disable2 = "";
if($_SESSION['count']<=1)
{
$disable2 = "disabled";
}
//$sql2 = "INSERT INTO result (id, answer, email) VALUES ('', '$ans', '$email') ".mysqli_error();
/*
$sql3 = mysqli_query($db, "INSERT INTO result (answer, email) VALUES ('$ans', '$email')");
if(mysqli_affected_rows($sql3)== true)
{
echo "inserted";
}
else
{
echo "not inserted";
}
*/
echo $ans. $email;
}
$sql4 = mysqli_query("select * from result");
$row = mysqli_fetch_array($db, $sql4);
// while()
echo $row['id'];
for($i=1;$i<=5;$i++)
{
}
?>
<?php
if(isset($_POST['sub2'])){
$result2 = $_SESSION['count']-1;
$_SESSION['count'] = $result2;
$sql = (" SELECT * FROM qna WHERE id = $result2 ");
$query = mysqli_query($db, $sql);
$row=mysqli_fetch_array($query, MYSQLI_ASSOC);
$questions = $row['questions'];
$o1 = $row['o1'];
$o2 = $row['o2'];
$o3 = $row['o3'];
$o4 = $row['o4'];
if($_SESSION['count']<=1){
$disable2 = "disabled";
}
}
session_write_close();
?>
<?php
if(isset($_POST['start'])){
$order = $_SESSION['count']+1;
echo $order;
$_SESSION['count'] = $order;
$sql = (" SELECT * FROM qna WHERE id = 1 ");
$query = mysqli_query($db, $sql);
$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
$questions = $row['questions'];
$o1 = $row['o1'];
$o2 = $row['o2'];
$o3 = $row['o3'];
$o4 = $row['o4'];
$disable="";
if($_SESSION['count']>=5)
{
$disable = "disabled";
}
$disable2 = "";
if($_SESSION['count']<=1){
$disable2 = "disabled";
}
session_write_close();
}
?>
<center><br><br><br>
<form method="post">
<input type="submit" name="start" value="start">
</form>
Log out
<form action="" method="post" >
<table border="1" height="300px" width="500px">
<tr>
<th colspan="2"><?php echo $questions; ?></th>
</tr>
<tr>
<td><input type="radio" name="ans" id="ans" value="<?php echo $o1; ?>"><?php echo $o1; ?></td>
<td><input type="radio" name="ans" value="<?php echo $o2; ?>"><?php echo $o2; ?></td>
</tr>
<tr>
<td><input type="radio" name="ans" value="<?php echo $o3; ?>"><?php echo $o3; ?></td>
<td><input type="radio" name="ans" value="<?php echo $o4; ?>"><?php echo $o4; ?></td>
</tr>
<tr colspan="2">
<td><center><input type="submit" name="sub1" value="next" <?php echo $disable ?>> </td>
<td><center><input type="submit" name="sub2" value="previous" <?php echo $disable2 ?>>
<input type="submit" name="submit3" value="submit" > </td>
</tr>
</form>
</table>
<?php
if(isset($_POST['submit3']))
{
$ans = $_POST['ans'];
$email = "dummy";
//$sql2 = "INSERT INTO result (id, answer, email) VALUES ('', '$ans', '$email') ".mysqli_error();
$sql3 = mysqli_query($db, "INSERT INTO result (answer, email) VALUES ('$ans', '$email')");
if(mysqli_affected_rows($sql3)== true)
{
echo "inserted";
}
else
{
echo "not inserted";
}
echo $ans. $email;
}
?>
when you are reloading a web-page, you are reloading its POST (and also GET) data as well if it's there. if you are submitting a form then the target page contains POST data in its header. so if you reload this page it's like you would have clicked the button again.
since you are already using a session there is a workaround:
add a hidden field with a micro-timestamp in your form. this micro-timestamp will be different every time your page gets loaded (per user) - but this "new" timestamp only get's posted when you use the button. when you just refresh the page, you are reloading with the old timestamp.
so you just need to save compare the last timestamp (saved in a session variable) with the currently posted timestamp. if they are equal - the page just got refreshed - if they are not equal, then you got a new timestamp which was sent by your form:
<?php
session_start();
if(!isset($_SESSION["timestamp"]))
$_SESSION["timestamp"] = 0;
if(!isset($_POST["timestamp"]))
$_POST["timestamp"] = 0;
// previous timestamp - saved in session variable:
$prev_ts = $_SESSION["timestamp"];
// currently posted timestamp:
$post_ts = $_POST["timestamp"];
if($prev_ts != $post_ts)
{
// code to increase your counter goes here.
$feedback = "button pressed";
}
else
{
// do nothing when the page just got refreshed
$feedback = "refreshed";
}
$_SESSION["timestamp"] = $post_ts;
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php echo $feedback; ?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<input type="hidden" name="timestamp" value="<?php echo microtime(); ?>">
<input type="submit" name="go" value="count">
</form>
</body>
</html>
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
?>
How do I create a link to each photo or text of these search results to a profile.php to query and output database?
In a nutshell - search database and click any result that would take you to their profile page. Example: Match.com, facebook.com...etc (search and click to view profile).
Also, how can I output first name and last name on the same line?
Please Help.
Here is the HTML Search Form.
<h2>Search</h2>
<form name="search" action="searchresults.php" method="POST">
Search for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="firstName">First Name</option>
<Option VALUE="email">Email</option>
</Select>
Search for: <input type="text" name="find1" /> in
<Select NAME="field1">
<Option VALUE="lastName">Last Name</option>
</Select>
<br><br>
Search for: <input type="text" name="find2" /> in
<Select NAME="field2">
<Option VALUE="gender">Gender</option>
</Select>
<br><br>
Search for: <input type="text" name="find3" /> in
<Select NAME="field3">
<Option VALUE="age">Age</option>
</Select>
<br><br>
Search for: <input type="text" name="find4" /> in
<Select NAME="field4">
<Option VALUE="city">City</option>
</Select>
Search for: <input type="text" name="find5" /> in
<Select NAME="field5">
<Option VALUE="state">State</option>
</Select>
<br><br>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
Here is the searchresults.php.
DATABASE CONNECTION
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$find=$_POST['find'];
$field=$_POST['field'];
$find1=$_POST['find1'];
$field1=$_POST['field1'];
$find2=$_POST['find2'];
$field2=$_POST['field2'];
$find3=$_POST['find3'];
$field3=$_POST['field3'];
$find4=$_POST['find4'];
$field4=$_POST['field4'];
$find5=$_POST['find5'];
$field5=$_POST['field5'];
$data="SELECT firstName, lastName, email, userphoto, gender, age, city, state FROM actorsInfo
WHERE upper($field) LIKE '%$find%'
AND upper($field1) LIKE '%$find1%'
AND upper($field2) LIKE '%$find2%'
AND upper($field3) LIKE '%$find3%'
AND upper($field4) LIKE '%$find4%'
AND upper($field5) LIKE '%$find5%'
";
$result = mysql_query($data);
$count=mysql_numrows($result);
echo '<br><br>';
if($count > 0){
echo"<table border=0>";
//get images and names in two arrays
$firstName= $row["firstName"];
$lastName= $row["lastName"];
$email= $row["email"];
$userphoto= $row["userphoto"];
$gender= $row["gender"];
$age= $row["age"];
$city= $row["city"];
$state= $row["state"];
$age = array();
$gender = array();
$userphoto = array();
$firstName = array();
$lastName = array();
$city = array();
$state = array();
while ($row = mysql_fetch_array($result))
{
$userphoto[] = "<img src='images/".$row['userphoto']."' height='200' width='160'>";
$firstName[] = $row['firstName'];
$lastName[] = $row['lastName'];
$age[] = $row['age'];
$gender[] = $row['gender'];
$email[] = $row['email'];
$city[] = $row['city'];
$state[] = $row['state'];
}
while(!empty($userphoto))
{
//output images
foreach(array($userphoto, $firstName, $lastName, $age, $email, $city, $state) as $items)
{
echo "<tr>";
foreach($items as $key=>$item)
{
echo "<td><font size =\"2\" >$item</td>";
//output only four of them
if($key==4)
{
break;
}
}
echo "</tr>";
}
//remove the first five images from $images because they're already printed
$userphoto = array_slice($userphoto, 5);
$firstName = array_slice($firstName, 5);
$lastName= array_slice($lastName, 5);
$email = array_slice($email, 5);
$age = array_slice($age, 5);
$city = array_slice($city, 5);
$state = array_slice($state, 5);
}
Here is the solution that passes the id to profile.php.
$userphoto[] = "<a href='profile.php?id=".$row['id']."'><img src='images/".$row['userphoto']."' height='200' width='160'></a>";
Here is the profile.php.
<?php
$id = (int)$_GET['id'];
if (isset($_GET['id']))
{
//fetch and display the information with database Query
$con=mysqli_connect("127.0.0.1", "admin", "password","actors");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM actorsInfo where id = " . $_GET['id']);
while($row = mysqli_fetch_array($result))
{
echo $row['firstName'] . " " . $row['email'];
echo "<br>";
}
mysqli_close($con);
}
?>
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.