Editing mysql data with drop downs - php

I have a data entry page where a few drop downs exist. The selected item in the drop down stores in my mysql database with no issue.
I've made a 2nd page to edit individual records. I can display the data from the drop down in a text box with no problem. However, I want to be able to edit the result with the same drop down choices.
I can add the drop down menu on my edit.php page with all the correct options, but the stored value from the database does not appear. Instead I get the first choice by default and not the stored value.
<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";
while ($row = mysql_fetch_array($position_result)) {
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>
I'm using POST and GET to get the correct record ID.
My text boxes, which work fine are as follows:
Department:<input type="text" name="department" size="20" value="<?php echo "$row[department]"; ?>">
I'm assuming I have to build some sort of if statement to display the stored value?
not sure if this helps, but this is my how I grab the ID for the record I want to edit:
<?php
$id= ($_GET["id"]);
$sql = "SELECT * FROM people
WHERE id='$id' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>
Entire Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<?php
$BASE_PATH = 'C:\xampp\htdocs\OGS';
include_once($BASE_PATH . "\includes\layouts\header.php");
?>
<div id="main">
<div id="subnavigation">
<?php include_once($BASE_PATH . "\mods\main_menu\index.html");?>
</div>
<div id="page"
<br><br>
<table border=1>
<tr>
<td align=center>Update Employee Information</td>
</tr>
<tr>
<td>
<table>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('ogs');
?>
<?php
$id= ($_GET["id"]);
$sql = "SELECT * FROM people
WHERE id='$id' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo "$row[id]"; ?>">
<fieldset>
<legend><b>Name</b></legend>
First Name:<input type="text" name="first_name" size="20" value="<?php echo "$row[first_name]"; ?>">
Last Name:<input type="text" name="last_name" size="40" value="<?php echo "$row[last_name]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Contact Information</b></legend>
Town:<input type="text" name="town" size="20" value="<?php echo "$row[town]"; ?>">
Address:<input type="text" name="address" size="40" value="<?php echo "$row[address]"; ?>">
Province:<input type="text" name="province" size="20" value="<?php echo "$row[province]"; ?>">
Postal Code:<input type="text" name="postal_code" size="40" value="<?php echo "$row[postal_code]"; ?>">
<br><br>
Home Phone:<input type="text" name="home_phone" size="20" value="<?php echo "$row[home_phone]"; ?>">
Cell Phone:<input type="text" name="cell_phone" size="40" value="<?php echo "$row[cell_phone]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Emergency Contact</b></legend>
Emergency Contact Name:<input type="text" name="first_name" size="20" value="<?php echo "$row[first_name]"; ?>">
Emergency Contact Number:<input type="text" name="last_name" size="40" value="<?php echo "$row[last_name]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Work Information</b></legend>
Role:<input type="text" name="role" size="20" value="<?php echo "$row[role]"; ?>">
Employer:<input type="text" name="company_works_for" size="40" value="<?php echo "$row[company_works_for]"; ?>">
<br><br>
Department:<input type="text" name="department" size="20" value="<?php echo "$row[department]"; ?>">
Position:
<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";
// You should use PHP to get the existing value here, I have made it up here as 14
$existing_id = '$row[id]';
while ($row = mysql_fetch_array($position_result))
{
// Check if the existing id is the same as the current id we are displaying
// If it is, set the selected attribute
if($existing_id == $row['id'])
echo "<option selected='selected' value='" . $row['id'] . "'>" . $row['position'] . "</option>";
else
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>
<br><br>
Is Supervisor?:
<input type="radio" name="is_supervisor" value="<?php echo "$row[is_supervisor]"; ?>"> Yes
<input type="radio" name="is_supervisor" value="<?php echo "$row[is_supervisor]"; ?>"> No
<br><br>
Is Active?:
<input type="radio" name="active_employee" value="<?php echo "$row[active_employee]"; ?>"> Yes
<input type="radio" name="active_employee" value="<?php echo "$row[active_employee]"; ?>"> No
<br><br>
Start Date:<input type="text" name="start_date" size="40" value="<?php echo "$row[start_date]"; ?>">
</fieldset>
<input type="submit"
name="submit value" value="Update">
</form>
</div>
</div>
</body>
</html>
The record for Nicholas Furlong shows him as a Health & Safety Coordinator.
http://prntscr.com/3o34g2
But when I click edit, and go to my edit page, it has him listed as controller. (This is the first option in the column.)
http://prntscr.com/3o36qu

Its not entirely clear what you are asking. But I will try
<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";
// You should use PHP to get the existing value here, I have made it up here as 14
$existing_id = 14;
while ($row = mysql_fetch_array($position_result))
{
// Check if the existing id is the same as the current id we are displaying
// If it is, set the selected attribute
if($existing_id == $row['id'])
echo "<option selected='selected' value='" . $row['id'] . "'>" . $row['position'] . "</option>";
else
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>

Related

fill form after option select

I have a product list that filled automatically from the DB, I want to get the data corresponding to the selected product and fill the form.
<form name='login' action='<?php echo ($_SERVER['PHP_SELF']); ?>' method='post' enctype="multipart/form-data">
<?php
$sql = "SELECT product_name FROM products ORDER BY product_name";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
echo "<select>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option>" . $row["product_name"]."</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$mysqli->close();
?>
<input type="text" name="product_code" placeholder="Product Code" />
<input type="text" name="product_name" placeholder="Product Name" />
<input type="text" name="product_price" placeholder="Price" />
<input type="file" name="fileToUpload" accept="image/*">
<textarea rows="8" name="product_desc_de" placeholder="Deutsch description"></textarea>
<textarea rows="8" name="product_desc_en" placeholder="English description"></textarea>
<textarea rows="8" name="product_desc_es" placeholder="Spanich description"></textarea>
<button type="submit" class="btn btn-default">Submit</button>
</form>
I know it should be somehow with AJAX
You have not shown any AJAX code so I assume that was just a mistake.
You do not NEED Ajax to make a form work.
So first give your <select> tag a name
echo '<select name="prods">';
And then your <option> tags a value
echo '<option value="' . $row["product_name"] . '">' . $row["product_name"] . '</option>';
Now when you look at the $_POST array you will have a $_POST['prods']
Personally I would add the id field to the SELECT
$sql = "SELECT id, product_name FROM products ORDER BY product_name";
And use the id as the value like this
echo '<option value="' . $row['id'] . '">' . $row['product_name'] . '</option>';

Only one field in my form being set php and sql

Hi I am coming into an issue in my form when i try to set the value of my field
to a value of a variable it works exactly how i want it for my product field but nothing is showing up in the price field in my form
any help is appreciated i have benn at it for a good while
$query = "SELECT * FROM suplements WHERE product = '$product' OR name = '$prodname' OR price = '$price' OR image = '$image'";
$result = #mysql_query($query) or die ("could not execute SQL query");
while($row = mysql_fetch_array($result))
{ ?>
<table>
<tr>
<th>Product :<?php echo $row["product"]?></th>
</tr>
<td><img src = "<?php echo $row["image"] ?>"height="400" width="400"><?php ?>
<td>
<form>
<input type="text" name="qty" value="1" size="2"><br>
Product <input type="text" name="product" value="<?php echo htmlentities($product); ?>" /></br>
Price <input type="text" name="price" value="<?php echo htmlentities($price); ?>" />
<input type="submit" name="submit" value="ADD" >
</form>
<?php echo $row["information"];?></td>
<tr><td><?php echo $row["product"];?>
<?php echo $row["name"];?>
<?php echo $row["drand"];?>
<?php echo $row["weight"];?>
<td><?php echo $row["price"];?></td></tr>
</tr>
</table>
Is this what you are looking for?
Product <input type="text" name="product" value="<?php echo htmlentities($row['product']); ?>" /></br>
Price <input type="text" name="price" value="<?php echo htmlentities($row['price']); ?>" />
For debug sake, do this after the while: "print_r($row);" - this will show you the exact contents of the $row variable
Plus... it would be nice to add limit 1 to your query

update form not populating from existing data in mysql database

Hi im trying to populate my Update form with existing data from mysql database so I can modify the data and save it back to the mysql database. Everything is working and no errors but its not populating at all. Thanks in advance
Index.php
<?php
require('includes/mysqli_connect.php');
$query = "SELECT * FROM item";
$result = mysqli_query($dbc, $query);
WHILE($items = mysqli_fetch_array($result)) {
echo "<h3>" . $items['item_id'] . "</h3>";
echo "<p>" . $items['title'] . "</p>";
echo "Modify Item";
echo "<span> </span>";
echo "<span> </span>";
echo "Delete Item";
}
?>
Modify.php
<?php
require('includes/mysqli_connect.php');
if(!isset($_POST['submit'])) {
include('includes/mysqli_connect.php');
$q = "SELECT * FROM item WHERE item_id = '$_GET[item_id]'";
$result = mysqli_query($dbc, $q);
$items = mysqli_fetch_array($result)
/* or die("Error: ".mysqli_error($dbc)) */;
}
?>
<h1>Modify item</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<h4>Selling format:</h4>
<h4>Auction:
<input type="checkbox" value="<?php echo $items['auction']; ?>" name="inputauction"></h4>
<h4>Buy Now:
<input type="checkbox" value="<?php echo $items['buynow']; ?>" name="inputbuynow"></h4>
<h4>Select category:</h4>
<select name="cat_id">
<?php
include('includes/mysqli_connect.php');
$q = "SELECT cat_id, cat_name FROM category ".
"ORDER BY cat_id";
$r = mysqli_query($dbc,$q);
while($row = mysqli_fetch_array($r))
{
echo "<option value=\"".$row['cat_id']."\">".$row['cat_name']."</option>\n ";
}
?>
</select>
<h4>Title:
<input type="text" size="30" maxlength="30"
value="<?php echo $items['title']; ?>" name="inputtitle"></h4>
<h4>Description:</h4>
<textarea rows="7" cols="27" value="<?php echo
$items['description']; ?>" name="inputdescription" wrap="physical"></textarea>
<p><a href="#" onclick="window.open('uploadwin.php', 'newwindow',
'width=200, height=150'); return false;">
<h4>Click here to upload picture</h4></a></p>
<h4>Auction Start Price:
<input type="text" size="15" maxlength="15"
value="<?php echo $items['auct_price']; ?>" name="inputauct_price"></h4>
<h4>Buynow Price:
<input type="text" size="15" maxlength="15"
value="<?php echo $items['buynow_price']; ?>" name="inputbuynow_price"></h4>
<h4>Duration:
<select name="inputduration">
<option value="<?php echo $items['1 Day']; ?>">1 Day</option>
<option value="<?php echo $items['3 Days']; ?>">3 Days</option>
<option value="<?php echo $items['5 Days']; ?>">5 Days</option>
</select></h4>
<h4>Quantity:
<input type="text" size="15" maxlength="15"
value="<?php echo $items['qty']; ?>" name="inputqty"></h4>
<h4>Item Location:
<input type="text" size="15" maxlength="15"
value="<?php echo $items['item_location']; ?>" name="inputitem_location"></h4>
<h4>Accepted Payment:</h4>
<h4>PayPal:
<input type="checkbox" value="<?php echo $items['paypal']; ?>" name="inputpaypal"></h4>
<h4>EFT:
<input type="checkbox" value="<?php echo $items['EFT']; ?>" name="inputEFT"></h4>
<h4>Postage:</h4>
<h4>Pickup:
<input type="checkbox" value="<?php echo $items['pickup']; ?>" name="inputpickup"></h4>
<h4>Post Interstate:
<input type="checkbox"
value="<?php echo $items['post_nationally']; ?>" name="inputpost_nationally"></h4>
<h4>Post National:
<input type="checkbox" value="
<?php echo $items['post_internation']; ?>" name="inputpost_internation"></h4>
<h4>Post Internation:
<input type="checkbox"
value="<?php echo $items['buyerpay_post']; ?>" name="inputbuyerpay_post"></h4>
<input type="hidden" name="item_id" value="<?php echo $_GET['item_id']; ?>" />
<input type="submit" value="Modify" name="submit">
</form><br><br>
<?php
if(isset($_POST['submit'])) {
$u = "UPDATE `ICADBS504A`.`item` SET `auct_price` = '$_POST[inputauct_price]',
`buynow_price` ='$_POST[inputbuynow_price]', `title` = '$_POST[inputtitle]',
`description` = '$_POST[inputdescription]', `qty` = '$_POST[inputqty]',
`img_path` = '$imgpath', `item_location` = '$_POST[inputitem_location]',
`auction` = '$_POST[inputauction]', `buynow` = '$_POST[inputbuynow]',
`duration` = '$_POST[inputduration]', `pickup` = '$_POST[inputpickup]',
`post_nationally` = '$_POST[inputpost_nationally]',
`post_internation` = '$_POST[inputpost_internation]',
`buyerpay_post` = '$_POST[inputbuyerpay_post]', `cat_id` = '$cat',
`paypal` = '$_POST[inputpaypal]', `EFT` = '$_POST[inputEFT]' WHERE
`item_id` = $_POST[item_id]";
mysqli_query($u) or die(mysqli_error());
echo "Item has been modified!";
header("Location: index.php");
}
include('includes/footer.html');
?>

populating form with mysql data

Hopefully someone can help with my problem, I have a database using MySQL and I have a form that posts to the database, this all works fine.
I have created a customer look up form using HTML and PHP that echos a table and then the table is filled with the MySQL data, again this all works OK.
My problem is that I would like to click the table row or text in that row which ever and open it in a HTML form and include all the data from the row in the MySQL database.
<div id="content">
<h3>Customer Lookup</h3>
<?php
$con=mysqli_connect("localhost","root","","repairsdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customers");
echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Surname</th>
<th>Address</th>
<th>Postcode</th>
<th>Landline</th>
<th>Mobile</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['addressl1'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['landline'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "<td>" . $row['businessname'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<FORM>
</FORM>
</div>
New Code
<h3>Customer Lookup</h3>
<?php
$con=mysqli_connect("localhost","root","","repairsdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customers");
echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Surname</th>
<th>Address</th>
<th>Postcode</th>
<th>Landline</th>
<th>Mobile</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='/repairdb/customer_form.php?customerid=" . $row['customerid'] . "'>" . $row['firstname'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['addressl1'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['landline'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
customer_form.php
<?php
$con=mysqli_connect("localhost","root","","repairsdb");
if( isset( $_GET['customerid'] ) ) {
$customerid = $_GET['customerid'];
}
$result = mysqli_query($con,"SELECT * FROM customers where customerid = " . $customerid . " Limit 1");
?>
<form method="post">
<input type="text" name="firstname" value="$result['firstname']" />
<input type="text" name="surname" value="$result['surname']" />
<input type="text" name="addressl1" value="$result['addressl1']" />
<input type="text" name="postcode" value="$result['postcode']" />
<input type="text" name="landline" value="$result['landline']" />
<input type="text" name="mobile" value="$result['mobile']" />
<input type="text" name="businessname" value="$result['businessname']" />
</form>
The easiest way to do this would be to open the form in a new page. So to do this we would start by adding a link to your table.
I'm going to assume that in your database you have an unique ID for each customer - we will use this in the link:
echo "<tr>";
echo "<td><a href='/customer_form.php?customer_id=" . $row['ID'] . "'>" . $row['firstname'] . "</a></td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['addressl1'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['landline'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "<td>" . $row['businessname'] . "</td>";
echo "</tr>";
As you can see I have named the new page customer_form.php. And we will pass the ID to this page via the URL, like so.
www.example.com/customer_form.php?customer_id=3
In your new customer_form.php page you will first need to retrieve the ID from the URL.
if( isset( $_GET['customer_id'] ) ) {
$customer_id = $_GET['customer_id'];
}
Then we will modify this block of code a little more to retrieve the customer from the database, using the ID, and then we can add this data to a new form.
if( isset( $_GET['customer_id'] ) ) {
$customer_id = $_GET['customer_id'];
$result = mysqli_query($con,"SELECT * FROM customers where ID = " . $customer_id . " Limit 1");
echo '
<form method="post">
<input type="text" name="firstname" value="'. $result['firstname'] .'" />
<input type="text" name="surname" value="'. $result['surname'] .'" />
<input type="text" name="addressl1" value="'. $result['addressl1'] .'" />
<input type="text" name="postcode" value="'. $result['postcode'] .'" />
<input type="text" name="landline" value="'. $result['landline'] .'" />
<input type="text" name="mobile" value="'. $result['mobile'] .'" />
<input type="text" name="businessname" value="'. $result['businessname'] .'" />
</form>
';
}
You could do it other ways, in a pop up window, or even on the same page using JavaScript. But this is the least complicated.
I should note that the SQL query I wrote would be vulnerable to SQL injection, since it takes a value from the address bar and injects it directly into the query. But it's a little outside of the scope of the question, so I suggest you look it up if interested.
sounds like this would work...
use clicks on link (selects something in)
then - jqueryAJAX POSTs FORM
then - php processPOST
then - mysql getDataFrom DB
then - php process MYSQL results && echo $jsoResults
then - jqueryAJAX process results open new Form with inputs populated...
I managed to sort the errors, I have posted the code for anyone having the same problem. I have answered this question myself only for the reason so I can post the final code, but the credit has to go to #user1100149 and also a chap on YouTube.
Link to youtube video editing mysql with PHP
<?php
// Connects to Our Database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("repairsdb") or die(mysql_error());
if(!isset ($_POST['submit'])) {
$q = "SELECT * FROM customers WHERE customerid = $_GET[customerid]";
$result = mysql_query($q);
$person = mysql_fetch_array($result)or die (mysql_error());
}
?>
<form name="submit_customer" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div id="frcol3">
<fieldset>
<label id="label" for="webaddress">Business</label>
<input name="businessname" type="text" class="inputBox" id="" value="<?php echo $person['businessname']; ?>" />
<label id="label" for="firstname">First Name</label>
<input class="inputBox" type="text" name="firstname" id="" value="<?php echo $person['firstname']; ?>" />
<label id="label" for="Surname">Surname</label>
<input name="surname" type="text" class="inputBox" id="" value="<?php echo $person['surname']; ?>" />
<label id="label" for="landline">Telephone No.</label>
<input name="landline" type="text" class="inputBox" id="" value="<?php echo $person['landline']; ?>" />
<label id="label" for="mobile">Mobile No.</label>
<input name="mobile" type="text" class="inputBox" id="" value="<?php echo $person['mobile']; ?>" />
<label id="label" for="email">Email</label>
<input name="email" type="text" class="inputBox" id="" value="<?php echo $person['email']; ?>" />
</fieldset>
</div>
<div id="frcol2">
<fieldset>
<label id="label" for="webaddress">Web Address</label>
<input name="webaddress" type="text" class="inputBox" id="" value="<?php echo $person['webaddress']; ?>" />
<label id="label" for="addressl1">Address Line 1</label>
<input name="addressl1" type="text" class="inputBox" id="" value="<?php echo $person['addressl1']; ?>" />
<label id="label" for="addressl2">Address Line 2</label>
<input name="addressl2" type="text" class="inputBox" id="" value="<?php echo $person['addressl2']; ?>" />
<label id="label" for="town">Town</label>
<input name="town" type="text" class="inputBox" id="" value="<?php echo $person['town']; ?>" />
<label id="label" for="county">County</label>
<input name="county" type="text" id="" class="inputBox" value="<?php echo $person['county']; ?>" />
<label id="label" for="postcode">Postcode</label>
<input name="postcode" type="text" id="" class="inputBox" value="<?php echo $person['postcode']; ?>" />
</fieldset>
</div>
<div id="frcol1">
<textarea name="notes" id="notes" placeholder="<?php echo $person['notes']; ?>" cols="45" rows="5"></textarea>
<br />
<input type="hidden" name="customerid" value="<?php echo $_GET['customerid']; ?>" />
<input type="submit" name="submit" id="" class=" button" value="Submit" />
</div>
</form>

PHP comment form

I have a comment form that I have created. It gets the id from the database and prints out the data that goes with that id, but it also prints out the information into the form. How can I get a blank form, so that the user can add a comment to the record?
This the code for the form:
<form method="post" action="pv.php?id=<?php echo $row['ID']?>&action=<?php echo $form_action ?>">
<fieldset>
<legend></legend>
<p>
<label for="cname">Date Of Birth</label> *
<input id="cname" name="dateofbirth" class="required date" value="<?php echo $row['Date_Of_Birth']?>" /> (eg 1978.11.11)
</p>
<p>
<label for="cgender">Gender</label> *
<input type="radio"
name="gender"
value="Male"
<?php if($row['Gender']=='male'){echo 'checked';}?>/>
Male
<input type="radio"
name="gender"
value="Female"
<?php if($row['Gender']=='female'){echo 'checked';}?>/> Female </td>
</p>
<p>
<label for="curl">Title</label> *
<select name="title" id="title" class="required">
<option value="">Please Select</option>
<option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option>
<option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option>
<option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option>
<option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option>
<option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option>
</select>
</p>
<p>
<label for="ccomment">First Name</label> *
<input type="text" name="firstname" value="<?php echo $row['First_Name']?>" maxlength="50" />
</p>
<p>
<label for="cemail">Last Name</label> *
<input id="cemail" type="text" name="lastname"
value="<?php echo $row['Last_Name']?>" maxlength="75" />
</p>
<p>
<label for="ccomment">Address 1</label>*
<input type="text" name="address1"
value="<?php echo $row['Address_Line_1']?>" maxlength="50" />
</p>
<p>
<label for="ccomment">Address 2</label>
<input type="text" name="address2"
value="<?php echo $row['Address_Line_2']?>" maxlength="50" />
</p>
<p>
<label for="ccomment">City</label>*
<input type="text" name="city"
value="<?php echo $row['City']?>" maxlength="50" />
</p>
<p>
<label for="ccomment">Postcode</label>*
<input type="text" name="postcode"
value="<?php echo $row['Postcode']?>" maxlength= "10" /> (eg LE5 5QE)
</p>
<p>
<label for="ccomment">Contact No</label>*
<input type="text" name="contactno"
value="<?php echo $row['Contact_No']?>" maxlength= "12" /> (eg 077448825723)
</p>
<p>
<label for="ccomment">Email</label>*
<input type="text" name="email"
value="<?php echo $row['Email']?>" maxlength= "40"/> (eg info#example.com)
</p>
<p>
<label for="ccomment">Comment</label>
<textarea rows="10" cols="30" name="note"
maxlength= "500"><?php echo $row['Additional_Comment']?></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
<p>
<a href='pv.php'>Main Page</a>
</p>
</fieldset>
</form>
This is the code for printing out the data on the page:
if($_GET['action'] == 'comment') {
$form_action = 'comment_ok';
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM project_data WHERE id='$id'");
$row = mysql_fetch_array($result);
echo'<b>';
echo $row['Date_Of_Birth'];
echo '&nbsp&nbsp';
echo $row['Gender'];
echo '&nbsp&nbsp';
echo $row['Title'];
echo '&nbsp&nbsp';
echo $row['First_Name'];
echo '&nbsp&nbsp';
echo $row['Last_Name'];
echo '&nbsp&nbsp';
echo $row['Address_Line_1'];
echo '&nbsp&nbsp';
echo $row['Address_Line_2'];
echo '&nbsp&nbsp';
echo $row['City'];
echo '&nbsp&nbsp';
echo $row['Postcode'];
echo '&nbsp&nbsp';
echo $row['Contact_No'];
echo '&nbsp&nbsp';
echo $row['Email'];
echo '&nbsp&nbsp';
echo $row['Additional_Comment'];
echo '</b>';
}
And a snippet of the code I am using to send the id to the form:
echo "<td><a href='pv.php?action=edit&id=" . $row['ID'] .
"'>Edit</a>&nbsp&nbsp<a href='pv.php?action=delete_ok&id=" . $row['ID'] .
"'>Delete</a>&nbsp&nbsp**<a href='pv.php?action=comment&id=" . $row['ID'] .
"'>Comment</a></td>"**;
echo "</tr>";
How can I do it?
If you want one page that allows the user to fill in a blank form, see a filled out form and update a form.
You can do the blank form or filled form by checking if an id exist, like this:
<?
if (isset(id)) // Here you can check if the id exists, which means you will be doing an update to the SQL query
echo'
<form method="post" action="pv.php?id=' . $row['ID'] . '&action=' . $form_action . '">
<!-- Your form here with values -->
e.g. <label for="cemail">Last Name</label> *
<input id="cemail" type="text" name="lastname" value="' . $row['Last_Name'] . '" maxlength="75" />
</form>';
// Put your SQL update here to take the values from the above form
}
elseif (!isset(id)) // Here there is no id so you will want to insert
{
echo'
<form method="post" action="pv.php?id=' . $row['ID'] . '&action=' . $form_action . '">
<!-- Your form here with values -->
e.g. <label for="cemail">Last Name</label> *
<input id="cemail" type="text" name="lastname" maxlength="75" /> // No value here because will insert new record
</form>';
// Put your SQL insert here to create a new record
}
?>
If you may want the user to be able to add a new comment entirely rather than editing the one already associated with their id, I think you would have to do this by giving them the option to add another comment. To do this, I would create a new table and then insert the comments into that, so you have one table, example_users with the user information, and then another table, user_comments, with the user comments, for example:
Table example_users
id = 1
fname=Joe
lname=Bloggs
email=jbloggs#example.com
Table user_comments
id=1
user_id=1
comment=Comment will be saved here
This way, any user can have any number of comments. You could render this on the page by using a foreach statement to render a text box with all their existing comments and then have a blank one at the end for any new ones. Then they can edit any comment and add a new comment.
If you want the form to be blank, then just set the value attribute to empty. E.g value=''. Except for the id field.

Categories