Incorporate PHP Into HTML Form - php

I wonder whether someone may be able to help me please.
I'm trying to put together functionality which allows an administrator to search for user details in a mySQL database using the email address as the search criteria. Once the search has taken place I would like the 'first' and 'surname' fields in my form to be completed with the record retrieved.
I think the search is working as the form refreshes but it doesn't retrieve any visible information.
I just wondered whether it would be at all possbible please that someone could take a look at my code below and let me know where I've gone wrong.
Many thanks
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<?php
require("phpfile.php");
// Opens a connection to a MySQL server
$connection=mysql_connect ("hostname", $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$email = $_POST['email'];
$result = mysql_query("SELECT * FROM userdetails WHERE emailaddress like '%$email%'");
?>
<title>Map!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/gen_validatorv4.js" type="text/javascript"></script>
</head>
<h1> </h1>
<form name="userpasswordreset" id="userpasswordreset" method="post">
<div class="container">
<p align="justify"> </p>
<div class="title1">
<h2>User Details </h2>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="26%" height="25"><strong>Email Address</strong></td>
<td width="4%"> </td>
<td width="70%"><input name="email" type="email" id="email" size="50" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm Email Address </strong></td>
<td> </td>
<td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
</tr>
<tr>
<td height="25"><label>
<input type="submit" name="Submit" value="Search" />
</label></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" value="<?php echo $row['forename']; ?>" /> </td>
</tr>
<tr>
<td height="25"><strong>Last Name </strong></td>
<td> </td>
<td><input name="lname" type="text" id="lname" size="30" value="<?php echo $row['surname']; ?>" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>Password</strong></td>
<td> </td>
<td><input name="pass" type="password" id="pass" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm Password </strong></td>
<td> </td>
<td><input name="conf_pass" type="password" id="conf_pass" size="30" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>Password Hint </strong></td>
<td> </td>
<td><input name="hint" type="text" id="hint" size="30" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</form>
<script language="JavaScript" type="text/javascript">
// Code for validating the form
var frmvalidator = new Validator("userpasswordreset");
frmvalidator.addValidation("email","req","Please provide your email address");
frmvalidator.addValidation("email","email","Please enter a valid email address");
frmvalidator.addValidation("conf_email","eqelmnt=email", "The confirmed email address is not the same as the email address");
</script>
</div>
</body>
</html>
UPDATED CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<?php
require("phpfile.php");
// Opens a connection to a MySQL server
$connection=mysql_connect ("hostname", $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$email = mysql_real_escape_string($_POST['email']); // make the value safe for in the query
$result = mysql_query("SELECT * FROM userdetails WHERE emailaddress like '%$email%'");
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
// $rows is now a multi dimensional array with values found by the query
?>
<title>Map</title>
<script src="js/gen_validatorv4.js" type="text/javascript"></script>
<h1> </h1>
<form name="userpasswordreset" id="userpasswordreset" method="post">
<p align="justify"> </p>
<div class="title1">
<h2>Your Details </h2>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="26%" height="25"><strong>Email Address </strong></td>
<td width="4%"> </td>
<td width="70%"><input name="email" type="email" id="email" size="50" /></td>
</tr>
<tr>
<td height="25"><strong> Confirm Email Address </strong></td>
<td> </td>
<td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
</tr>
<tr>
<td height="25"><label>
<input type="submit" name="Submit" value="Search" />
</label></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Last Name</strong></td>
<td> </td>
<td><input name="lname" type="text" id="lname" size="30" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>Password</strong></td>
<td> </td>
<td><input name="pass" type="password" id="pass" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm Password </strong></td>
<td> </td>
<td><input name="conf_pass" type="password" id="conf_pass" size="30" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>Password Hint </strong></td>
<td> </td>
<td><input name="hint" type="text" id="hint" size="30" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</form>
<script language="JavaScript" type="text/javascript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator = new Validator("userpasswordreset");
frmvalidator.addValidation("email","req","Please provide your email address");
frmvalidator.addValidation("email","email","Please enter a valid email address");
frmvalidator.addValidation("conf_email","eqelmnt=email", "The confirmed email address is not the same as the email address");
</script>
</body>
</html>

Aren't you missing the step where you actually run the mysql_fetch_assoc function against your query? Just assigning the result of mysql_query to the variable $result is not enough!
Add something like this after your call to mysql_query:
if($result)
{
$row = mysql_fetch_assoc($result));
}
Edit: mysql_fetch_assoc PHP documentation

To get all the rows resulted with the query you would need to use a mysql fetch function (there are several).
The preffered method is definitly mysql_fetch_assoc
The relevant part of your code would look something like this:
$email = mysql_real_escape_string($_POST['email']); // make the value safe for in the query
$result = mysql_query("SELECT * FROM userdetails WHERE emailaddress like '%$email%'");
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
// $rows is now a multi dimensional array with values found by the query

Just going over your code breifly, I dont think your passing the value of the email to in your sql query correctly.
try using this.
$result = mysql_query("SELECT * FROM userdetails WHERE emailaddress like '%" . $email . "%'");

Related

MySQL Insert query returning false

This is probably the most asked question here. I have made a simple user registration form. The values are not inserted into the database. I used echo and the query is returning false. The form was working well before but then i separated the name into first and last name and dropped the previous table and made a new one. Here is the registration page code:
<!DOCTYPE html>
<?php
session_start();
include('includes/connect.php');
?>
<html>
<head>
<?php
if(isset($_POST['register_ok'])){
global $con;
$user_first_name = $_POST['user_first_name'];
$user_last_name = $_POST['user_last_name'];
$email = $_POST['email'];
$password = $_POST['password'];
echo "<script type='text/javascript'>alert(\"$user_first_name\");</script>"; //This echo is returning username successfully!
$query = "insert into user(user_first_name, user_last_name, user_email, password) values('$user_first_name', '$user_last_name', '$email', '$password')";
if(mysqli_query($con, $query)){
$_SESSION['user'] = 'user';
$_SESSION['user_first_name'] = $user_first_name;
echo "header('Location:index.php')";
} else {
echo "<script type='text/javascript'>alert(\"Values not inserted\");</script>"; //This is running which means query is not successfull.
}
} else {
echo "<script type='text/javascript'>alert(\"Page didn't receive post values\");</script>";
}
?>
<link rel="stylesheet" type="text/css" href="styles/register_style.css">
<title>New User Registration</title>
</head>
<body>
<div class="wrapper">
<header></header>
<div class="form_div">
<div class="form">
<form id="register_form" method="post" action="" autocomplete="autocomplete">
<table>
<tr>
<td id="label">First Name: </td>
<td id="input"><input type="text" name="user_first_name" required="required" id="input_box"></td>
</tr>
<tr>
<td id="label">Last Name: </td>
<td id="input"><input type="text" name="user_last_name" required="required" id="input_box"></td>
</tr>
<tr>
<td id="label">Email: </td>
<td id="input"><input type="text" name="email" required="required" id="input_box"></td>
</tr>
<tr>
<td id="label">Password: </td>
<td id="input"><input type="password" name="password" id="input_box"></td>
</tr>
<tr>
<td id="label">Confirm Password: </td>
<td id="input"><input type="password" name="confirm_password" id="input_box"></td>
</tr>
<tr id="button_row">
<td colspan="2"><input type="reset" value="Reset" id="button">
<input type="submit" value="Register" id="button" name="register_ok"></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
And this is the table :
The table is empty and the first alert is returning user first name and the second alert runs when the query returns false. It is returning false.
I think it may be a typo but cannot narrow it down. Any help would be welcome.
Change your column types from int to varchar. I'm talking about string columns (names and email). Mysql has an option to check for the data type you are trying to insert and fail if they don`t match.
Firstly,
Change column name from INT to VARCHAR using this query.
"ALTER TABLE `user`
MODIFY COLUMN `user_first_name` VARCHAR(225),
MODIFY COLUMN `user_last_name` VARCHAR(225),
MODIFY COLUMN `user_email` VARCHAR(225),
MODIFY COLUMN `password` VARCHAR(225);";
Secondly,
You kept id for both input & <td> as same in each and every row. ID can't be same.
Change it to.
<table>
<tr>
<td id="label1">First Name: </td>
<td id="input1"><input type="text" name="user_first_name" required="required" id="input_box1"></td>
</tr>
<tr>
<td id="label2">Last Name: </td>
<td id="input2"><input type="text" name="user_last_name" required="required" id="input_box2"></td>
</tr>
<tr>
<td id="label3">Email: </td>
<td id="input3"><input type="text" name="email" required="required" id="input_box3"></td>
</tr>
<tr>
<td id="label4">Password: </td>
<td id="input4"><input type="password" name="password" id="input_box4"></td>
</tr>
<tr>
<td id="label5">Confirm Password: </td>
<td id="input5"><input type="password" name="confirm_password" id="input_box5"></td>
</tr>
<tr id="button_row">
<td colspan="2"><input type="reset" value="Reset" id="button">
<input type="submit" value="Register" id="button" name="register_ok"></td>
</tr>
</table>
You need to learn about sql injections and also about securing your passwords, use mysqli prepared statements / PDO what ever you find easy to learn
<!DOCTYPE html>
<?php
session_start();
include('includes/connect.php');
?>
<html>
<head>
<?php
if(isset($_POST['register_ok'])){
$user_first_name = $_POST['user_first_name'];
$user_last_name = $_POST['user_last_name'];
$email = $_POST['email'];
$password = $_POST['password'];
// Lets encrypt the password;
$hash = pasword_hash($password,PASSWORD_DEFAULT);
// lets insert then
$query = $con->prepare("INSERT INTO user (user_id,user_first_name, user_last_name, user_email, password) VALUES(?,?,?,?)");
$query->bind_param("ssss", $user_first_name, $user_last_name, $email,$hash);
$query->execute();
if ($query->execute()) {
echo "<script type='text/javascript'>alert(\"$user_first_name\");</script>"; //This echo is returning username successfully!
} else {
echo "<script type='text/javascript'>alert(\"Values not inserted\");</script>"; //This is running which means query is not successfull.
}
}
?>
<link rel="stylesheet" type="text/css" href="styles/register_style.css">
<title>New User Registration</title>
</head>
<body>
<div class="wrapper">
<header></header>
<div class="form_div">
<div class="form">
<form id="register_form" method="post" action="" autocomplete="autocomplete">
<table>
<tr>
<td id="label">First Name: </td>
<td id="input"><input type="text" name="user_first_name" required="required" id="input_box"></td>
</tr>
<tr>
<td id="label">Last Name: </td>
<td id="input"><input type="text" name="user_last_name" required="required" id="input_box"></td>
</tr>
<tr>
<td id="label">Email: </td>
<td id="input"><input type="text" name="email" required="required" id="input_box"></td>
</tr>
<tr>
<td id="label">Password: </td>
<td id="input"><input type="password" name="password" id="input_box"></td>
</tr>
<tr>
<td id="label">Confirm Password: </td>
<td id="input"><input type="password" name="confirm_password" id="input_box"></td>
</tr>
<tr id="button_row">
<td colspan="2"><input type="reset" value="Reset" id="button">
<input type="submit" value="Register" id="button" name="register_ok"></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
NB : You need to check if the username does not exist before inserting it.
In my case, the strict mode setting was causing the issue. It requires values for all the columns to be specified in the INSERT command. Everything worked after the strict mode setting was disabled.
Check for strict mode (this should be blank):
SHOW VARIABLES LIKE 'sql_mode';
Turn off strict mode:
SET GLOBAL sql_mode = '';
Turn on strict mode:
SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES';
Credit: this post

How to update a whole document in MongoDB using PHP?

I want to update my older entries through PHP interface in MongoDB.
First I get data from the text fields and then store that into variables and then using those variables to update data in Mongodb here is my code please help me i tried all ways but every time disappointment.
<?php
if(isset($_REQUEST['btn']))
{
$a=$_REQUEST['textfield'];
$b=$_REQUEST['textfield2'];
$c=$_REQUEST['textfield3'];
$d=$_REQUEST['textfield4'];
$e=$_REQUEST['textfield5'];
$f=$_REQUEST['textfield6'];
$g=$_REQUEST['textfield7'];
$h=$_REQUEST['textfield8'];
$m = new MongoClient(); // connect to mongodb
$db = $m->app; // select a database named app
$collection = $db->lafaz;
$db->lafaz->update(array("_id"=> new MongoID($a)),$doct, array('multiple' => true));
header('Location:page.php');
}?>
<!doctype html>
<html>
<head>
<style type="text/css">
header a {
font-weight: bold;
font-family: Sarina;
font-size: 45pt;
font-style: oblique;
text-decoration: none;
text-shadow: 2px 2px #33CCFF;
color: #369;
}
</style>
<link href="form.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
<title>Edit Word</title>
</head>
<body>
<div>
<header align="center">Tarmeem -e- Haraf </header>
<nav align="center">
</nav>
<div align="right" >
<?php
$m = new MongoClient();
$db = $m->app;
$collection = $db->lafaz;
$id=$_REQUEST['_id'];
$cursor = $collection->find(array("_id"=> new MongoID($id)));
foreach ($cursor as $obj)
{
?>
<form action="#" method="post">
<table width="100%" class="top-table" >
<tr>
<td width="40%" align="right"><p>ID:</p></td>
<td width="3%"> </td>
<td width="57%" align="left">
<input class="tf" type="text" name="textfield" id="textfield" readonly value=" <?php echo $obj["_id"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Encoding:</p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield2" id="textfield2" required value=" <?php echo $obj["Encoding"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Pos:</p> </td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield3" id="textfield3" required value=" <?php echo $obj["Pos"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Roman: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield4" id="textfield4" required value=" <?php echo $obj["Roman"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Important: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield5" id="textfield5" required value=" <?php echo $obj["Important"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Hindi: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield6" id="textfield6" required value=" <?php echo $obj["Hindi"];; ?> " ></td>
</tr>
<tr>
<td align="right"><p>English: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield7" id="textfield7" required value=" <?php echo $obj["English"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Type: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield8" id="textfield8" required value=" <?php echo $obj["Type"]; ?> " ></td>
</tr>
</table>
<input name="btn" id="btn" class="button" type="submit" value="Save"></td>
<?php } ?>
</form>
</div>
</div>
</body>
</html>
Here is a fully working page, commented up so you can follow me. You will need to update your PHP runtime to a newer version by the sounds of it but that should already be done, especially since PHP is always backwards compatible currently.
$m = new \MongoClient();
$db = $m->app;
$collection = $db->lafaz;
// Better to sometimes actually check the right array, especially if someone wants to attack
// you by tricking PHP into combining the REQUEST from the GET
if(isset($_POST['btn'])){
// We take the $_POST piece by piece, do a trim to strip white space
// and then we add it to the doc array ready for use in MongoDB
// Of course this is not good practice for production programs but
// it is somewhere to start
$doc = [];
foreach($_POST as $k => $v){
$doc[$k] = trim($v);
}
// This will detect if we are updating or not, if no a is set then no _id was passed
if(isset($doc['_id'])){
// Some validation to ensure that we have a valid MongoID, consider it free knowledge
try{
$_id = new \MongoId($doc['_id']);
}catch(\Exception $e){
throw new \Exception('The _id inputted was not valid: ' . var_export($doc['_id'], true));
}
// Unset the _id as to not raise an error
unset($doc['_id']);
// Could do an upsert here but, meh, I prefer the logic displayed here
$collection->update(["_id"=> $_id], ['$set' => $doc], ['multiple' => true]);
}else{
$collection->insert($doc);
}
// We redirect to somewhere
header('Location:page.php');
// stop further processing and just send the damn headers
exit();
}
// If there is a _id in the REQUEST array
if(isset($_REQUEST['_id'])){
// Some validation to ensure that we have a valid MongoID, consider it free knowledge
try{
$_id = new \MongoId($_REQUEST['_id']);
}catch(\Exception $e){
throw new \Exception('The _id inputted was not valid: ' . var_export($_REQUEST['_id'], true));
}
// We find the one we just did, btw your code atm means this never happens...
$cursor = $collection->find(["_id"=> new \MongoID($_id)]);
}else{
// We just find all
$cursor = $collection->find();
}
?>
<!doctype html>
<html>
<head>
<style type="text/css">
header a {
font-weight: bold;
font-family: Sarina;
font-size: 45pt;
font-style: oblique;
text-decoration: none;
text-shadow: 2px 2px #33CCFF;
color: #369;
}
</style>
<link href="form.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
<title>Edit Word</title>
</head>
<body>
<div>
<header align="center">Tarmeem -e- Haraf </header>
<nav align="center">
</nav>
<div align="right">
<!-- Add a new one form. There are better ways to do this but this is just to get it working -->
<form action="#" method="post">
<table width="100%" class="top-table" >
<tr>
<td align="right"><p>Encoding:</p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Encoding" id="textfield2" required></td>
</tr>
<tr>
<td align="right"><p>Pos:</p> </td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Pos" id="textfield3" required></td>
</tr>
<tr>
<td align="right"><p>Roman: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Roman" id="textfield4" required></td>
</tr>
<tr>
<td align="right"><p>Important: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Important" id="textfield5" required></td>
</tr>
<tr>
<td align="right"><p>Hindi: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Hindi" id="textfield6" required></td>
</tr>
<tr>
<td align="right"><p>English: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="English" id="textfield7" required></td>
</tr>
<tr>
<td align="right"><p>Type: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Type" id="textfield8" required></td>
</tr>
</table>
<input name="btn" id="btn" class="button" type="submit" value="Create">
</form>
<?php foreach ($cursor as $obj){ ?>
<form action="#" method="post">
<table width="100%" class="top-table" >
<tr>
<td width="40%" align="right"><p>ID:</p></td>
<td width="3%"> </td>
<td width="57%" align="left">
<input class="tf" type="text" name="_id" id="textfield" readonly value="<?= $obj["_id"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Encoding:</p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Encoding" id="textfield2" required value="<?= $obj["Encoding"] ?> "/></td>
</tr>
<tr>
<td align="right"><p>Pos:</p> </td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Pos" id="textfield3" required value="<?= $obj["Pos"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Roman: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Roman" id="textfield4" required value="<?= $obj["Roman"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Important: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Important" id="textfield5" required value="<?= $obj["Important"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Hindi: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Hindi" id="textfield6" required value="<?= $obj["Hindi"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>English: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="English" id="textfield7" required value="<?= $obj["English"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Type: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Type" id="textfield8" required value="<?= $obj["Type"] ?>"/></td>
</tr>
</table>
<input name="btn" id="btn" class="button" type="submit" value="Save">
</form>
<?php } ?>
</div>
</div>
</body>
</html>
<?php

How to update stock table after making a sale

i'm creating an inventory management system and i can't figure out how to link inventory table with the sales table so that products in inventory table are updated when a sale is made. I'm using MySql
Here's the sold.php, When I sell a product this page save a record in sale table but i want to update manuf table qtyleft row.
<?php require_once('../Connections/bidco.php'); ?>
<?php
mysql_select_db($database_bidco, $bidco);
$query_rsSaletype = "SELECT * FROM saletype ORDER BY type ASC";
$rsSaletype = mysql_query($query_rsSaletype, $bidco) or die(mysql_error());
$row_rsSaletype = mysql_fetch_assoc($rsSaletype);
$totalRows_rsSaletype = mysql_num_rows($rsSaletype);
mysql_select_db($database_bidco, $bidco);
$query_rsCustomercategory = "SELECT * FROM buyertype ORDER BY type ASC";
$rsCustomercategory = mysql_query($query_rsCustomercategory, $bidco) or die(mysql_error());
$row_rsCustomercategory = mysql_fetch_assoc($rsCustomercategory);
$totalRows_rsCustomercategory = mysql_num_rows($rsCustomercategory);
mysql_select_db('invmgt', mysql_connect('localhost','root','dream2014')) or die(mysql_error());
?>
<?php
//Start session
session_start();
//Unset the variables stored in session
unset($_SESSION['SESS_ID']);
unset($_SESSION['SESS_Username']);
unset($_SESSION['SESS_Name']);
?>
<?php
if (isset ($_POST ['Submit']))
{
$da=date("Y-m-d");
$itemname=$_POST['itemname'];
$itemcode=$_POST['itemcode'];
$itemtype=$_POST['itemtype'];
$price=$_POST['unitprice'];
$quantity=$_POST['quantity'];
$ttype=$_POST['select2'];
$ccat=$_POST['select'];
$idate=date("Y-m-d");
mysql_query("INSERT INTO sold_goods (itemname, itemcode, itemtype, unitprice, quantity, transactiontype, customercategory, Date) VALUES ('$itemname', '$itemcode', '$itemtype', '$unitprice', '$quantity', '$ttype', '$ccat', '$idate')");
header("location:sold.php");
}
?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<table width="711" height="27" border="0">
<tr>
<td width="216">Manufactured Goods </td>
<td width="152">Sold Goods </td>
<td width="148">Client Details </td>
<td width="167">User Accounts </td>
<td width="167">Home </td>
<td width="167"><div align="right">Logout </div></td>
</tr>
</table>
<p> </p>
<form id="form9" name="form9" method="POST" action="">
<table width="828" height="572" border="1" align="center">
<tr>
<td height="55" colspan="3"><div align="center"><strong>SOLD GOODS </strong></div></td>
</tr>
<tr>
<td height="33" colspan="2">User</td>
<td height="33"><?php date_default_timezone_set('Africa/Nairobi'); echo "Time " . date("h:i:sa");
?></td>
</tr>
<tr>
<td height="14">Customer code
<div align="center"></div></td>
<td height="14">Customer category
<select name="select2">
<?php
do {
?>
<option value="<?php echo $row_rsCustomercategory['type']?>"><?php echo $row_rsCustomercategory['type']?></option>
<?php
} while ($row_rsCustomercategory = mysql_fetch_assoc($rsCustomercategory));
$rows = mysql_num_rows($rsCustomercategory);
if($rows > 0) {
mysql_data_seek($rsCustomercategory, 0);
$row_rsCustomercategory = mysql_fetch_assoc($rsCustomercategory);
}
?>
</select></td>
<td height="14"><?php echo "Date of Transaction " . date("Y/m/d")?></td>
</tr>
<tr>
<td height="15"> </td>
<td height="15"> </td>
<td height="15"> </td>
</tr>
<tr>
<td height="32" colspan="2"><div align="center"><strong>ITEM DETAILS </strong></div></td>
<td width="258" rowspan="3"><div align="center"></div>
<div align="center"></div></td>
</tr>
<tr>
<td width="227" height="23"><div align="right">Item name </div></td>
<td width="321"><input name="itemname" type="text" id=itemname /></td>
</tr>
<tr>
<td><div align="right">Item code </div></td>
<td><input name="itemcode" type="text" id="itemcode" /></td>
</tr>
<tr>
<td><div align="right">Item type </div></td>
<td><input name="itemtype" type="text" id="itemtype" /></td>
<td rowspan="3"><div align="center"></div></td>
</tr>
<tr>
<td height="23"><div align="right">Unit price </div></td>
<td><input name="unitprice" type="text" id=price /></td>
</tr>
<tr>
<td height="32"><div align="right">Quantity </div></td>
<td><input name="quantity" type="text" id=qty /></td>
</tr>
<tr>
<td height="42" colspan="2"><div align="center"><strong>SALES</strong></div></td>
<td><div align="center"></div>
<div align="center"></div></td>
</tr>
<tr>
<td><div align="right">Gross</div></td>
<td> </td>
<td><div align="center"></div></td>
</tr>
<tr>
<td><div align="right">Type of sale </div></td>
<td><select name="select">
<?php
do {
?>
<option value="<?php echo $row_rsSaletype['type']?>"><?php echo $row_rsSaletype['type']?></option>
<?php
} while ($row_rsSaletype = mysql_fetch_assoc($rsSaletype));
$rows = mysql_num_rows($rsSaletype);
if($rows > 0) {
mysql_data_seek($rsSaletype, 0);
$row_rsSaletype = mysql_fetch_assoc($rsSaletype);
}
?>
</select></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="28" colspan="2"><div align="center"><strong>DEDUCTIONS</strong></div></td>
<td> </td>
</tr>
<tr>
<td height="36"><div align="right">Discount</div></td>
<td height="36"> </td>
<td><div align="center">
<input type="submit" name="Submit" value="ADD NEW" />
</div></td>
</tr>
<tr>
<td height="36"><div align="right">V.A.T</div></td>
<td height="36"> </td>
<td> </td>
</tr>
<tr>
<td height="36"><div align="right">Net</div></td>
<td height="36"> </td>
<td rowspan="2"><div align="center">
<input type="reset" name="Submit5" value="CANCEL" />
</div></td>
</tr>
<tr>
<td height="30" colspan="2"><div align="center">
<label><strong>CALCULATE</strong></label>
</div></td>
</tr>
</table>
</form>
<p> </p>
<p> </p>
</body>
</html>
<?php
mysql_free_result($rsSaletype);
mysql_free_result($rsCustomercategory);
?>
This is manuf.php for adding new stock
<?php require_once('../Connections/bidco.php'); ?>
<?php
if (isset ($_POST ['Submit']))
{
$da=date("Y-m-d");
$itemname=$_POST['itemname'];
$itemcode=$_POST['itemcode'];
$itemtype=$_POST['itemtype'];
$unitprice=$_POST['unitprice'];
$quantity=$_POST['quantity'];
$idate=date("Y-m-d");
mysql_query("INSERT INTO manuf (itemname, itemcode, itemtype, price, qtyleft, Date) VALUES ('$itemname', '$itemcode', '$itemtype', '$unitprice', '$quantity', '$idate')");
header("location:manuf.php");
}
?>
?><!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<p> </p>
<table width="711" height="27" border="0" align="center">
<tr>
<td width="216">Inventory</td>
<td width="152">Sold Goods </td>
<td width="148">Client Details </td>
<td width="167">User Accounts </td>
<td width="167">Home </td>
</tr>
</table>
<p> </p>
<form id="form1" name="form1" method="POST" action="">
<table width="743" height="282" border="1" align="center">
<tr>
<td colspan="3"><div align="center"><strong>ADD NEW PRODUCT </strong></div></td>
</tr>
<tr>
<td colspan="2">Username </td>
<td width="265"><?php date_default_timezone_set('Africa/Nairobi'); echo "Time " . date("h:i:sa");
?></td>
</tr>
<tr>
<td width="180"><div align="right">Item name </div></td>
<td width="276"><input name="itemname" type="text" id="itemname" /></td>
<td> </td>
</tr>
<tr>
<td><div align="right">Item code </div></td>
<td><input name="itemcode" type="text" id="itemcode" /></td>
<td><div align="center"></div></td>
</tr>
<tr>
<td><div align="right">Type</div></td>
<td><input name="itemtype" type="text" id="itemtype" /></td>
<td><div align="center"></div></td>
</tr>
<tr>
<td><div align="right">Unit price </div></td>
<td><input name="unitprice" type="text" id="unitprice" /></td>
<td><div align="center"></div></td>
</tr>
<tr>
<td><div align="right">Quantity</div></td>
<td><input name="quantity" type="text" id="quantity" /></td>
<td><div align="center">
<input type="submit" name="Submit" value="Add stock" />
</div></td>
</tr>
<tr>
<td><div align="right">Date</div></td>
<td><input name="idate" type="varchar" id="idate" /></td>
<td><div align="center">
<input type="reset" name="Submit5" value="Cancel" />
</div></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
<div align="center"></div>
<div align="center"></div>
<p> </p>
</body>
</html>
After inserting to sold table, update the manf table as well:
mysql_query("INSERT INTO sold_goods
(itemname, itemcode, itemtype, unitprice, quantity,
transactiontype, customercategory, Date)
VALUES
('$itemname', '$itemcode', '$itemtype', '$unitprice',
'$quantity', '$ttype', '$ccat', '$idate')");
mysql_query("UPDATE manuf SET qtyleft = qtyleft - $quantity where
itemcode = '$itemcode'" );

MySql php data display in tables

I am doing a simple set of PHP scripts to edit and return MySQL records from a web site.
Everything works fine but there is a cosmetic that I just cannot seem to correct.
I presume being very rusty I am missing something obvious - I have tried everything I can think of though.
The content of field ADDTEXT can be fairly large and i would like to word wrap it all into the table cell. This script truncates it when a single line length is exceeded.
And yes I know I should be using mysqli_... but I am deaeling with that !
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>EDIT NEWS ITEM</td>
</tr>
<tr>
<td>
<table style="width:100%">
<tr>
<?
$id=$_GET['id'];
include "D***************.uk\public_html\html\ConnectDB.php";//database connection
$order = "SELECT * FROM st¬¬¬¬¬¬¬ where TYPE = '".$id."'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<? echo "$row[TITLE]"?>">
<tr>
<td>Item Title</td>
<td>
<input type="text" name="title"
value="<? echo "$row[TITLE]"?>">
</td>
</tr>
<tr>
<td>Item Text</td>
<td>
<input type="text" name="text"
value="<? echo "$row[ADDTEXT]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
Change it to textarea field.
<textarea name="text"><?php echo $row['ADDTEXT'];?></textarea>
Try it
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>EDIT NEWS ITEM</td>
</tr>
<tr>
<td>
<table style="width:100%">
<tr><td>
<?
$id=$_GET['id'];
include "D***************.uk\public_html\html\ConnectDB.php";//database connection
$order = "SELECT * FROM st¬¬¬¬¬¬¬ where TYPE = '".$id."'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo $row['TITLE']?>">
<tr>
<td>Item Title</td>
<td>
<input type="text" name="title"
value="<?php echo $row['TITLE']?>">
</td>
</tr>
<tr>
<td>Item Text</td>
<td>
<input type="text" name="text"
value="<?php echo $row['ADDTEXT']?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>

Load Returned mySQL Values Into Form

I wonder whether someone may be able to help me please.
I've put together a form and php code (below) that allows an administrator to search for member records from a mysql database using the email address as the search criteria.
HTML Form
<form name="memberpasswordresetform" id="memberpasswordresetform" method="post" action="search.php">
<div class="container">
<p align="justify">Member Details </p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="26%" height="25"><strong>Email Address </strong></td>
<td width="4%"> </td>
<td width="70%"><input name="email" type="email" id="email" size="50" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm Email Address </strong></td>
<td> </td>
<td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
</tr>
<tr>
<td height="25"><label>
<input type="submit" name="Submit" value="search" />
</label></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Last Name </strong></td>
<td> </td>
<td><input name="lname" type="text" id="lname" size="30" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>New Password</strong></td>
<td> </td>
<td><input name="newpass" type="password" id="newpass" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm New Password </strong></td>
<td> </td>
<td><input name="conf_newpass" type="password" id="conf_newpass" size="30" /></td>
</tr>
<tr>
<td height="25"><input type="submit" name="save" value="save" /></td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</form>
PHP Script
<?php
include("admin/link.php");
include("admin/opendb.php");
mysql_select_db ("userdetails");
$term = $_POST['email'];
$sql = mysql_query("select forename, surname, email address from userdetails where emailaddress like '%$email%'");
while ($row = mysql_fetch_array($sql)){
echo '<br/> First Name: '.$row['forename'];
echo '<br/> Last Name: '.$row['surname'];
echo '<br/><br/>';
}
?>
The search functionality works fine, but I can't work out how to populate the forename and surname fields on my form from the records retrieved from my database. I've been looking for, and found examples on how to do this, if I want to simply show the data as a table, but I can't find any that explain how to populate the fields in a form.
I just wondered whether it would be at all possible that someone could provide some guidance please on how I can do this.
Many thanks
The previous answer will work fine if short tags are enabled on the server. You should stick to the long syntax as below in case you change hosting providers at a later date.
<input name="fname" type="text" id="fname" size="30" value="<?php echo $row['surname']; ?>" />
just set the value of your input element
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" value="<?= $row['surname'] ?>" /></td>
</tr>

Categories