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>
Related
I currently have an edit form, where all values are echoed from the db as the "values" for each input, the user would then retype and "save" the value to append to the db. All echoes work but i have been unable to echo the database value for the options... anybody knows how to?
& Thanks
Code below:
<fieldset>
<form id="input-form" method="POST" action="../php/edit-import-record.php">
Airway Bill Number*:
<input type=text name=AwbNo size=30 class="input" value="<?php echo $awb ?>" required>
Client Code:
<?php //OPEN DROP DOWN BOX
include("../login/dbinfo.inc.php");
$comm=#mysql_connect(localhost,$username,$password);
$rs=#mysql_select_db($database) or die( "Unable to select database");
$sql= "SELECT DISTINCT ClientCode, ClientName FROM tbl_client ORDER BY ClientCode";
$result = mysql_query($sql);
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."</option>";
}
echo "</select><br>"; //CLOSE DROP DOWN BOX
?><br>
Vessel Name*:
<input type=text name=VesselName class=form-control id=inputSuccess size=30 class="input" value="<?php echo $vsl ?>" required>
Number of Pieces:
<input type=number name=Pieces size=30 class="input" value="<?php echo $pcs ?>" >
Total Weight (kg):
<input type=number name=Weight size=30 class="input" value="<?php echo $wgt ?>" >
Carrier:
<input type=text name=Carrier size=30 class="input" value="<?php echo $car ?>" >
Sender:
<input type=text name=Sender size=30 class="input" value="<?php echo $snd ?>" >
Status:
<input type=text name=Status size=30 class="input" value="<?php echo $stt ?>" >
Arrival Date:
<input type=date name=ArrivalDate size=30 class="input" value="<?php echo $ard ?>" >
Customs:
<input type=text name=Customs size=30 class="input" value="<?php $ctm ?>" >
<br><small>Fields marked with * are required to be filled in.</small>
<div class="inputformbutton">
<button type="reset" class="btn btn-default btn-sm">Reset</button>
<button type="submit" class="btn btn-primary btn-sm">Create Record</button>
</div>
</fieldset>
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>';
I have this table:
emailtype:
emailtypeID emailtype
1 primary
2 secondary
3 old
I have this code to show emails in the input form:
$sql = "SELECT * from emailtype";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>' . $row["emailtype"];
echo '<input type="text" name="email[]" id="" /><br />'; }
GOAL:
I would like to repeat twice the emailtypeID = 2 or emailtype = secondary, so that I can enter two email addresses with the secondary ID.
Is it possible in the while loop?
Thanks!
SOLUTION:
For anybody who needs it, this is the new code as per the Marc B suggestion.
while($row = $result->fetch_assoc()) {
echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>' . $row["emailtype"];
echo '<input type="text" name="email[]" id="" /><br />';
if($row["emailtype"] == 'secondario'){
echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>' . $row["emailtype"];
echo '<input type="text" name="email[]" id="" /><br />';
}
}
Please use this:
while($row = $result->fetch_assoc()) {
echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>' . $row["emailtype"];
echo '<input type="text" name="email[]" id="" /><br />';
if($row["emailtype"] == 'secondario' || $row["emailtypeID"]==2){
echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>' . $row["emailtype"];
echo '<input type="text" name="email[]" id="" /><br />';
}
}
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>";
?>
I need to update the selected row in the page. I have a webpage where user as to enter the id. In the second page the all the rows and values with the same Id entered as to be displayed for editing. How can i do this..
Here is the code
<form method="post" action="edituser.php">
<label type="text" name="name" maxlength="50" size="30" class="label">Enter the Membership Number</label><br />
<input type="text" name='id' placeholder="enter Membership Number" class="input" size="40"/><br />
<span class="field">(* Required field)</span><br /><br />
<input type="submit" name="submit" value="SUBMIT" class="button"><br /><br /><br /><br />
</form>
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("anthonys");
if(isset($_POST['submit']))
{
$id= $_POST['id'];
if( ! ctype_alnum($id) )
die('invalid id');
$query = "SELECT id FROM `member` WHERE `id` =$id";
$run = mysql_query($query);
if(mysql_num_rows($run)>0){
echo "<script>window.open('edit.php?id=".$id."','_self')</script>";
}
else {
echo "<script>alert('Membership No is Invalid!')</script>";
}
}
?>
<
h2>Application for the Membership</h2><br /><br />
<table border="0px" style="border-collapse:collapse; width:810px;" align="center">
<tr>
<td>
<form name="XIForm" id="XIForm" method="POST" action="pdf/pdf2.php">
<label type="text" name="uid" maxlength="50" size="30" class="label">Membership No</label><br />
<input type="text" name="id" id="id" value="<?php if(isset($_GET['id'])) { echo $_GET['id']; } ?>" readonly> <br /><br />
<label type="text" name="fathername" maxlength="50" size="30" class="label">Father`s Name</label><br />
<input name="fathername"name="fathername" placeholder="" class="input" size="40"value="<?php if(isset($_GET['fathername'])) { echo $_GET['fathername']; } ?>"> <br /><br />
<input type="hidden" name="formType" id="formType" value="reg"/>
<input type="button" name="XISubmit" id="XISubmit" value="ADD" class="button" />
<br /><br /><br /><br />
</form></td>
</tr>
</table>
</div>
</div>
</section>
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("anthonys");
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE member SET fathername='$_POST[fathername]' WHERE id='$_POST[hidden]'";
$run=mysql_query($UpdateQuery);
};
echo "<table border=1>
<tr>
<th>Fathers_Name</th>
"</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action=edit.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=Father_name value=" . $record['fathername'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['id'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=edit.php method=post>";
echo "<tr>";
echo "<td><input type=text name=fathername></td>";
echo "</form>";
echo "</table>";
?>
</body>
</html>
You are getting only id now
$query = "SELECT * FROM `member` WHERE `id` =$id"; // will give you all the rows
you're missing a lot of quotes in your strings, you can use \" to echo a double quote without closing the string eg:
echo "<form action=\"modify.php\" method=\"post\">";
Also you shouldn't use unquoted array indexes, while technically this will work,
"SET fathername='$_POST[fathername]'"
its relying on the fact that php will fall back to a string if it can't locate the constant and will produce warnings:
Instead concatenate it like this:
"SET fathername=".$_POST['fathername']."...."
you may have some sql injection vulnerabilities in your code, you should read up on that. and use mysqli_* not mysql_* as it's depreciated