Form validates but won't submit data to database table - php

I'm working on this project and it's pretty much finished, it's a form that validates if the user submits invalid data, but for some reason the data won't store in the table and therefore won't get posted back to the screen, I'm just wondering if anybody could spot where I'm going wrong as I've been looking at it for over an hour. Thanks a lot.
Below is my code:
index.php
<!Doctype html public>
<body>
<table cellpadding="5">
<td>
<h1> Games Club Website</h1>
<form action="process.php" method = "post">
<tr>
<td class="label">
<label for="firstName">
First name
</label>
</td>
<td>
<input type="text"
name="firstName"
id="firstName"
value="<?php
if (isset($validator))
echo $validator->getValue('firstName');
?>"
/>
<span class="error">
<?php
if (isset($validator))
echo $validator->getError('firstName');
?>
</span>
</td>
</tr>
<tr>
<td class="label">
<label for="lastName">
Surname
</label>
</td>
<td>
<input type="text"
name="lastName"
id="lastName"
value="<?php
if (isset($validator))
echo $validator->getValue('lastName');
?>"
/>
<span class="error">
<?php
if (isset($validator))
echo $validator->getError('lastName');
?>
</span>
</td>
</tr>
<tr>
<td class="label">
<label for="email">
Email Address
</label>
</td>
<td>
<input type="text"
name="email"
id="email"
value="<?php
if (isset($validator))
echo $validator->getValue('email');
?>"
/>
<span class="error">
<?php
if (isset($validator))
echo $validator->getError('email');
?>
</span>
</td>
</tr>
<tr>
<td class="label">
<label for="age">
Age
</label>
</td>
<td>
<input type="text"
name="age"
id="age"
value="<?php
if (isset($validator))
echo $validator->getValue('age');
?>"
/>
<span class="error">
<?php
if (isset($validator))
echo $validator->getError('age');
?>
</span>
</td>
</tr>
<tr>
<td class="label">
<label>
Gender
</label>
</td>
<td>
<label for="genderMale">Male</label>
<input type="radio"
name="gender"
id="genderMale"
value="Male"
<?php
if (isset($validator))
echo $validator->isChecked("gender", "Male");
?>
/>
<label for="genderFemale">Female?</label>
<input type="radio"
name="gender"
id="genderFemale"
value="Female"
<?php
if (isset($validator))
echo $validator->isChecked("gender", "Female");
?>
/>
<span class="error">
<?php
if (isset($validator))
echo $validator->getError('gender');
?>
</span>
</td>
</tr>
<tr>
<td class="label">
<label>
What is your preferred gaming platform?
</label>
</td>
<td>
<label for="consoleXbox">Xbox 360</label>
<input type="radio"
name="console"
id="consoleXbox"
value="Xbox 360"
<?php
if (isset($validator))
echo $validator->isChecked("console", "Xbox 360");
?>
/>
<label for="consolePs3">Playstation 3</label>
<input type="radio"
name="console"
id="consolePs3"
value="PS3"
<?php
if (isset($validator))
echo $validator->isChecked("console", "PS3");
?>
<label for="consoleWii">Nintendo Wii</label>
<input type="radio"
name="Console"
id="consoleWii"
value="Wii"
<?php
if (isset($validator))
echo $validator->isChecked("console", "Wii");
?>
/>
<span class="error">
<?php
if (isset($validator))
echo $validator->getError('console');
?>
</span>
</td>
</tr>
<tr>
<td class="label">
<label for="password1">
Enter a password:
</label>
</td>
<td>
<input type="password"
name="p1"
id="p1"
value="<?php
if (isset($validator))
echo $validator->getValue('p1');
?>"
/>
<span class="error">
<?php
if (isset($validator))
echo $validator->getError('p1');
?>
</span>
</td>
</tr>
<tr>
<td class="label">
<label for="p2">
Confirm password:
</label>
</td>
<td>
<input type="password"
name="p2"
id="p2"
value="<?php
if (isset($validator))
echo $validator->getValue('p2');
?>"
/>
<span class="error">
<?php
if (isset($validator))
echo $validator->getError('p2');
?>
</span>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit"
name="submitButton"
id="submitButton"
value="Confirm Registration" />
<input type="reset"
name="resetButton"
id="resetButton"
value="Clear Data"
style="margin-right: 20px;" />
</td>
</tr>
</form>
</td>
</table>
</body>
</html>
Process.php
<?php
require_once "FormValidator.php";
$validator = new FormValidator();
if ($validator->validate($_POST)) {
require 'dao.php';
}
else {
require 'index.php';
}
?>
FormValidator.php
<?php
class FormValidator {
private $valid;
private $errors;
private $data;
public function __construct() {
$this->valid = TRUE;
$this->errors = array();
$this->data = NULL;
}
public function validate($data) {
$this->data = $data;
if (empty($data['firstName'])) {
$this->valid = FALSE;
$this->errors['firstName'] = 'A <u>First Name</u> is required<br/>';
}
if (empty($data['lastName'])) {
$this->valid = FALSE;
$this->errors['lastName'] = 'A <u>Surname</u> is required.<br/>';
}
if (empty($data['p1'])) {
$this->valid = FALSE;
$this->errors['p1'] = 'A <u>Password</u> is required.<br/>';
}
if (empty($data['console'])) {
$this->valid = FALSE;
$this->errors['console'] = 'Please choose a <u>Console</u>.<br/>';
}
if (empty($data['p2'])) {
$this->valid = FALSE;
$this->errors['p2'] = 'Please <u>Confirm</u> password.<br/>';
}
if (empty($data['age'])) {
$this->valid = FALSE;
$this->errors['age'] = 'Please enter your <u>Age</u>.<br/>';
}
else if (!$this->isValidIntegerInRange($data['age'], 18, 100)) {
$this->valid = FALSE;
$this->errors['age'] = 'Invalid age. You also need to be at least 18 to sign up.<br/>';
}
if (empty($data['email'])) {
$this->valid = FALSE;
$this->errors['email'] = 'Please enter a valid <u>email address</u>.<br/>';
}
else if (!$this->isValidEmail($data['email'])) {
$this->valid = FALSE;
$this->errors['email'] = 'Incorrect format (name#website.something is required)<br/>';
}
if (empty($data['p2'])) {
$this->valid = FALSE;
$this->errors['p2'] = 'Please <u>Confirm</u> password.<br/>';
}
if (!empty($data['p1'])
&& !empty($data['p2'])
&& $data['p1'] !== $data['p2']) {
$this->valid = FALSE;
$this->errors['p2'] = 'Error, passwords <u>do not match</u> .<br/>';
}
if (empty($data['gender'])) {
$this->valid = FALSE;
$this->errors['gender'] = '<u>Please select a Gender.<u>';
}
return $this->valid;
}
public function getError($key) {
$error = "";
if (isset($this->errors[$key])) {
$error = $this->errors[$key];
}
return $error;
}
public function getValue($key) {
$value = "";
if (isset($this->data[$key])) {
$value = $this->data[$key];
}
return $value;
}
public function isChecked($key, $value) {
$checked = "";
if (isset($this->data[$key]) && $this->data[$key] === $value) {
$checked = ' checked="checked"';
}
return $checked;
}
public function isSelected($key, $value) {
$selected = "";
if (isset($this->data[$key]) && $this->data[$key] === $value) {
$selected = ' selected="selected"';
}
return $selected;
}
private function isValidEmail($email) {
return (filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE);
}
protected function isValidIntegerInRange($integer, $min, $max) {
$options = array(
'options' => array(
'min_range' => $min,
'max_range' => $max,
)
);
return (filter_var($integer, FILTER_VALIDATE_INT, $options) !== FALSE);
}
}
?>
dao.php
<html>
<body>
<?php
//Make connection to the database
$host = "localhost";
$username = "root";
$password = "";
$database = "my_db";
$dsn = "mysql:host=$host;dbname=$database";
TRY {
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST['submit'])) {
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$console = $_POST['console'];
$p1 = $_POST['p1'];
$p2 = $_POST['p2'];
if (isset($_POST['id'])) {
//Updates the record
$id = $_POST['id'];
$sql = "UPDATE userprofile2 SET"
. "firstName=".$conn->quote($fname)
. "lastName=".$conn->quote($lastName)
. "email=".$conn->quote($email)
. "age=".$conn->quote($age)
. "gender=".$conn->quote($gender)
. "console=".$conn->quote($console)
. "p1=".$conn->quote($p1)
. "p2=".$conn->quote($p2)
. "WHERE id = ".$conn->quote($id);
$userprofile2 = $conn->query($sql);
} else {
// Inserts new values into table
$sql = "INSERT INTO userprofile2(firstName, lastName, email, age, gender, console, p1, p2"
. " ) VALUES ("
. $conn->quote($firstName).","
. $conn->quote($lastName).","
. $conn->quote($email).","
. $conn->quote($age).","
. $conn->quote($gender).","
. $conn->quote($console).","
. $conn->quote($p1).","
. $conn->quote($p2) . ")";
$userprofile2 = $conn->query($sql);
}
} elseif (isset($_GET['ID'])) {
// edit mode, allows user to change a selected parameter in the table (Not working)
$userEditDataRows = $conn->query('SELECT * FROM userprofile2 WHERE ID ='.$conn->quote($_GET['ID']));
if (sizeof($userEditDataRows)>0) {
// $row = $userEditDataRows[0];
$firstName = $row['firstName'];
$lastName = $row['lastName'];
$email = $row['email'];
$age = $row['age'];
$gender = $row['gender'];
$console = $row['console'];
$console = $row['p1'];
$console = $row['p2'];
$ID = $_GET['ID'];
}
} else {
//Set the empty values for fields that haven't been filled in
$firstName = '';
$lastName = '';
$email = '';
$age = '';
$gender = '';
$console = '';
$p1 = '';
$p2 = '';
$ID = false;
}
//construct the table
$sql = "SELECT * FROM userprofile2";
$userprofile2 = $conn->query($sql);
$table = '<table>';
$table .= '<tr>';
$table .= '<th> ID </th>
<th> First Name </th>
<th> Last Name </th>
<th> Email Address </th>
<th> Age </th>
<th> Gender </th>
<th> Console </th>
<th> Password </th>
<th> Password (Confirmed) </th>';
$table .= '</tr>';
foreach ($userprofile2 as $userprofile2) {
$table .= ' <tr>';
$table .= ' <td>' . $userprofile2['id'] ." ". '</td>';
$table .= ' <td>' . $userprofile2['firstName'] . '</td>';
$table .= ' <td>' . $userprofile2['lastName'] . '</td>';
$table .= ' <td>' . $userprofile2['email'] . '</td>';
$table .= ' <td>' . $userprofile2['age'] . '</td>';
$table .= ' <td>' . $userprofile2['gender'] . '</td>';
$table .= ' <td>' . $userprofile2['console'] . '</td>';
$table .= ' <td>' . $userprofile2['p1'] . '</td>';
$table .= ' <td>' . $userprofile2['p2'] . '</td>';
$table .= ' </tr> ';
}
$table .= '</table>';
} catch (PDOException $e) {
exit("Connection failed: " . $e->getMessage());
//catches errors and prints them to screen
}
?>
<h2>Thank you <?php echo $_POST["firstName"]; // confirmation of a successful
//entry ?>, your details have been stored!<br /></h2>
<u><h1>Here are the contents of your database:</h1></u>
<?php echo $table ?>
</br>
Click Here to go back to the form. </br>
<html>
<body>

I think the problem is $_POST['id']. It's not defined anywhere so it doesn't get the chance to insert because of the following code block:
if (isset($_POST['id'])) {
Try using $_GET['id'] instead:
if (isset($_GET['id'])) {

Related

how to make Laser fiche quote page in php (record indert,update, delete and listing)

I have made one application for laser fiche quote page.
Make lase fiche quote page in PHP
create an Order Management System
How can I achieve:
create a new data form
quote edit data
quote delete in listing
Insert record code :
<script type="text/javascript">
function calc(control){
var row = $(control).parent().parent();
var qty = row.find("input[id^='qty']").val();
var price = row.find("input[id^='unitprice']").val();
var laspprice = row.find("input[id^='laspprice']").val();
row.find("input[id^='subtotal']").val( (qty * price) + (qty *laspprice) );
var grandTotal = 0;
$(".subtotal").each(function () {
var stval = parseFloat($(this).val());
grandTotal += isNaN(stval) ? 0 : stval;
});
$('#grdtot').val(grandTotal.toFixed(2));
}
</script>
<?php
$servername = "*****";
$username = "*****";
$password = "*****";
$dbname = "******";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, parrent_id, listitem, unitprice, lsapprice FROM avantlist";
$result = $conn->query($sql);
if ($result->num_rows > 0){
?>
<div class="lf-quote-page">
<div class="lf-title clearfix">
<div class="wrap">
<h1 class="entry-title">Laserfiche Avante Quote</h1>
</div>
</div>
<div class="lf-content">
<div class="wrap">
<div class="lf-quote-table">
<form action="http://10.0.0.16:8080/data2/userdata.php" method="post" id="form">
<table id="tblProducts" class="table">
<thead>
<tr>
<td style="width: 40%;"><b>List Item</b></td>
<td><b>Quantity</b></td>
<td><b>Unit Price</b></td>
<td><b>LSAP Price</b></td>
<td><b>Sub-Total</b></td>
<td width="150"><b>Select Sub Item</b></td>
</tr>
<tr><td colspan="6"><b>Server</b></td></tr>
</thead>
<tbody>
<input type="hidden" class="id" value="<?php $id; ?>" name="id" id="id[]" style="width:300px;"/>
<?php
$i = 1;
$j = 1;
while($row = $result->fetch_assoc()){
$id = $row['id'];
$parrent_id = $row['parrent_id'];
$listitem = $row['listitem'];
$unitprice = $row['unitprice'];
$lsapprice = $row['lsapprice'];
if($parrent_id == 0 || $parrent_id == 2 || $parrent_id == 3) {
if($row['id']!=='10' && $row['id']!=='14' && $row['id']!=='17' && $row['id']!=='20'){?>
<tr data-type="parent<?php echo $i++;?>" id="<?php echo 'row'.$id;?>">
<td><input type="text" class="listitem" value="<?php echo $listitem; ?>" name="listitem[]" id="listitem" readonly/></td>
<td><input type="text" class="qty" value="0" name="qty[]" onkeypress="return isNumberKey(event)" id="qty" onblur="calc(this)"/></td>
<td><input type="text" class="unitprice" value="<?php echo $unitprice;?>" name="unitprice[]" id="unitprice" readonly/></td>
<td><input type="text" class="laspprice" value="<?php echo $lsapprice;?>" name="laspprice[]" id="laspprice" readonly/></td>
<td><input type="text" class="subtotal" value="0" name="subtotal[]" id="subtotal" readonly/></td>
<td><?php if ($parrent_id == 0) { ?><input type="checkbox" onchange="showDetails(this)" /></td><?php } ?>
</tr>
<?php } if($row['id']=='10') {?>
<tr><td colspan="6"><b><?php echo $listitem;?></b></td></tr>
<?php } if($row['id']=='14') {?>
<tr><td colspan="6"><b><?php echo $listitem;?></b></td></tr>
<?php } if($row['id']=='17') {?>
<tr><td colspan="6"><b><?php echo $listitem;?></b></td></tr>
<?php } if($row['id']=='20') {?>
<tr><td colspan="6"><b><?php //echo $listitem;?></b></td></tr>
<?php }
}elseif($parrent_id == 0 || $parrent_id == 1) {?>
<tr data-type="child<?php echo $j++;?>" class="hide">
<td><input type="text" class="listname" value="<?php echo $listitem; ?>" name="listname[]" id="listname" readonly/></td>
<td><input type="text" class="qty" value="0" name="qty[]" onkeypress="return isNumberKey(event)" id="qty" onblur="calc(this)"/></td>
<td><input type="text" class="unitprice" value="<?php echo $unitprice;?>" name="unitprice[]" id="unitprice" readonly/></td>
<td><input type="text" class="laspprice" value="<?php echo $lsapprice;?>" name="laspprice[]" id="laspprice" readonly/></td>
<td><input type="text" class="subtotal" value="0" name="subtotal[]" id="subtotal" readonly/></td>
<td> </td>
</tr>
<?php }
}?>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td style="text-align:right;">Total System Price:</td>
<td><input type="text" class="grdtot" value="" id="grdtot" name="" readonly/></td>
</tr>
</tfoot>
</table>
<div class="quote-bottom-row clearfix">
<div class="quote-lable">
<label>Quote name</label>
<input type="text" class="formbox" name="quotename" required><br>
<input type="submit" value="Submit" name="submit" class="btn"/>
<input type="button" value="cancel" name="cancel" class="btn gray-btn"/>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php } ?>
<script type="application/javascript">
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
<script>
function showDetails(chk) {
//var animalType = animal.getAttribute("data-type");
var tr = $(chk).parent().parent();
var attr = tr.attr("data-type");
var attr1 = tr.attr("data-type");
var attr = attr.replace("parent", "child")
var t = $("#tblProducts").children('tbody').children('tr').each(function (element) {
if ($(chk).prop('checked') == true) {
if ($(this).attr("data-type") == attr) {
$(this).addClass("show");
$(this).removeClass("hide");
}
else {
if ($(this).attr("data-type").indexOf("child") != -1) {
$(this).addClass("hide");
$(this).removeClass("show");
}
if ($(this).attr("data-type").indexOf("parent") != -1) {
if ($(this).attr("data-type") != attr1) {
$(this).children().find("input").prop('checked', false);
}
}
}
}
else
{
$(this).children().find("input").prop('checked', false);
if ($(this).attr("data-type").indexOf("child") != -1) {
$(this).addClass("hide");
}
}
});
// $(tr).addClass("show");
//$(tr).removeClass("hide");
}
</script>
Edit/Delete code : [Edit functionality not working, Delete functionality working]
<script type="text/javascript">
function calc(control){
var row = $(control).parent().parent();
var qty = row.find("input[id^='qty']").val();
var price = row.find("input[id^='unitprice']").val();
var laspprice = row.find("input[id^='laspprice']").val();
row.find("input[id^='subtotal']").val( (qty * price) + (qty *laspprice) );
var grandTotal = 0;
$(".subtotal").each(function () {
var stval = parseFloat($(this).val());
grandTotal += isNaN(stval) ? 0 : stval;
});
$('#grdtot').val(grandTotal.toFixed(2));
}
</script>
<input type="hidden" name="quoteid" value=<?php echo $_GET['quoteid'];?>>
<?php
if(isset($_POST['update'])){
$servername = "****";
$username = "*****";
$password = "*****";
$dbname = "*****";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$quoteid = $_GET['quoteid'];
$sql = "DELETE FROM userdata1 WHERE quoteid='$quoteid'";
echo $sql;
if($conn->query($sql) === TRUE){
//echo "Record deleted successfully";
$servername = "******";
$username = "******";
$password = "******";
$dbname = "*****";
$conn2 = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("Connection failed: " . $conn2->connect_error);
}
$listitem = $_POST['listitem'];
$qty = $_POST['qty'];
$unitprice = $_POST['unitprice'];
$laspprice = $_POST['laspprice'];
$subtotal = $_POST['subtotal'];
$quotename = $_POST['quotename'];
function getGUID(){
if(function_exists('com_create_guid')){
return com_create_guid();
}
else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = "" // "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12)
.""; // "}"
return $uuid;
}
}
$GUID = getGUID();
$GUID = str_replace("{","",$GUID );
$GUID = str_replace("}","",$GUID );
foreach($_POST['listitem'] as $row=>$Listitem){
//$username=mysqli_real_escape_string($Listitem);
//$id=$_POST['id'][$row];
$listitem=$_POST['listitem'][$row];
$qty=$_POST['qty'][$row];
$unitprice=$_POST['unitprice'][$row];
$laspprice=$_POST['laspprice'][$row];
$subtotal=$_POST['subtotal'][$row];
$quotename=$_POST['quotename'][$row];
$sql2 = "INSERT INTO userdata1(listitem, qty, unitprice, laspprice, subtotal, quotename, quoteid)
VALUES('$listitem', '$qty', '$unitprice', '$laspprice', '$subtotal', '$quotename','$GUID')";
//print_r($sql2 .'<br/>');
$result2 = mysqli_query($sql2)or die(mysqli_error());
//print_r($result2);
}
if($result2){
echo "Quote updated successful";
echo "<meta http-equiv=\"refresh\" content=\"1;URL=http://10.0.0.16:8080/data2/index.php?file=Product_list \">";
}
}
else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
}
$servername = "******";
$username = "*****";
$password = "*****";
$dbname = "****";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$quoteid = $_GET['quoteid'];
$sql = "SELECT * FROM userdata1 WHERE quoteid='$quoteid'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
?>
<div class="lf-quote-page">
<div class="lf-title clearfix">
<div class="wrap">
<h1 class="entry-title">Laserfiche Avante Quote- Update</h1>
</div>
</div>
<div class="lf-content">
<div class="wrap">
<div class="lf-quote-table">
<form action="<?php $_PHP_SELF ?>" method="post" id="form">
<table id="tblProducts" class="table">
<thead>
<tr>
<td style="width: 40%;"><b>List Item</b></td>
<td><b>Quantity</b></td>
<td><b>Unit Price</b></td>
<td><b>LSAP Price</b></td>
<td><b>Sub-Total</b></td>
<!--<td width="150"><b>Select Sub Item</b></td>-->
</tr>
<tr><td colspan="5"><b>Server</b></td></tr>
</thead>
<input type="hidden" class="id" value="<?php $id; ?>" name="id" id="id[]" style="width:300px;"/>
<tbody>
<?php
$i = 1;
$j = 1;
while($row = $result->fetch_assoc()){
$id = $row['id'];
$listitem = $row['listitem'];
$qty = $row['qty'];
$unitprice = $row['unitprice'];
$laspprice = $row['laspprice'];
$subtotal = $row['subtotal'];
$quotename = $row['quotename'];
//$quoteid = $row['quoteid'];
?>
<tr data-type="parent<?php echo $i++;?>" id="<?php echo 'row'.$id;?>">
<td><input type="text" class="listitem" value="<?php echo $listitem; ?>" name="listitem[]" id="listitem" readonly/></td>
<td><input type="text" class="qty" value="0" name="qty[]" onkeypress="return isNumberKey(event)" id="qty" onblur="calc(this)"/></td>
<td><input type="text" class="unitprice" value="<?php echo $unitprice;?>" name="unitprice[]" id="unitprice" readonly/></td>
<td><input type="text" class="laspprice" value="<?php echo $laspprice;?>" name="laspprice[]" id="laspprice" readonly/></td>
<td><input type="text" class="subtotal" value="0" name="subtotal[]" id="subtotal" readonly/></td>
<!--<td><?php //if ($parrent_id == 0) { ?><input type="checkbox" onchange="showDetails(this)" /></td><?php //} ?>-->
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td style="text-align:right;">Total System Price:</td>
<td><input type="text" class="grdtot" value="" id="grdtot" name="" readonly/></td>
</tr>
</tfoot>
</table>
<div class="quote-bottom-row clearfix">
<div class="quote-lable">
<label>Quote name</label>
<input type="text" class="formbox" value="<?php echo $quotename; ?>" name="quotename[]" required><br>
<input type="submit" name="update" value="Update" class="btn">
<input type="button" value="cancel" name="cancel" class="btn gray-btn"/>
<input type="hidden" name="quoteid" value=<?php echo $_GET['quoteid'];?>>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php } ?>
<script type="application/javascript">
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
<script>
function showDetails(chk) {
//var animalType = animal.getAttribute("data-type");
var tr = $(chk).parent().parent();
var attr = tr.attr("data-type");
var attr1 = tr.attr("data-type");
var attr = attr.replace("parent", "child")
var t = $("#tblProducts").children('tbody').children('tr').each(function (element) {
if ($(chk).prop('checked') == true) {
if ($(this).attr("data-type") == attr) {
$(this).addClass("show");
$(this).removeClass("hide");
}
else {
if ($(this).attr("data-type").indexOf("child") != -1) {
$(this).addClass("hide");
$(this).removeClass("show");
}
if ($(this).attr("data-type").indexOf("parent") != -1) {
if ($(this).attr("data-type") != attr1) {
$(this).children().find("input").prop('checked', false);
}
}
}
}
else
{
$(this).children().find("input").prop('checked', false);
if ($(this).attr("data-type").indexOf("child") != -1) {
$(this).addClass("hide");
}
}
});
}
</script>
You can load your calc function like this:
window.onload = function()
{
calc();
};
Manage Radio button Like this:
<?php if($row['id']=='1') {?>
<input type="radio" name="rad" value="rad1" onchange="showDetails(this);" />
<?php } ?>
<?php if($row['id']=='2') {?>
<input type="radio" name="rad" value="rad3" onchange="showDetails(this);" />
<?php } ?>
<script>
if(selValue=='rad1')
{
$(".addqty1").val(1);
$("input.addqty1").attr("readOnly", true);
$("input.addqty3").attr("disabled", true);
$("input.addqty4").attr("disabled", true);
$("input.addqty5").attr("disabled", true);
$("input.addqty6").attr("disabled", true);
$("input.addqty7").attr("disabled", true);
$("input.addqty8").attr("disabled", true);
$("input.addqty9").attr("disabled", true);
}
else if(selValue=='rad2')
{
$(".addqty2").val(1);
$(".subtotal").val('');
$("input.addqty3").attr("readOnly", true);
$("input.addqty3").attr("disabled", false);
$("input.addqty4").attr("readOnly", true);
$("input.addqty1").attr("disabled", true);
$("input.addqty2").attr("disabled", true);
$("input.addqty5").attr("disabled", true);
$("input.addqty6").attr("disabled", true);
$("input.addqty7").attr("disabled", true);
$("input.addqty8").attr("disabled", true);
$("input.addqty9").attr("disabled", true);
}
</script>
Not tested, hope this help for you.

php error :Trying to get property of non-object

i'm trying the example in php (just begining to learn)
employee.php :
<?php
class Employee {
private $id;
private $name;
private $age;
private $address;
private $tax;
private $salary;
public function __construct($name, $age, $address, $tax, $salary) {
$this->name = $name;
$this->age = $age;
$this->address = $address;
$this->tax = $tax;
$this->salary = $salary;
}
public function __get($param) {
return $this->$param;
}
public function calculateSalary()
{
return $this->salary - ($this->salary * $this->tax / 100);
}
}
index :
<?php
require_once 'db.php';
require_once 'employee.php';
if (isset($_POST['submit']))
{
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$address = filter_input(INPUT_POST, 'address', FILTER_SANITIZE_STRING);
$age = filter_input(INPUT_POST, 'age', FILTER_SANITIZE_NUMBER_INT);
$salary = filter_input(INPUT_POST, 'salary', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$tax = filter_input(INPUT_POST, 'tax', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$params = array( ':name' => $name,
':address' => $address,
':age' => $age,
':salary' => $salary,
':tax' => $tax);
if(isset($_GET['action']) && $_GET['action'] == 'edit' && isset($_GET['id'])){
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$sql = 'UPDATE employees SET name = :name ,address = :address , age = :age , salary = :salary, tax =:tax WHERE id = :id';
$params[':id'] = $id;
} else {
$sql = 'INSERT INTO employees SET name = :name ,address = :address , age = :age , salary = :salary, tax =:tax ';
}
$stmt = $connection->prepare($sql);
if($stmt->execute($params) === true
)
{
$message = 'Employee ' . $name . ' saved successfully';
header('Location: /advancedphp');
exit;
} else {
$error = true;
$message = 'Error saving employee ' . $name ;
}
}
if (isset($_GET['action']) && $_GET['action'] == 'edit' && isset($_GET['id'])) {
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
if ($id > 0) {
$sql = 'SELECT * FROM employees WHERE id = :id';
$result = $connection->prepare($sql);
$founduser = $result->execute(array(':id' => $id));
if($founduser === true){
$user = $result->fetchall(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Employee', array('name', 'age', 'address', 'tax', 'salary'));
$user = array_shift($user);
}
}
}
//Reading from database back
$sql = 'SELECT * FROM employees';
$stmt = $connection->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Employee', array('name', 'age', 'address', 'tax', 'salary'));
?>
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="css/font-awesome.css">
<title>PDO by example</title>
</head>
<body>
<div class="wrapper">
<div class="empform">
<form class="appform" method="post" enctype="application/x-www-form-urlencoded">
<fieldset>
<legend>Employee Information</legend>
<?php if(isset($message)) { ?>
<p class="message <?= isset($error) ? 'error' : ' ' ?>"><?= $message ?></p>
<?php } ?>
<table>
<tr>
<td>
<label for="name">Employee Name</label>
</td>
</tr>
<tr>
<td>
<input required type="text" name="name" id="name" placeholder="Write the employee name " maxlength="50" value="<?= isset($user) ? $user->name : ' ' ?>">
</td>
</tr>
<tr>
<td>
<label for="age">Employee Age</label>
</td>
</tr>
<tr>
<td>
<input required type="number" name="age" id="age" min="22" max="60" value="<?= isset($user) ? $user->age : ' ' ?>">
</td>
</tr>
<tr>
<td>
<label for="address">Employee Address</label>
</td>
</tr>
<tr>
<td>
<input required type="text" name="address" id="address" placeholder="Write the employee address " maxlength="100" value="<?= isset($user) ? $user->address : ' ' ?>">
</td>
</tr>
<tr>
<td>
<label for="salary">Employee Salary</label>
</td>
</tr>
<tr>
<td>
/*line 105*/ <input required type="number" step="0.01" name="salary" id="salary" min="1500" max="9000" value="<?= isset($user) ? $user->salary : ' ' ?>">
</td>
</tr>
<tr>
<td>
<label for="tax">Employee Tax (%)</label>
</td>
</tr>
<tr>
<td>
<input required type="number" step="0.01" name="tax" id="tax" min="1" max="5" value="<?= isset($user) ? $user->tax : ' ' ?>">
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Save">
</td>
</tr>
</table>
</fieldset>
</form>
</div>
<div class="employees">
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Address</th>
<th>Salary</th>
<th>Tax (%)</th>
<th>Control</th>
</tr>
</thead>
<tbody>
<?php
if(false !== $result){
foreach ($result as $employee) {
?>
<tr>
<td><?= $employee->name ?></td>
<td><?= $employee->age?></td>
<td><?= $employee->address ?></td>
<td><?= round($employee->calculateSalary()) ?> L.E</td>
<td><?= $employee->tax ?></td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-times"></i>
</td>
</tr>
<?php
}
} else {
?>
<td colspan="5"><p>Sorry, no employees to list</p></td>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
the project works well .. but if i edit fields .. it updated with errors in name and address fields
"<br /><b>Notice</b>: Trying to get property of non-object in <b>C:\xampp\htdocs\PhpProject\advancedphp\index.php</b> on line <b>105</b><br />"
as shown :
I noticed that the error happen on windows but it gone on linux and it run without errors
I don't know where is the wrong ...
(Sorry i'm begginer)
I do not see where you are instantiating your class Employee
The references to $user are checked by isset() but your references to $employee are not. somewhere you must instantiate the class i.e.
$user = new Employee(stuff,here,as,required,by,constructor);
or
$employee = new Employee(stuff,here,as,required,by,constructor);

trouble in submiting form in php

I'm doing a database project for university and I'm having a problem in here.
I receive from a previous page an id as $_POST['ids'] and in the form I send that same value in a hidden field so it can do a sort of a cicle.
But when I click the submit button I got a lot of errors on $service_info and no information is loaded on the page. I tried do var_dump() everything and I just can't find what is the problem in here.
<?php
//error_reporting();
require 'core/init.php';
require 'db/connect.php';
require 'functions/security.php';
?>
<html>
<head>
<title>Make a reservation</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/common.css">
</head>
<body>
<?php require 'parts/header.php'; ?>
<hr>
<?php
$query = "SELECT * FROM service WHERE id=" . $_POST['ids'];
if ($result = $db->query($query)) {
if ($result->num_rows) {
$service_info = $result->fetch_object();
$result->close();
}
}
$query = "SELECT name FROM tour WHERE id =" . $service_info->idtour;
if ($result = $db->query($query)) {
if ($result->num_rows) {
$tour_name = $result->fetch_object();
$result->close();
}
}
$query = "SELECT SUM(nrseats) AS res_seats FROM reservation_service WHERE idservice =" . $service_info->id;
$nr_reservations_info = $db->query($query);
$nr_reservations = $nr_reservations_info->fetch_row();
$nr_reservations_info->close();
$count = $service_info->nrseats - $nr_reservations[0];
if($count === 0){
echo "<script>alert('There are no more places available for this tour. You are being redirected for the main page!')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
else{
$count = $service_info->nrseats;
}
?>
<form action="" method="POST">
<div class="registering">
<table>
<tbody>
<tr>
<td>
<label for="tname">Related tour</label>
</td>
<td>
<label for="splace"><br>Service name</label>
</td><p><br></p>
</tr>
<tr>
<td>
<input type="text" readonly="" name="tour" id="tour" required="" autofocus="" value="<?php echo $tour_name->name ?>">
</td>
<td>
<input type="text" readonly="" name="name" id="name" required="" value="<?php echo $service_info->name ?>">
</td>
</tr>
<tr>
<td>
<label for="sprice"><br>Price (€)</label>
</td>
<td>
<label for="sdescription"><br>Description</label>
</td>
</tr>
<tr>
<td>
<input type="number" name="price" id="price" readonly="" required="" value="<?php echo $service_info->price ?>">
</td>
<td>
<input type="text" name="description" id="description" required="" readonly="" value="<?php echo $service_info->description ?>">
</td>
</tr>
<tr>
<td>
<label for="sseats"><br>Seats left</label>
</td>
<td>
<label for="snreservations"><br>Number of reservations (people)</label>
</td>
</tr>
</tr>
<tr>
<td>
<input type="number" name="nrseats" id="nrseats" required="" value="<?php echo $count ?>" readonly="">
</td>
<td>
<input type="number" name="nrreservations" id="nrreservations" required="" value="1">
</td>
<td>
<input type="hidden" name="ids" required="" value="<?php $service_info->id ?>">
</td>
</tr>
</tr>
<tr>
<td colspan="2">
<label for="next"><br></label>
<input type="submit" value="Next">
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>
<?php
if (!empty($_POST)) {
if (isset($_POST['name'], $_POST['ids'], $_POST['tour'], $_POST['price'], $_POST['description'], $_POST['nrseats'], $_POST['nrreservations'])) {
$_POST = array_map("trim", $_POST);
$name = $_POST['name'];
$tour = $_POST['tour'];
$price = $_POST['price'];
$description = $_POST['description'];
$nrseats = $_POST['nrseats'];
$nrreservations = $_POST['nrreservations'];
$ids = $_POST['ids'];
if (!empty($name) && !empty($ids) && !empty($tour) && !empty($price) && !empty($description) && !empty($nrseats) && !empty($nrreservations)) {
$query = "SELECT id FROM customer WHERE email='" . $_SESSION['user_email'] . "'";
if ($result = $db->query($query)) {
$id_user = $result->fetch_object();
$result->close();
}
$query = "SELECT id FROM reservation WHERE idtour={$service_info->idtour} AND idcustomer={$id_user->id}";
if ($result = $db->query($query)) {
if ($result->num_rows) {
$id_reservation = $result->fetch_object();
$result->close();
}
}
$query = "SELECT * FROM reservation_service WHERE idservice=" . $service_info->id;
if ($result = $db->query($query)) {
if ($result->num_rows) {
$reservation_service_exists = $result->fetch_object();
if ($nrreservations < 1) {
echo "<script>alert('Your must make a reservation for, at least, one person!')</script>";
echo "<script>window.open('new_reservation_service.php','_self')</script>";
} else if ($count - $nrreservations < 0) {
echo "<script>alert('You can not make the reservation because there are only " . $count . " seats available in this tour!')</script>";
echo "<script>window.open('new_reservation_service.php','_self')</script>";
} else if ($result->num_rows) {
$query = "SELECT * FROM reservation WHERE idcustomer= '" . $id_user->id . "' AND idtour= '" . $service_info->idtour . "'";
if ($result = $db->query($query)) {
if ($result->num_rows) {
$reservation_exists = $result->fetch_object();
$result->close();
if ($reservation_exists->idcustomer === $id_user->id) {
if ($reservation_exists->id === $reservation_service_exists->idreservation) {
echo "<script>alert('You already made a reservation for this service. Please see your reservation panel!')</script>";
echo "<script>window.open('reservations.php','_self')</script>";
}
}
}
}
}
}else {
$query = "INSERT INTO reservation_service (idreservation, idservice, date, nrseats) VALUES (?, ?, NOW(), ?)";
$insert = $db->prepare($query);
$insert->bind_param('iii', $id_reservation->id, $service_info->id, $nrreservations);
$insert->execute();
echo "<script>alert('You successfully made a reservation! You are being redirected to your reservations page')</script>";
echo "<script>window.open('reservations.php','_self')</script>";
}
}
}
}
}
?>
change inside your form this input hidden you created:
<input type="hidden" name="ids" required="" value="<?php $service_info->id ?>">
to
<input type="hidden" name="ids" required="" value="<?php echo $service_info->id ?>">
If you don't echoing this value, $_POST['ids'] won't be get any value passed from form.

Populate form field with specific data row from database table for updating PHP MYSQL

What i have is an event table list that shows a list of events for a team. beside each row is an edit button that when clicked brings you to an edit page where you can edit that selected event. however when i click the button i get nothing but a blank page. iv included the connection file and the index file
Index.php
<?php
require('model/connection.php');
require('model/functions.php');
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'root_menu';
}
if ($action == 'root_menu') {
include('homePage.php');
} else if ($action == 'add_user') {
$email = $_POST['email'];
$password = $_POST['password'];
$last_name = $_POST['last_name'];
$first_name = $_POST['first_name'];
$country = $_POST['country'];
$city_town = $_POST['city_town'];
$user_type_id = $_POST['user_type_id'];
add_user($email, $password, $last_name, $first_name, $country, $city_town, $user_type_id);
$team_manager = get_users();
include('homePage.php');
} else if ($action == 'add_team') {
$name = $_POST['name'];
$sport = $_POST['sport'];
$country = $_POST['country'];
$city_town = $_POST['city_town'];
$age_profile = $_POST['age_profile'];
$user_id = $_POST['user_id'];
add_team($name, $sport, $country, $city_town, $age_profile, $user_id);
$team_manager = get_teams();
include('userPage.php');
} else if ($action == 'add_player') {
$last_name = $_POST['last_name'];
$first_name = $_POST['first_name'];
$dob = $_POST['dob'];
$position = $_POST['position'];
$email = $_POST['email'];
$country = $_POST['country'];
$city_town = $_POST['city_town'];
$password = $_POST['password'];
$team_id = $_POST['team_id'];
$user_type_id = $_POST['user_type_id'];
add_player($last_name, $first_name, $dob, $position, $email, $country, $city_town, $password, $team_id, $user_type_id);
$team_manager = get_players();
$from = "teammanager0#outlook.com"; // this is the web app's Email address
$subject = "Welcome to Team Manager";
$message = "You have been added to a team on our web app TEAM MANAGER!" . "\n\n" . "In order to login to your team please use
the following details: " . "\n\n" . "Email: " . $email . "\n\n" . "Password: " . $password;
$headers = "From:" . $from;
mail($email, $subject, $message, $headers);
header("location: http://localhost/TeamManager/teamPage.php?id=$team_id");
} else if ($action == 'add_event') {
$event_type = $_POST['event_type'];
$event_desc = $_POST['event_desc'];
$event_date = $_POST['event_date'];
$event_start = $_POST['event_start'];
$event_end = $_POST['event_end'];
$team_name = $_POST['team_name'];
$age_profile = $_POST['age_profile'];
$user_id = $_POST['user_id'];
$team_id = $_POST['team_id'];
add_event($event_type, $event_desc, $event_date, $event_start, $event_end, $team_name, $age_profile, $user_id, $team_id);
$team_manager = get_events();
header("location: http://localhost/TeamManager/teamPage.php?id=$team_id");
} else if ($action == 'delete_event') {
$event_id = $_POST['event_id'];
delete_event($event_id);
header("location: http://localhost/TeamManager/userPage.php");
} else if ($action == 'edit_event_form') {
$event_id = $_POST('event_id');
$event = get_event($event_id);
$event_type = $event['event_type'];
$event_desc = $event['event_desc'];
$event_date = $event['event_date'];
$event_start = $event['event_start'];
$event_end = $event['event_end'];
$team_name = $event['team_name'];
$age_profile = $event['age_profile'];
$user_id = $event['user_id'];
$team_id = $event['team_id'];
include('editEvent.php');
}
?>
connection.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "brendan";
$mysql_password = "admin";
$mysql_database = "team_manager";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>
eventPage.php
<?php
require_once('auth.php');
session_start();
if (trim($_SESSION['SESS_USER_TYPE']) == '2') {
header("location: playerPage.php");
exit();
}
require_once('model/connection.php');
require_once('model/deleteEvent.php');
$query = "SELECT * FROM events WHERE user_id = '" . $_SESSION['SESS_USER_ID'] . "'";
$team_manager = mysql_query($query) or die(mysql_error());
?>
<div id="sectionLeft">
<div class="eventsTable">
<h3>Events</h3>
<table>
<tr>
<td>Team Name</td>
<td>Event</td>
<td>Description</td>
<td>Date</td>
<td>Start Time</td>
<td>End Time</td>
</tr>
<?php while ($row = mysql_fetch_assoc($team_manager)) { ?>
<tr>
<td><?php echo $row['team_name']; ?> <?php echo $row['age_profile']; ?></td>
<td><?php echo $row['event_type']; ?></td>
<td><?php echo $row['event_desc']; ?></td>
<td><?php echo $row['event_date']; ?></td>
<td><?php echo $row['event_start']; ?></td>
<td><?php echo $row['event_end']; ?></td>
<td>
<form action="index.php" method="post" id="delete_event_button" name="form">
<input type="hidden" name="action" value="delete_event"/>
<input type="hidden" name="event_id"
value="<?php echo $row['event_id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
<td>
<form action="index.php" method="post" id="edit_event_button" name="form">
<input type="hidden" name="action" value="edit_event_form"/>
<input type="hidden" name="event_id"
value="<?php echo $row['event_id']; ?>" />
<input type="submit" value="Edit" />
</form>
</td>
</tr>
<?php } ?>
</table>
</div>
<br /><br />
</div>
editEvent.php
<?php
require_once('auth.php');
session_start();
if (trim($_SESSION['SESS_USER_TYPE']) == '2') {
header("location: playerPage.php");
exit();
}
require_once('model/connection.php');
require_once('model/deleteEvent.php');
$query = "SELECT * FROM events WHERE user_id = '" . $_SESSION['SESS_USER_ID'] . "'";
$team_manager = mysql_query($query) or die(mysql_error());
?>
<div id="sectionLeft">
<div class="eventsTable">
<h3>Events</h3>
<table>
<tr>
<td>Team Name</td>
<td>Event</td>
<td>Description</td>
<td>Date</td>
<td>Start Time</td>
<td>End Time</td>
</tr>
<?php while ($row = mysql_fetch_assoc($team_manager)) { ?>
<tr>
<td><?php echo $row['team_name']; ?> <?php echo $row['age_profile']; ?></td>
<td><?php echo $row['event_type']; ?></td>
<td><?php echo $row['event_desc']; ?></td>
<td><?php echo $row['event_date']; ?></td>
<td><?php echo $row['event_start']; ?></td>
<td><?php echo $row['event_end']; ?></td>
<td>
<form action="index.php" method="post" id="delete_event_button" name="form">
<input type="hidden" name="action" value="delete_event"/>
<input type="hidden" name="event_id"
value="<?php echo $row['event_id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
<td>
<form action="index.php" method="post" id="edit_event_button" name="form">
<input type="hidden" name="action" value="edit_event_form"/>
<input type="hidden" name="event_id"
value="<?php echo $row['event_id']; ?>" />
<input type="submit" value="Edit" />
</form>
</td>
</tr>
<?php } ?>
</table>
</div>
<br /><br />
</div>
get event function from functions.php
function get_event($event_id) {
global $bd;
$query = "SELECT * FROM events
WHERE event_id = '$event_id'";
$events = $bd->query($query);
$event = $events->fetch();
return $event;
}
line 77 wrong paranthesis -> $event_id = $_POST('event_id');
need square ones

My PHP Form will not refresh properly

So when I hit submit on the form that is displayed, the page (if working properly) should refresh, and the ELSE statement should be displayed instead, but I have 2 problems
The else statement is not displayed until I manually refresh the page
The Pub Score is not updated until the page is manually refreshed either, I think my code placement might be what's causing it, but I tried to put my form as far down as I could, I'm out of ideas, any help would be great thanks.
<?php
require_once('header.php');
require_once('connectdb.php');
require_once('sessioncheck.php');
if (isset($_SESSION['user_id'])) {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_DATA);
$user_name = mysqli_real_escape_string($dbc, trim($_GET['username']));
$query = "SELECT * FROM blah WHERE username = '$user_name'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
if (mysqli_num_rows($data) != 0) {
if ($row['havemic'] == 1) {
$micstatus = "Yes";
} else {
$micstatus = "No";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title><?php echo $user_name . ' profile' ?></title>
</head>
<body>
<?php
$commenduser = $user_name;
$query = "SELECT * FROM blah where commenduser = '$commenduser'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
$lowerusername = strtolower($username);
$loweruser_name = strtolower($user_name);
if (mysqli_num_rows($data) == 0) {
if (isset($_POST['submit'])) {
$commendplayer = mysqli_real_escape_string($dbc, trim($_POST['commendplayer']));
$commend = mysqli_real_escape_string($dbc, trim($_POST['commend']));
$comment = mysqli_real_escape_string($dbc, trim($_POST['comment']));
if (empty($comment)) {
echo '<p class="error">Please fillout a comment before submitting</p>';
} else {
$query = "INSERT INTO commend (commendby, commenduser, comment) VALUES ('$username', '$user_name', '$comment')";
mysqli_query($dbc, $query);
if ($commend == true) {
$query = "UPDATE blah SET points=points+1 WHERE username='$user_name'";
mysqli_query($dbc, $query);
echo '<p class="success">Your commendation has been submitted with + 1 account points.</p>';
} else {
echo '<p class="success">Your commendation has been submitted with no affect on the users account points.</p>';
}
}
}
} else {
echo '<p class="success">You have already submitted a commendation for this player.</p>';
}
?>
<div id="accsettings">
<table cellpadding="5">
<tr><td><label for="username" class="reglabel">Username: </label></td>
<td><label for="username" class="reglabel"><?php echo $row['username']; ?></label></td></tr>
<tr><td><label class="reglabel">Pub Score: </label></td><td><label class="reglabel">
/*This value 'points' should be updated to the new value after form submit */
/*As well the ELSE statement near the bottom should be displayed*/
<?php echo $row['points'] ?></label></td></tr>
<tr><td><label for="steamname" class="reglabel">Steam Name: </label></td>
<td><label for="steamname" id="acclink"><?php echo '' . $row['steamname'] . ''; ?></label>
<tr><td><label for="favchar" class="reglabel">Prefered Hero: </label></td>
<td><label for="favchar" class="reglabel"><?php echo $row['favchar']; ?></label></td></tr>
<tr><td><label for="language" class="reglabel">Spoken Language: </label></td>
<td><label for="language" class="reglabel"><?php echo $row['language']; ?></label></td></tr>
<tr><td><label for="playernote" class="reglabel">Player Note: </label></td>
<td><label for="playernote" class="reglabel"><?php echo $row['note']; ?></label></td></tr>
<tr><td><label for="micstatus" class="reglabel">Has a Mic and VO-IP?</label></td>
<td><label for="micstatus" class="reglabel"><?php echo $micstatus; ?></label></td></tr>
<tr><td colspan="2">Players Comments</td></tr>
<?php
if ($row['commendby'] != $username && $lowerusername != $loweruser_name) {
?>
<tr><td><br></td></tr>
<tr><td colspan="2"><p class="success">Player Commendations/Comments</p></td></tr>
<tr><td><br></td></tr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] . '?username=' . $user_name; ?>">
<tr><td><label for="comment">Leave a comment</label></td>
<td><input type="text" name="comment" class="regtext" /></td></td>
<tr><td colspan="2"><label for="commend" class="right">Commend Player?</label><input type="checkbox" class="right" name="commend" value="yes" /></td></tr>
<tr><td colspan="2"><input id ="submit" type="submit" class="button1" name="submit" value="Submit" /></td></tr>
</form>
<?php
} else {
/*This is what should be being displayed after the form is submitted. But it is not.*/
$query = "SELECT * FROM blah where commenduser = '$commenduser'";
$data = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($data)) {
echo '<tr><td><br></td></tr>';
echo '<tr><td><br></td></tr>';
echo '<tr><td><label class="reglabel" for="commendedbyy">Comment From: ' . $row['commendby'] . '</label></td>';
echo '<td><label class="reglabel">' . $row['comment'];
echo '<input type="hidden" name="submit" />';
echo '</form>';
}
}
?>
</table>
<?php
} else {
echo '<p class="error">' . $user_name . ' is not a registered account.</p>';
}
}
else {
echo '<p class="error">You must Log In to view this profile.</p>';
}
?>
</div>
</body>
</html>
<?php
require_once('footer.php');
?>
The $row is first grabbed from the database and then the database is updated.
You can do one of three things, the last being the simplest:
You can refactor the code to change the order
Reload the data using another query after the update
Once you update the points then update the array (i.e. do $row['points'] = $row['points'] + 1;)

Categories