Display database data in HTML text area (PHP, MySQL) - php

I am trying to get data from my database to appear in a textbox or textarea so that it can be easily edited, ready to be submitted to the database. For some reason the data displays but not in a textarea, and therefore cant be edited. This could be a syntax issue. Can anyone help? Thank you.
My code is:
while($row = mysqli_fetch_array($result))
{
echo "<div id='item'>";
echo "<form action='updateproductmain.php' method='post'";
echo "<textarea rows='5' cols='20' name='quote' wrap='physical'>" . $row['product_name'] . "</textarea>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
echo "</div>";
}

Missing the end > on your form tag:
while($row = mysqli_fetch_array($result))
{
echo "<div id='item'>";
echo "<form action='updateproductmain.php' method='post'>";
echo "<textarea rows='5' cols='20' name='quote' wrap='physical'>" . $row['product_name'] . "</textarea>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
echo "</div>";
}

Your data probably contains characters that break the html, like < or >.
You should escape your data using htmlspecialchars():
echo "<textarea rows='5' cols='20' name='quote' wrap='physical'>" . htmlspecialchars($row['product_name']) . "</textarea>";

<form action="" method="post" enctype="multipart/form-data">
<table style="border:2px solid black;" >
<tr><td> <input type="text" name="id" value="<?php echo $edit; ?>" style="display:none;"></td><tr></tr>
<tr><td> <input type="text" name="firstname" value="<?php echo $firstname; ?>"></td><tr></tr>
<td> <input type="text" name="lastname" value="<?php echo $lastname; ?>"></td><tr></tr>
<td> <input type="text" name="contact" value="<?php echo $contact; ?>"></td><tr></tr>
<td> <textarea name="address" rows="" cols="" value="<?php echo $address; ?>"></textarea></td><tr></tr>
<tr><td><input type="file" name="image" ></td></tr>
<tr><td><input type="text" name="image" style="display:none;" value="<?php echo $image?>"></td><tr></tr>
<td> <input type="submit" name="update" value="update"></td>
</table>
</form>

Related

How to echo db value in php edit form option

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>

Layout of PHP form needs altering

Part of code:
echo "<form method ='post' action='NextFile.cgi'>";
echo "<table>";
echo "<tr><td>Date: </td></td> <input type='date' name='Date' value =".$record['Date']." autofocus required='required'
/></td></tr>";
echo "<tr><td>Time: </td></td> <input type='time' name='Time' value=".$record['Time']." autofocus required='required'
/></td></tr>";
echo "</table>";
echo "<input type='submit' name='submitname' value='Save' />";
echo "</form>";
$record is related to the mysql query.
When running the code in a web browser, the two input boxes are displayed side by side.
Below are the characters />
Below is 'Date:'
Below is 'Time:'
I am trying to alter the code so that Date: is beside the Date input box, and the same for Time below.
How can I do it?
The main problem is your </td></td> which should be </td><td>
$date = $record["Date"];
$time = $record["Time"];
<form method ="post" action="NextFile.cgi">
<table>
<tr><td>Date: </td><td><input type="date" name="Date"$date" autofocus required="required"/></td></tr>
<tr><td>Time: </td><td><input type="time" name="Time"$time" autofocus required="required"/></td></tr>
</table>"
<input type="submit" name="submitname" value="Save" />
</form>
Here's a cleaned up version of your code:
echo '<table>';
echo '<tr><td>Date: </td> <input type="date" name="Date" "value="'.$record['Date'].'" autofocus required="required" /></td></tr>';
echo '<tr><td>Time: </td> <input type="time" name="Time" value="'.$record['Time'].'" autofocus required="required"/></td></tr>';
echo '</table>';
As an explanation, I added the quotes around your values (not sure if that was causing your issue, but it's good form; and your results might have erred without it) and removed the second close row </td> as was mentioned above.
Edit: try it now

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>

update the selected row in php

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

How to echo values in forms

I am echo values by the codes below. but i want to create for each values form for saving to database.
How can i insert form inside echo ?
I am beginer in php and i want form like this and it must replace with all echo values.
----FORM----
Thumbnail url :
url :
description:
submit Button
----FORM----
this is the code
foreach($posts as $post){
if ($post){
echo $post->thumbnail . "<br />";
echo $post->url . "<br />";
echo $post->description . "<br />";
}
}
I solved how to add.
foreach($posts as $post){
if ($post){
echo '<input id="title" class="form-control" type="text" name="title" value="'.htmlspecialchars($post->thumbnail).'">';
echo '<input id="teaser" class="form-control" type="text" name="teaser" value="'.htmlspecialchars($post->Url).'">';
echo '<input id="text" class="form-control" type="text" name="text" value="'.htmlspecialchars($post->description).'">';
echo '<button class="btn btn-primary" type="submit">add</button>';
}
}
this works fine... My second question is how i can add the code below codes to inside echo in a same way ?
i tried to add same way but (if isset line ) gives error
<input type="hidden" name="mode" value="news" />
<input type="hidden" name="edit_news_submit" value="true" />
<?php if(isset($edit_news['id'])): ?>
<input type="hidden" name="id" value="<?php echo $edit_news['id']; ?>" />
<?php endif; ?>

Categories