retreive values from database and store it as session variable - php

I have created a form for payment in html. The values in the form are added automaticaly usng session. when the user logs in, the values are added using the user infromation. my html is below:
<table>
<form name="postForm" action="form_process.php" method="POST" >
<tr><td><input type="hidden" name="txnid" value="<?php echo $txnid=time().rand(1000,99999); ?>" /></td></tr>
<tr><td>amount</td><td><input type="text" name="amount" value="<?php echo $_SESSION['amount']; ?>" /></td></tr>
<tr><td>firstname</td><td><input type="text" name="firstname" value="<?php echo $_SESSION['firstname']; ?>" /></td></tr>
<tr><td>email</td><td><input type="text" name="email" value="<?php echo $_SESSION['email']; ?>" /></td></tr>
<tr><td>phone</td><td><input type="text" name="phone" value="<?php echo $_SESSION['phone']; ?>" /></td></tr>
<tr><td>productinfo</td><td><input type="text" name="productinfo" value="<?php echo $_SESSION['productinfo']; ?>" /></td></tr>
<tr><td><input type="hidden" name="surl" value="success.php" size="64" /></td></tr>
<tr><td><input type="hidden" name="furl" value="fail.php" size="64" /></td></tr>
<tr><td><input type="submit" /></td><td><input type="reset" /></td></tr>
</form>
</table>
in the login page, i have stored the session as below:
<?php
session_start();
include("config.php");
if(isset($_POST['login']))
{
$user_email=$_POST['email'];
$user_pass=$_POST['pass'];
$check_user="select * from users WHERE user_email='$user_email'AND user_pass='$user_pass'";
$run=mysqli_query($dbcon,$check_user);
if(mysqli_num_rows($run)>0)
{
$result=mysqli_fetch_array($run);
$_SESSION['email']=$user_email;
$_SESSION['access']=$result['access'];
//here session is used and value of $user_email store in $_SESSION.
echo "<script>window.open('index.php','_self')</script>";
}
else
{
echo "<script>alert('Email or password is incorrect!')</script>";
}
}
?>
<html>
<head lang="en">
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">
<title>Login</title>
<link rel="icon" type="image/png" href="images/imageedit_2_5125240109.gif"/>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/appointment_style.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/font-awesome.css" rel="stylesheet">
<!-- //for bootstrap working -->
<link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,300i,400,400i,600,600i,700" rel="stylesheet">
</head>
<style>
.login-panel {
margin-top: 150px;
}
</style>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Sign In</h3>
</div>
<div class="panel-body">
<form role="form" method="post" action="login.php">
<fieldset>
<div class="form-group" >
<input class="form-control" placeholder="E-mail" name="email" type="email" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="pass" type="password" value="">
</div>
<input type="submit" value="login" name="login" >
<!-- Change this to a button or input when using this as a form -->
<!-- Login -->
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
the user email is correctly inputted in the form , but the mobile number and other details are not being added into the form. i tried string them as variables like above, but its not displaying. i think i need to fetch the details from database. can anyone tell me how to do this?

I have done alot of research and finally go the answer myself.
i used the $_SESSION and the select query to get the information of the current user from the database. and used the while loop to put them in variable.
the updated code is below:
<div class="sing"><?php
session_start();
if (isset($_SESSION['email']) && $_SESSION['email'] == true) {
echo " &nbsp Welcome " . $_SESSION['email'] ;
echo "<div style='float: right;'><a href='logout.php'>Logout</a>&nbsp</div>";
} else {
}
?>
</div>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<h2>Payment Gateway Testing Sample</h2>
<h3>Fill the form and submit it for starting the transaction...</h3>
</div>
<div>
<?php
include("config.php");
$user_email=$_SESSION['email'];
$check_user="select * from users WHERE user_email='$user_email'";
$run=mysqli_query($dbcon,$check_user);
while($row = $run->fetch_assoc())
{
$user_name=$row['user_name'];
$user_mobile=$row['user_mobile'];
}
//here session is used and value of $user_email store in $_SESSION.
?>
<table>
<form name="postForm" action="form_process.php" method="POST" >
<tr><td><input type="hidden" name="txnid" value="<?php echo $txnid=time().rand(1000,99999); ?>" /></td></tr>
<tr><td>amount</td><td><input type="text" name="amount" value="" /></td></tr>
<tr><td>Name</td><td><input type="text" name="firstname" value="<?php echo $user_name; ?>" /></td></tr>
<tr><td>email</td><td><input type="text" name="email" value="<?php echo $_SESSION['email']; ?>" /></td></tr>
<tr><td>phone</td><td><input type="text" name="mobile" value="<?php echo $user_mobile; ?>" /></td></tr>
<tr><td>productinfo</td><td><input type="text" name="productinfo" value="" /></td></tr>
<tr><td><input type="hidden" name="surl" value="success.php" size="64" /></td></tr>
<tr><td><input type="hidden" name="furl" value="fail.php" size="64" /></td></tr>
<tr><td><input type="submit" /></td><td><input type="reset" /></td></tr>
</form>
</table>
</div>
</body>
</html>

Related

I can't update all the data in MySQL database with PHP program

I can't figure out why the code below only updates the folowing data when i hit the Update Record(s) button:
Language
Phone
Hotel
Type
Amount
Also all the form fields has values inside (previously saved on the database) but when i hit the Update Record(s) button (let's assume i didn't change anything this time) all the records deleted from the database and only the above mentioned records still remain intact.
Can someone please help me to figure out what i'm doing wrong?
Thanks in advance.
<?php
include 'auth.php';
require 'connect_db.php';
$id = $_REQUEST['id'];
$query = "SELECT * from csvdata where id ='".$id."'";
$result = mysqli_query($connection, $query) or die (mysqli_error($connection));
$row = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Update Record(s)</title>
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap-theme.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
</head>
<body>
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$id = $_REQUEST['id'];
$clientDepartment = $_REQUEST['clientDepartment'];
$language = $_REQUEST['language'];
$arrivalDate = $_REQUEST['arrivalDate'];
$passengerName = $_REQUEST['passengerName'];
$phone = $_REQUEST['phone'];
$ticketorHotel = $_REQUEST['ticketorHotel'];
$lastSegFlightNr = $_REQUEST['lastSegFlightNr'];
$arrivalTime = $_REQUEST['arrivalTime'];
$hotel = $_REQUEST['hotel'];
$regNr = $_REQUEST['regNr'];
$type = $_REQUEST['type'];
$amount = $_REQUEST['amount'];
$update = "update csvdata set clientDepartment='".$clientDepartment."', language='".$language."', arrivalDate='".$arrivalDate."', passengerName='".$passengerName."', phone='".$phone."', ticketorHotel='".$ticketorHotel."', lastSegFlightNr='".$lastSegFlightNr."', arrivalTime='".$arrivalTime."', hotel='".$hotel."', regNr='".$regNr."', type='".$type."', amount='".$amount."' where id='".$id."'" or die(mysqli_error($connection));
mysqli_query($connection, $update) or die (mysqli_error($connection));
$status = "<div style='text-align:center'><h3 class='form-signin-heading'>Record(s) Updated Successfully!</h3></br></br>
<a class='btn btn-lg btn-primary' href='dashboard.php'><i class='fas fa-clipboard-list'></i> Dashboard</a></div>";
echo '<div style="color:#FF0000;">'.$status.'</div>';
echo "</div>";
}else {
?>
<form class="form-signin" method="POST" action="">
<h2 class="form-signin-heading">Update Record(s)</h2>
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['id'];?>" />
<label for="inputClient" class="">Client Department</label>
<input type="text" name="client" class="form-control" placeholder="Enter Client Department" value="<?php echo $row['department'];?>">
<label for="inputLanguage" class="">Language</label>
<input type="text" name="language" class="form-control" placeholder="Enter Language" value="<?php echo $row['language'];?>">
<label for="inputDate" class="">Arrival Date</label>
<input type="text" name="date" class="form-control" placeholder=" Enter Arrival Date" value="<?php echo $row['arrivalDate'];?>">
<label for="inputName" class="">Passenger Name</label>
<input type="text" name="name" class="form-control" placeholder="Enter Passenger Name" value="<?php echo $row['passengerName'];?>">
<label for="inputPhone" class="">Phone</label>
<input type="text" name="phone" class="form-control" placeholder="Enter Phone" value="<?php echo $row['phone'];?>">
<label for="inputTicketor" class="">Ticketor/Hotel</label>
<input type="text" name="ticketor" class="form-control" placeholder="Enter Ticketor/Hotel" value="<?php echo $row['ticketorHotel'];?>">
<label for="inputFlight" class="">LastSeg/FlightNr</label>
<input type="text" name="flight" class="form-control" placeholder="Enter LastSeg FlighrNr" value="<?php echo $row['lastSegFlightNr'];?>">
<label for="inputTime" class="">Arrival Time</label>
<input type="text" name="time" class="form-control" placeholder="Enter Arrival Time" value="<?php echo $row['arrivalTime'];?>">
<label for="inputHotel" class="">Hotel</label>
<input type="text" name="hotel" class="form-control" placeholder="Enter Hotel" value="<?php echo $row['hotel'];?>">
<label for="inputReg" class="">RegNr</label>
<input type="text" name="reg" class="form-control" placeholder="Enter RegNr" value="<?php echo $row['regNr'];?>">
<label for="inputType" class="">Type</label>
<input type="text" name="type" class="form-control" placeholder="Enter Type" value="<?php echo $row['type'];?>">
<label for="inputAmount" class="">Amount</label>
<input type="text" name="amount" class="form-control" placeholder="Enter Amount" value="<?php echo $row['amount'];?>">
<p> </p>
<p><button class="btn btn-lg btn-success" style="font-size: 1.1rem;" name="submit" type="submit"><i class="fas fa-plus-circle"></i> Update Record(s)</button> <a style="font-size: 1.1rem;" class="btn btn-lg btn-warning" href="dashboard.php" role="button"><i class="fas fa-times-circle"></i> Cancel</a></p>
</form>
<?php } ?>
</body>
</html>
Check the input names.
For example RegNr: Your form contains name="reg" and you are getting the value with $_REQUEST['regNr'] instead of $_REQUEST['reg'];
Same problems with other fields.
Check this link to learn more about the basics FORM/ GET / POST
https://www.w3schools.com/php/php_forms.asp

Form Data will not parse into database

I have the following form asking for simple information, yet the data will not parse to my database. I have similar forms on other parts of my site, and they are connected to the database and retrieving data perfectly. Have pasted the code below and believe that MYSQL script is written as it is supposed to be. Why is the data not being retrieved once a use presses the submit button?
<?php
// Written by KV Ghumaan
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
?>
<?php
// Parse the form data and add inventory item to the system
if(isset($_POST['fName'])){
$id = mysql_real_escape_string($_POST['id']);
$fName = mysql_real_escape_string($_POST['fName']);
$lName = mysql_real_escape_string($_POST['lName']);
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$zipCode = mysql_real_escape_string($_POST['zipCode']);
//Add this product into the database now
$sql = mysql_query("INSERT INTO checkoutInfo (id, fName, lName, address, city, state, zipCode)
VALUES('$id','$fName','$lName','$address','$city','$state','$zipCode')") or die (mysql_error());
$pid = mysql_insert_id();
}
?>
<html>
<head>
<!--JQUERY-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="jqueryui/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="jqueryui/jquery-ui.css"/>
<link rel="stylesheet" type="text/css" href="jqueryui/jquery-ui.structure.css"/>
<link rel="stylesheet" type="text/css" href="jqueryui/jquery-ui.theme.css"/>
<!--GOOGLE FONT & EXTERNAL LIBS-->
<link href="https://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="style/style.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CHECKOUT</title>
</head>
<body>
<?php include_once("template_header.php");?>
<table width="100%" height="76vh" border="10%" cellspacing="10" style="padding: 2vh 10vw 0vh 10vw; border-style:none;">
<tr height="75%" cellspacing="0" style="border-style:none;">
<td width="30%" style="border-style:none; padding-top: 10vh;">
<form class="form" action="confirmation.php" enctype="multipart/form-data" name="checkoutForm" id="checkoutForm" method="POST">
<div id="billingQuestions">
<h3><strong>BILLING ADDRESS:</strong></h3>
<div id="formQuestions">
<label>First Name:*</label><br />
<input type="text" name="fName" size="40vw" required>
</div>
<div id="formQuestions">
<label>Last Name:*</label><br />
<input type="text" name="lName" size="40vw" required>
</div>
<div id="formQuestions">
<label>Address 1:*</label><br />
<input type="text" name="address" size="40vw" required>
</div>
<div id="formQuestions">
<label>Address 2:*</label><br />
<input type="text" size="40vw">
</div>
<div id="formQuestions">
<label>City: *</label><br />
<input name="city" type="text" size="40vw" required>
</div>
<div id="formQuestions">
<label>State: *</label><br />
<input name="state" type="text" size="40vw" required>
</div>
<div id="formQuestions">
<label>Zip Code: *</label><br />
<input name="zipCode" type="text" size="40vw" required>
</div>
<input type="submit" value="Submit"/>
</div>
</form>
</td>
</tr>
</table>
<?php include_once("template_footer.php");?>
</body>
</html>

PHP Stopping a person pressing Submit multiple times

I have the below form, and after the form the PHP requests a Curl which can take a while. Because of this they keep pressing the submit button thinking that nothing has happened. This causes multiple entries in the database. Could someone help me and let me know what to put in to stop this.
I tried the Javascript solution in the first answer but it stopped the PHP script that followed. Not what I wanted. Sorry
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="css2/style.css" rel='stylesheet' type='text/css' />
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<!--webfonts-->
<link href='//fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800' rel='stylesheet' type='text.css'/>
<!--//webfonts-->
<center><img class="displayed" src="images/logo.jpg" alt="Logo" style="width:128px;height:128px;"></center>
</head>
<body>
<div class="main">
<form action="" method="POST" enctype="multipart/form-data" id="contactform" >
<input type="hidden" name="action" value="submit">
<h1><span>Sign Up</span> <lable> The Brook on Sneydes</lable> </h1>
<div class="inset">
<p>
<center><label ><?php echo $text; ?></label></center>
<label for="first">First Name</label>
<input type="text" name="first" id="first" value="" placeholder="" required/>
</p>
<p>
<label for="last">Surname</label>
<input type="text" name="last" id="last" value="" placeholder="" required/>
</p>
<p>
<label for="mobile">Mobile Number</label>
<input type="text" name="mobile" id="mobile" value="" placeholder="" required/>
</p>
<p>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" value="" placeholder="" required/>
</p>
</div>
<p class="p-container">
<input type="submit" name="submit" id="submit" value="Submit">
</p>
</form>
</div>
<!-----start-copyright---->
<div class="copy-right">
<p> © 2016 Spearhead Digital. All rights reserved.</p>
</div>
<!-----//end-copyright---->
</body>
</html>
<?php
if(isset($_POST['submit']) && !empty($_POST) ){
$first = $_POST['first'];
$last = $_POST['last'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
// Other Code
}
You can disable the submit button when submitting the form.
An example using jQuery:
<script type="text/javascript">
$(document).ready(function() {
$('#contactform').submit(function() {
$('input[type=submit]', this).prop("disabled", true);
});
});
</script>

Updating a phpmyadmin table through Php using a url varible

im having a problem when trying to implement a edit page in php.
Getting items from the database doesent pose any problem, yet trying to update it tells me that i have missed a field and trying to skip the check just leaves me with a unedited table.
To explain more, there is a page that directs to this one while passing on a id (propertyID) veriable and from that we use it to grab the desired variables.
Can anyone look at the code and tell me what i have done wrong, i have prevously got a similar page working and am getting quite flustered trying to figure out why this one isent.
<?php
// Start sessions
include('includes/security.inc.php');
authorise();
if (!isset($_GET['propertyID']) || !is_numeric($_GET['propertyID']))
{
header('Location:./houselist.php');
}
else
{
// Include connection file
include('includes/connection.inc.php');
// Get details
connect();
$propertyID = $_GET['propertyID'];
$sql = "SELECT * FROM Properties WHERE propertyID='$propertyID' LIMIT 1";
$result = #mysql_query($sql) or die('Unable to run query');
$record = mysql_fetch_object($result);
mysql_close();
// Check to see if the form has been submitted
if (isset($_POST['submit']))
{
// Check to see all fields have been completed
$address = $_POST['address'];
$categoryID = $_POST['categoryID'];
$price = $_POST['price'];
$landlordName = $_POST['landlordName'];
$img = $_POST['img'];
$description= $_POST['description'];
if (!empty($address) && !empty($categoryID) && !empty($price) && !empty($landlordName) && !empty($img) && !empty($description))
{
// Create an SQL query to add the comment
$sql = "UPDATE property SET propertyID = '$propertyID', img = '$img', address = '$address', price = '$price', landlordName = '$landlordName', description = '$description' WHERE propertyID = $propertyID";
// Connect to the database
connect();
// Run the query and store the result in a variable
$result = mysql_query($sql) or die("Could not run query1");
// Close connection to the database
mysql_close();
// Check if query was successful
if ($result)
{
$message = '<div class="success"><p>You have successfully edited Article details.</p><p>Please Click Here to view the Animal list.</p></div>';
}
else
{
$message = '<div class="error"><p>There was an error editing details, please try again</p></div>';
}
}
else
{
$message = '<div class="error"><p>Please make sure you fill all fields in before submitting the form.</p></div>';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/960.css"/>
<link rel="stylesheet" type="text/css" href="css/demo.css"/>
<link rel="stylesheet" type="text/css" href="css/960_24_col.css"/>
<link rel="stylesheet" type="text/css" href="css/reset.css"/>
<link rel="stylesheet" type="text/css" href="css/text.css"/>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<title>Complete Property Solutions</title>
<title>Homescreen - Complete Property Solutions</title>
</head>
<body>
<div class="container_24" id="container">
<div class="grid_24" id="banner">
<img src="img/banner.png" width="960" height="92" />
</div>
<div class="grid_18" id="nav" align="right">
<ul id="topnav">
<li>Home</li>
<li>Properties</li>
<li>Landlords</li>
<li>Tenants</li>
<li>Logout</li>
</ul>
</div>
<div class="grid_6" id="search" align="right">
<form action="search.php" method="GET">
<input type="text" name="term" size="15">
<input type="submit" value="Search">
</form>
</div>
</div>
<div class="container_24" id="container" align="center">
<div id="container">
<form id="PropertyEdit" name="PropertyEdit" method="post" action="<? echo $_SERVER['PHP_SELF'] . "?propertyID=" . $propertyID; ?>">
<input type="hidden" name="propertyID" id="propertyID" value="<?php echo $propertyID; ?>" />
<?php
if (isset($message))
{
echo $message;
}
else
{
?>
<div class="label"><label for="propertyID"></label></div>
<div class="input"><input type="hidden" name="propertyID" id="propertyID" tabindex="1" value="<? echo $record->propertyID; ?>" /></div>
<br />
<div class="label"><label for="categoryID">Category</label></div>
<div class="input"><input type="text" name="categoryID" id="categoryID" tabindex="1" value="<? echo $record->categoryID; ?>" /></div>
<br />
<div class="label">
<label for="address">Address:</label></div>
<div class="input"><input type="text" name="address" id="address" tabindex="1" value="<? echo $record->address; ?>" /></div>
<br />
<div class="label"><label for="price">Price:</label></div>
<div class="input"><input type="text" name="Price" id="price" tabindex="3" value="<? echo $record->price; ?>" /></div>
<div class="label"><label for="landlordName">Landlord</label></div>
<div class="input"><input type="text" name="landlordName" id="landlordName" tabindex="1" value="<? echo $record->landlordName; ?>" /></div>
<br />
<div class="label"><label for="img">Image</label></div>
<div class="input"><input type="text" name="img" id="img" tabindex="1" value="<? echo $record->img; ?>" /></div>
<br />
<div class="label"><label for="description">Description:</label></div>
<div class="input"><textarea name="description" id="description" cols="50" rows="10" tabindex="5"><? echo $record->description; ?></textarea></div>
<br />
<div class="label"> </div>
<div class="input">
<input type="reset" name="reset" id="reset" value="Reset" tabindex="6" />
<input type="submit" name="submit" id="submit" value="Submit" tabindex="7" />
</div>
<p class="normal">Click Here to Return to the Home page</p>
<?php
}
?>
</form> </div>
</div>
</body>
</html>
<?php
}
?>
my best guess for the problem would revolve around
// Check to see if the form has been submitted
if (isset($_POST['submit']))
{
// Check to see all fields have been completed
$address = $_POST['address'];
$categoryID = $_POST['categoryID'];
$price = $_POST['price'];
$landlordName = $_POST['landlordName'];
$img = $_POST['img'];
$description= $_POST['description'];
if (!empty($address) && !empty($categoryID) && !empty($price) && !empty($landlordName) && !empty($img) && !empty($description))
{
// Create an SQL query to add the comment
$sql = "UPDATE property SET propertyID = '$propertyID', img = '$img', address = '$address', price = '$price', landlordName = '$landlordName', description = '$description' WHERE propertyID = $propertyID";
// Connect to the database
connect();
// Run the query and store the result in a variable
$result = mysql_query($sql) or die("Could not run query1");
// Close connection to the database
mysql_close();
// Check if query was successful
if ($result)
{
$message = '<div class="success"><p>You have successfully edited Article details.</p><p>Please Click Here to view the Animal list.</p></div>';
}
else
{
$message = '<div class="error"><p>There was an error editing details, please try again</p></div>';
}
}
else
{
$message = '<div class="error"><p>Please make sure you fill all fields in before submitting the form.</p></div>';
}
}
And the below section, my problem is that im not sure where exactly
<div class="container_24" id="container" align="center">
<div id="container">
<form id="PropertyEdit" name="PropertyEdit" method="post" action="<? echo $_SERVER['PHP_SELF'] . "?propertyID=" . $propertyID; ?>">
<input type="hidden" name="propertyID" id="propertyID" value="<?php echo $propertyID; ?>" />
<?php
if (isset($message))
{
echo $message;
}
else
{
?>
<div class="label"><label for="propertyID"></label></div>
<div class="input"><input type="hidden" name="propertyID" id="propertyID" tabindex="1" value="<? echo $record->propertyID; ?>" /></div>
<br />
<div class="label"><label for="categoryID">Category</label></div>
<div class="input"><input type="text" name="categoryID" id="categoryID" tabindex="1" value="<? echo $record->categoryID; ?>" /></div>
<br />
<div class="label">
<label for="address">Address:</label></div>
<div class="input"><input type="text" name="address" id="address" tabindex="1" value="<? echo $record->address; ?>" /></div>
<br />
<div class="label"><label for="price">Price:</label></div>
<div class="input"><input type="text" name="Price" id="price" tabindex="3" value="<? echo $record->price; ?>" /></div>
<div class="label"><label for="landlordName">Landlord</label></div>
<div class="input"><input type="text" name="landlordName" id="landlordName" tabindex="1" value="<? echo $record->landlordName; ?>" /></div>
<br />
<div class="label"><label for="img">Image</label></div>
<div class="input"><input type="text" name="img" id="img" tabindex="1" value="<? echo $record->img; ?>" /></div>
<br />
<div class="label"><label for="description">Description:</label></div>
<div class="input"><textarea name="description" id="description" cols="50" rows="10" tabindex="5"><? echo $record->description; ?></textarea></div>
<br />
<div class="label"> </div>
<div class="input">
<input type="reset" name="reset" id="reset" value="Reset" tabindex="6" />
<input type="submit" name="submit" id="submit" value="Submit" tabindex="7" />

Working with HTML forms and php [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm making a calculator-like web application that asks for user names, then displays their names on the next page and adds their score. I have three problems:
The user names won't display on the second page
It won't add the score
Whenever you enter a score for player 1, player 2's score is reset to zero, and vise-versia
You can access the page at: ripdvd.x10.mx/index.php
Thanks in advance!
First page:
<!DOCTYPE html>
<html>
<head>
<title>Select Players</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action="scoregen.php">
<p class="par">
<label for="player1">Please type in the one of the players first name:</label>
<input type="text" id="player1" name="player1" size="17" maxlength="17" class="textbox" />
</p>
<p class="par">
<label for="player2">Please type in another players first name:</label>
<input type="text" id="player2" name="player2" size="17" maxlength="17" class="textbox" />
</p>
<input type="submit" class="button" name="button" value="Start Playing!" />
</form>
</body>
</html>
Second page(displays scores and provides a way to update them)
<?php
// Get data from HTML form.
//Gets the player names
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
$addScore1 = $_POST['addScore1'];
$addScore2 = $_POST['addScore2'];
$oldScore1 = $_POST['oldScore1'];
$oldScore2 = $_POST['oldScore2'];
$curr1=$_COOKIE["score1"]+$addScore1;
$curr2=$_COOKIE["score2"]+$addScore2;
setcookie("score1", $_COOKIE["score1"]+$addScore1, time()+3600);
setcookie("score2", $_COOKIE["score2"]+$addScore2, time()+3600);
//Reset cookies if reset button is 't', which makes it clear scores
if ($clse = t){
setcookie ("score1", "", time() - 3600);
setcookie ("score2", "", time() - 3600);
}
// Generate HTML form
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Score Add</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action=" ">
<p class="par">
<label for="addScore1">Enter your score, <?php echo $player1; ?>:</label>
<input type="text" name="addScore1" id="addScore1" class="textbox" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $oldscore1; ?>" />
<input type="submit" class="button" value="Add Score!" />
</p>
<p class="par">
<label for="addScore2">Enter your score, <?php echo $player2; ?>:</label>
<input type="text" name="addScore2" id="addScore2" class="textbox"/>
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" />
<input type="submit" class="button" value="Add Score!"/>
</form>
</p>
<form method="post" action=" ">
<input type="hidden" name="clsc" id="clsc" value="t" />
<input type="submit" class="reset" value="Clear Scores" />
</form>
<!--Shows player and score-->
<p class="par"><?php echo $player1;?>:<?php echo $curr1?></p>
<p class="par"><?php echo $player2;?>:<?php echo $curr2?></p>
</body>
</html>
I made several changes to your code (including the removal of cookies - hope that's ok). If you can live without cookies, try this:
<?php
// Get data from HTML form.
// Gets the player names
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
if ($_POST['addScore1'] == null)
$addScore1 = 0;
else
$addScore1 = $_POST['addScore1'];
if ($_POST['addScore2'] == null)
$addScore2 = 0;
else
$addScore2 = $_POST['addScore2'];
if ($_POST['oldScore1'] == null)
$oldScore1 = 0;
else
$oldScore1 = $_POST['oldScore1'];
if ($_POST['oldScore2'] == null)
$oldScore1 = 0;
else
$oldScore2 = $_POST['oldScore2'];
$curr1=$oldScore1+$addScore1;
$curr2=$oldScore2+$addScore2;
if ($_POST['clse'] == "t"){
$curr1 = 0;
$curr2 = 0;
}
// Generate HTML form
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Score Add</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p class="par">
<label for="addScore1">Enter your score, <?php echo $player1; ?>:</label>
<input type="text" name="addScore1" id="addScore1" class="textbox" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $curr1; ?>" />
<input type="submit" class="button" value="Add Score!" />
</p>
<p class="par">
<label for="addScore2">Enter your score, <?php echo $player2; ?>:</label>
<input type="text" name="addScore2" id="addScore2" class="textbox"/>
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $curr2; ?>" />
<input type="submit" class="button" value="Add Score!"/>
</p>
<input type="hidden" name="player1" value="<?PHP echo $player1;?>" />
<input type="hidden" name="player2" value="<?PHP echo $player2;?>" />
</form>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="hidden" name="clsc" id="clsc" value="t" />
<input type="submit" class="reset" value="Clear Scores" />
</form>
<!--Shows player and score-->
<p class="par"><?php echo $player1;?>:<?php echo $curr1?></p>
<p class="par"><?php echo $player2;?>:<?php echo $curr2?></p>
</body>
</html>
Note, I haven't actually ran this, but try it and let me know if you get any errors or still have problems.
Try this one:
index.php
<!DOCTYPE html>
<html>
<head>
<title>Select Players</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action="scoregen.php">
<p class="par">
<label for="player1">Please type in the one of the players first name:</label>
<input type="text" id="player1" name="player1" size="17" maxlength="17" class="textbox" />
</p>
<p class="par">
<label for="player2">Please type in another players first name:</label>
<input type="text" id="player2" name="player2" size="17" maxlength="17" class="textbox" />
</p>
<input type="submit" class="button" name="names" value="Start Playing!" />
</form>
</body>
</html>
scoregen.php
<?php
// Get data from HTML form.
//Gets the player names
if(isset($_POST['player1'])){
$player1 = $_POST['player1'];
}else{
$player1 = "No Player";
}
if(isset($_POST['player2'])){
$player2 = $_POST['player2'];
}else{
$player2 = "No Player";
}
//CHECKS IF THE ADDSCORE BUTTON IS HIT
if(isset($_POST['AddScore'])){
$oldScore1 = $_POST['oldScore1'];
$oldScore2 = $_POST['oldScore2'];
if(!empty($_POST['addScore1'])){
$addScore1 = $_POST['addScore1'];
}else{
$addScore1 = 0;
}
if(!empty($_POST['addScore2'])){
$addScore2 = $_POST['addScore2'];
}else{
$addScore2 = 0;
}
}else{//INITIALIZES THE SCORES TO ZERO; OR SETS IT TO ZERO IF THE CLEAR SCORES BUTTON IS HIT
$oldScore1 = 0;
$oldScore2 = 0;
$addScore1 = 0;
$addScore2 = 0;
}
$curr1 = $oldScore1 + $addScore1;
$curr2 = $oldScore2 + $addScore2;
// Generate HTML form
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Score Add</title>
<link rel=StyleSheet href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post" action=" ">
<p class="par">
<label for="addScore1">Enter your score, <?php echo $player1; ?>:</label>
<input type="text" name="addScore1" id="addScore1" class="textbox" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $curr1; ?>" />
<input type="submit" name="AddScore" class="button" value="Add Score!" />
</p>
<p class="par">
<label for="addScore2">Enter your score, <?php echo $player2; ?>:</label>
<input type="text" name="addScore2" id="addScore2" class="textbox"/>
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $curr2; ?>" />
<input type="submit" name="AddScore" class="button" value="Add Score!"/>
</p>
<input type="hidden" name="player1" value="<?php echo $player1;?>"/>
<input type="hidden" name="player2" value="<?php echo $player2;?>"/>
<p class="par">
<input type="submit" class="reset" value="Clear Scores" />
</p>
</form>
<!--Shows player and score-->
<p class="par"><?php echo $player1;?>:<?php echo $curr1?></p>
<p class="par"><?php echo $player2;?>:<?php echo $curr2?></p>
</body>
</html>
1) I did not use cookies here, you don't actually need it for the scores, you can use to save the usernames though.
2) You can actually just have one addScore button, but in this code it doesn't matter. it add the scores anyway.
3) I always check if a an element of the POST is set or if it has a value or not. So i put some checking for the scores and the names.
4) In this line of your code:
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" />
i changed the $oldscore to $curr, because the $curr score would be needed in the next submit as the oldscore so the score would add up.
$oldScore1 does not equal $oldscore1
and in any case you need to keep the new values to post to the next page
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $curr1; ?>" />
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $curr2; ?>" />
Drop the cookies and just use
$curr1=$oldScore1+$addScore1;
$curr2=$oldScore2+$addScore2;
It's also worth checking if the posts are set to avoid warnings, and making sure they are integers, e.g.
$oldScore1 = isset($_POST['oldScore1'])? (int)$_POST['oldScore1'] : 0;
$oldScore2 = isset($_POST['oldScore2'])? (int)$_POST['oldScore2'] : 0;

Categories