Pulling Text Area Data from MySQL into existing HTML Form - php

I have a form that I have created for internal use. I have been able to populate the MySQL database with the data from the form. I have also created a search function that will pull the data back into the fields based on an ID key (in this case a number). All of the text fields come back into the fields, but the Text Area data does not. I go to the MySQL database, and the table fields for those Text Areas are filled in, but they don't seem to pull back into the form. Please help me if you can. Thank you.
My HTML form:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>- - = = EPC Print Order Form = = - - </title>
</head>
<body bgcolor="#DFDFD9">
<h1>Digital Print Form &nbsp &nbsp<input type="image" src="http://commonmedia.asicentral.com/180000/182101/EPC_KAPP.jpg"></h1>
<!-- form to get key detail of record in database -->
<fieldset style='width:570px; text-align:left; border-width:2px;'>
<legend><h3>Request RFQ</h3></legend>
<table bgcolor="beige" border="0" width="570px">
<tr>
<td><form name="form" method="POST" action="form1.php">
RFQ#:</td><td><input type="text" name="search"></td><td><input type="submit" value="submit"></td>
</form></tr></table>
</fieldset>
<!--------------------- Print Form Data ------------------>
<form action="mysqlTest.php" method="post" id="prayfrm" name="prayfrm" onsubmit="return checkform(this);" onreset="return confirm('Do you want to clear this form?')">
<input type="hidden" name="drfq" value="6">
<!--********** Basic Job Information for the form******************-->
<fieldset style='width:570px; text-align:left; border-width:2px;'>
<legend><h3>Job Information</h3></legend>
<table bgcolor="beige" border="0" width="570px">
<tr>
<td><label for="drepid">Sales Rep</label></td>
<td><input type="text" name="drepid" size="30" required="true"/>
</td>
<td><label>Today's Date</label></td>
<td><input type="text" name="datepicker" size="10"/></td>
</tr>
<tr>
<td><label for="repemail">Rep Email</label></td>
<td><input id="repemail" name="repemail" type="text" size="30" required="true" /></td>
<td><label>Date Needed</label></td>
<td><input id="dateneeded" name="dateneeded" type="text" size="10" required="true" /> </td>
</tr>
<tr>
<td><label for="description">Description</label></td>
<td><input id="description" name="description" type="text" size="30" required="true" /></td>
<td><label for="qty">Quantity</label></td>
<td><input id="qty" name="qty" type="text" size="10" required="true" /></td>
</tr>
<tr>
<td><label for="pgsizeh">Page Size (h) x (w)</label></td>
<td><input id="pgsizeh" name="pgsizeh" type="text" size="10" required="true" /> x <input id="pgsizew" name="pgsizew" type="text" size="13" required="true" /></td>
<td><label for="pageno">Page Number</label></td>
<td><input id="pageno" name="pageno" type="text" size="10" required="true" /></td>
</tr>
<tr>
<td><label for="stock">Stock</label></td>
<td><input id="stock" name="stock" type="text" size="10" required="true" /> &nbsp<label for="stock">Color &nbsp </label>
<input type="text" name="ink" size="6" required="true"/>
</td>
<td><label for="inknote"> &nbsp Explain </label></td>
<td><input id="inknote" name="inknote" type="text" size="10" /></td>
</tr>
<tr>
<td><label for="rfq">Referenced RFQ:</label></td>
<td><input id="rfq" name="rfq" type="text" size="10" required="true" /><td><label for="finishing">Finishing</label></td>
<td><input id="finishing" name="finishing" type="text" size="10" required="true" />
</tr>
</table></fieldset>
<!--********** Customer Information for the form******************-->
<fieldset style='width:570px; text-align:left; border-width:2px;'>
<legend><h3>Customer Information</h3></legend>
<table bgcolor="beige" border="0" width="570px">
<tr>
<td><label for="dfirstname">First Name</label></td><td><input id="dfirstname" name="dfirstname" type="text" size="31" required="true" /></td><td><label for="dlastname">&nbsp Last </label></td><td><input id="dlastname" name="dlastname" type="text" size="31" required="true" /></td>
</tr>
<tr>
<td><label for="dorganization">Organization</label></td>
<td><input id="dorganization" name="dorganization" type="text" size="31" required="true" /></td><td><label for="email">Email</label></td>
<td><input id="email" name="email" type="text" size="31" required="true" /></td>
</tr>
<tr>
<td><label for="daddress1">Address</label></td><td><input id="daddress1" name="daddress1" type="text" size="31" required="true" /><td></td><td><input id="daddress2" name="daddress2" type="text" size="31" />
</tr>
<tr>
<td><label for="dcity">City</label></td><td><input id="dcity" name="dcity" type="text" size="25" required="true" /> <label for="dstate"> State</label></td><td><input id="dstate" name="dstate" type="text" size="3" required="true" /> <td><label for="dzip"> Zip </label> <input id="dzip" name="dzip" type="text" size="27" required="true" /></tr>
<tr>
<td><label for="phone">Phone </label><td><input id="phone" name="phone" type="text" size="31" required="true" /></td><td><label for="fax">Fax </label><td><input id="fax" name="fax" type="text" size="31" required="true" /> </td>
</tr></table></fieldset>
<!--********** Delivery Information for the form******************-->
<fieldset style='width:570px; text-align:left; border-width:2px;'>
<legend><h3>Delivery Information</h3></legend>
<table bgcolor="beige" border="0" width="570px">
<tr>
<td><label for="proof">Proof?</label>&nbsp <input type="checkbox" name="proof" value="yes" required="true" />&nbsp </td><td><label for="whoproof">Proof to Whom?</label> <input id="whoproof" name="whoproof" type="text" size="44" required="true" /></td>
</tr>
<tr>
<td><label for="quote">Quote?</label>&nbsp<input type="checkbox" name="quote" value="yes" required="true" />&nbsp </td>
<td><label for="amount">Amount: $</label> <input id="amount" name="amount" type="text" size="50" required="true" /></td>
</tr>
<tr>
<td><label for="delivery">Delivery Instructions</label></td><td><textarea name="delivery" rows="4" cols="50"></textarea></tr>
<tr>
<td><label for="notes">Additional Notes</label></td><td><textarea name="notes" rows="4" cols="50"></textarea>
</tr>
<td align="right"><input name="submit" type="submit" id="submit" value="Submit" /></td>
<td align="left"><input name="Reset" type="reset" id="reset" value="Reset Form" /></table></fieldset>
</body>
</html>
My PHP File for adding fields to database
<?php
$dbhost = 'hostname';
$dbuser = 'username';
$dbpass = '*******';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'dbasename';
mysql_select_db($dbname);
// -------------------------------------------------------------------
// Field Names
// -------------------------------------------------------------------
$drepid=mysql_real_escape_string($_POST['drepid']);
$datepicker=mysql_real_escape_string($_POST['datepicker']);
$repemail=mysql_real_escape_string($_POST['repemail']);
$dateneeded=mysql_real_escape_string($_POST['dateneeded']);
$description=mysql_real_escape_string($_POST['description']);
$qty=mysql_real_escape_string($_POST['qty']);
$pgsizeh=mysql_real_escape_string($_POST['pgsizeh']);
$pgsizew=mysql_real_escape_string($_POST['pgsizew']);
$pageno=mysql_real_escape_string($_POST['pageno']);
$stock=mysql_real_escape_string($_POST['stock']);
$ink=mysql_real_escape_string($_POST['ink']);
$inknote=mysql_real_escape_string($_POST['inknote']);
$rfq=mysql_real_escape_string($_POST['rfq']);
$finishing=mysql_real_escape_string($_POST['finishing']);
$dfirstname=mysql_real_escape_string($_POST['dfirstname']);
$dlastname=mysql_real_escape_string($_POST['dlastname']);
$dorganization=mysql_real_escape_string($_POST['dorganization']);
$email=mysql_real_escape_string($_POST['email']);
$daddress1=mysql_real_escape_string($_POST['daddress1']);
$daddress2=mysql_real_escape_string($_POST['daddress2']);
$dcity=mysql_real_escape_string($_POST['dcity']);
$dstate=mysql_real_escape_string($_POST['dstate']);
$dzip=mysql_real_escape_string($_POST['dzip']);
$phone=mysql_real_escape_string($_POST['phone']);
$fax=mysql_real_escape_string($_POST['fax']);
$proof=mysql_real_escape_string($_POST['proof']);
$whoproof=mysql_real_escape_string($_POST['whoproof']);
$quote=mysql_real_escape_string($_POST['quote']);
$amount=mysql_real_escape_string($_POST['amount']);
$delivery=mysql_real_escape_string($_POST['delivery']);
$notes=mysql_real_escape_string($_POST['notes']);
$sql = "INSERT INTO epc_print (drepid, datepicker, repemail, dateneeded, description, qty, pgsizeh, pgsizew, pageno, stock, ink, inknote, rfq, finishing, dfirstname, dlastname, dorganization, email, daddress1, daddress2, dcity, dstate, dzip, phone, fax, proof, whoproof, quote, amount, delivery, notes) values ('$drepid', '$datepicker', '$repemail', '$dateneeded', '$description', '$qty', '$pgsizeh', '$pgsizew', '$pageno', '$stock', '$ink', '$inknote', 'rfq', '$finishing', '$dfirstname', '$dlastname', '$dorganization', '$email', '$daddress1', '$daddress2', '$dcity', '$dstate', '$dzip', '$phone', '$fax', '$proof', '$whoproof', '$quote', '$amount', '$delivery', '$notes')";
mysql_query($sql) or die(mysql_error());
echo "
<code><pre></code>";
echo "POST:";
print_r($_POST);
echo "<code></pre></code>
";
echo "1 record added";
?>
Then my php file to retrieve data:
<?php
$connection = mysql_connect('hostname','username','*****') or die ("Couldn't connect to server.");
$db = mysql_select_db('databasename', $connection) or die ("Couldn't select database.");
$search=$_POST['search'];
$data = 'SELECT * FROM `epc_print` WHERE `drfq` = "'.$search.'"';
$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
$data2 = mysql_fetch_array($query);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>- - = = EPC Print Form = = - - </title>
</head>
<body bgcolor="#DFDFD9">
<h1>Digital Print Form &nbsp &nbsp<input type="image" src="http://commonmedia.asicentral.com/180000/182101/EPC_KAPP.jpg"></h1>
<!-- form to get key detail of record in database -->
<fieldset style='width:570px; text-align:left; border-width:2px;'>
<legend><h3>Request RFQ</h3></legend>
<table bgcolor="beige" border="0" width="570px">
<tr>
<td><form name="form" method="POST" action="form1.php">
RFQ#:</td><td><input type="text" name="search"></td><td><input type="submit" value="submit"></td>
</form></tr></table>
</fieldset>
<!--------------------- Print Form Data ------------------>
<form action="form2.php" method="post" id="prayfrm" name="prayfrm" onsubmit="return checkform(this);" onreset="return confirm('Do you want to clear this form?')">
<input type="hidden" name="drfq" value="6">
<!--********** Basic Job Information for the form******************-->
<fieldset style='width:570px; text-align:left; border-width:2px;'>
<legend><h3>Job Information</h3></legend>
<table bgcolor="beige" border="0" width="570px">
<tr>
<td><label for="drepid">Sales Rep</label></td>
<td><input id="drepid" name="drepid" type="text" size="30" value="<?php echo htmlspecialchars ($data2[drepid])?>" required="true" /></td>
</td>
<td><label>Today's Date</label></td>
<td><input id="datepicker" name="datepicker" type="text" size="10" value="<?php echo htmlspecialchars ($data2[datepicker])?>" required="true" /></td>
</tr>
<tr>
<td><label for="repemail">Rep Email</label></td>
<td><input id="repemail" name="repemail" type="text" size="30" value="<?php echo htmlspecialchars ($data2[repemail])?>" required="true" /></td>
<td><label>Date Needed</label></td>
<td><input id="dateneeded" name="dateneeded" type="text" size="10" value="<?php echo htmlspecialchars ($data2[dateneeded])?>" required="true" /></td></td>
</tr>
<tr>
<td><label for="description">Description</label></td>
<td><input id="description" name="description" type="text" size="30" value="<?php echo htmlspecialchars ($data2[description])?>" required="true" /></td>
<td><label for="qty">Quantity</label></td>
<td><input id="qty" name="qty" type="text" size="10" value="<?php echo htmlspecialchars ($data2[qty])?>"required="true" /></td>
</tr>
<tr>
<td><label for="pgsizeh">Page Size (h) x (w)</label></td>
<td><input id="pgsizeh" name="pgsizeh" type="text" size="10" value="<?php echo htmlspecialchars ($data2[pgsizeh])?>" required="true" /> x <input id="pgsizew" name="pgsizew" type="text" size="14" value="<?php echo htmlspecialchars ($data2[pgsizew])?>" required="true" /></td>
<td><label for="pageno">Page Number</label></td>
<td><input id="pageno" name="pageno" type="text" size="10" value="<?php echo htmlspecialchars ($data2[pageno])?>" required="true" /></td>
</tr>
<tr>
<td><label for="stock">Stock</label></td>
<td><input id="stock" name="stock" type="text" size="10" value="<?php echo htmlspecialchars ($data2[stock])?>" required="true" /> <label for="ink">Color </label> <input id="ink" name="ink" type="text" size="10" value="<?php echo htmlspecialchars ($data2[ink])?>" required="true" /></td>
<td><label for="inknote"> &nbsp Explain </label></td>
<td><input id="inknote" name="inknote" type="text" size="10" value="<?php echo htmlspecialchars ($data2[inknote])?>"/></td>
</tr>
<tr>
<td><label for="rfq">Referenced RFQ:</label></td>
<td><input id="rfq" name="rfq" type="text" size="10" value="<?php echo htmlspecialchars ($data2[rfq])?>" required="true"/></td> <td><label for="finishing">Finishing</label></td>
<td><input id="finishing" name="finishing" type="text" size="10" value= "<?php echo htmlspecialchars ($data2[finishing])?>" required="true" />
</tr>
</table></fieldset>
<!--********** Customer Information for the form******************-->
<fieldset style='width:570px; text-align:left; border-width:2px;'>
<legend><h3>Customer Information</h3></legend>
<table bgcolor="beige" border="0" width="570px">
<tr>
<td><label for="dfirstname">First Name</label></td><td><input id="dfirstname" name="dfirstname" type="text" size="31" value="<?php echo htmlspecialchars ($data2[dfirstname])?>" required="true" /></td><td><label for="dlastname">&nbsp Last </label></td><td><input id="dlastname" name="dlastname" type="text" size="31" value= "<?php echo htmlspecialchars ($data2[dlastname])?>"required="true" /></td>
</tr>
<tr>
<td><label for="dorganization">Organization</label></td>
<td><input id="dorganization" name="dorganization" type="text" size="31" value="<?php echo htmlspecialchars ($data2[dorganization])?>" required="true" /></td><td><label for="email">Email</label></td>
<td><input id="email" name="email" type="text" size="31" value="<?php echo htmlspecialchars ($data2[email])?>" required="true" /></td>
</tr>
<tr>
<td><label for="daddress1">Address</label></td><td><input id="daddress1" name="daddress1" type="text" size="31" value="<?php echo htmlspecialchars ($data2[daddress1])?>" required="true" /><td></td><td><input id="daddress2" name="daddress2" type="text" size="31" value="<?php echo htmlspecialchars ($data2[daddress2])?>"/>
</tr>
<tr>
<td><label for="dcity">City</label></td><td><input id="dcity" name="dcity" type="text" size="25" value="<?php echo htmlspecialchars ($data2[dcity])?>" required="true" /> <label for="dstate"> State</label></td><td><input id="dstate" name="dstate" type="text" size="3" value="<?php echo htmlspecialchars ($data2[dstate])?>" required="true" /> <td><label for="dzip"> Zip </label> <input id="dzip" name="dzip" type="text" size="27" value="<?php echo htmlspecialchars ($data2[dzip])?>" required="true" /></tr>
<tr>
<td><label for="phone">Phone </label><td><input id="phone" name="phone" type="text" size="31" value="<?php echo htmlspecialchars ($data2[phone])?>" required="true" /></td><td><label for="fax">Fax </label><td><input id="fax" name="fax" type="text" size="31" value="<?php echo htmlspecialchars ($data2[fax])?>"/> </td>
</tr></table></fieldset>
<!--********** Delivery Information for the form******************-->
<fieldset style='width:570px; text-align:left; border-width:2px;'>
<legend><h3>Delivery Information</h3></legend>
<table bgcolor="beige" border="0" width="570px">
<tr>
<td><label for="proof">Proof?</label>&nbsp <input type="checkbox" name="proof" value="<?php echo ($data2[proof])?>" />&nbsp </td><td><label for="whoproof">Proof to Whom?</label> <input id="whoproof" name="whoproof" type="text" size="44" value="<?php echo htmlspecialchars ($data2[whoproof])?>"required="true" /></td>
</tr>
<tr>
<td><label for="quote">Quote?</label>&nbsp<input type="checkbox" name="quote" value="<?php echo htmlspecialchars ($data2[quote])?>" required="true" />&nbsp </td>
<td><label for="amount">Amount: $</label> <input id="amount" name="amount" type="text" size="50" value="<?php echo htmlspecialchars ($data2[amount])?>" required="true" /></td>
</tr>
<tr>
<td><label for="delivery">Delivery Instructions</label></td><td><textarea name="delivery" rows="4" cols="50" value="<?php echo htmlspecialchars ($data2[delivery])?>" /></textarea></tr>
<tr>
<td><label for="notes">Additional Notes</label></td><td><textarea name="notes" rows="4" cols="50" value="<?php echo htmlspecialchars ($data2[notes])?>"/></textarea>
</tr>
<td align="right"><input name="submit" type="submit" id="submit" value="Submit" /></td>
<td align="left"><input name="Reset" type="reset" id="reset" value="Reset Form" /></table></fieldset>
</body>
</html>

The default value of a text area is provided by the text node inside the element, not a value attribute.
<textarea>Default value
Which can have new lines</textarea>

value isn't a valid attribute for textareas, you need to use <textarea>value</textarea>

Related

how to update multiple checkbox

I want to update the subject and other fields by adding multiple subjects, but the issue is it only saves one of subject which I have checked.
How can I solve this issue ?
Below is my code :
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$host="localhost";//host name
$username="root"; //database username
$word="";//database word
$db_name="tuichk";//database name
$tbl_name="data"; //table name
$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string
$id=$_REQUEST['id'];
$name =$_REQUEST['name'];
$stu_ic = $_REQUEST['stu_ic'];
$address = $_REQUEST['address'];
$contact = $_REQUEST['contact'];
$checkbox1=$_REQUEST['subject'];
$chk="";
$update="update data set name='".$name."', stu_ic='".$stu_ic."', address='".$address."', contact='".$contact."', sub='".$checkbox1."' where id='".$id."'";
mysql_query($update) or die(mysql_error());
$status = "Record Updated Successfully. </br></br><a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
this is my html form
<form name="form" method="post" action="">
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['id'];?>" />
<p><input type="text" name="name" placeholder="Enter Name" required value="<?php echo $row['name'];?>" /><input type="text" name="stu_ic" placeholder="Enter Student IC" required value="<?php echo $row['stu_ic'];?>" /></p>
<p><input type="text" name="address" placeholder="Enter Address" required value="<?php echo $row['address'];?>" /><input type="text" name="contact" placeholder="Enter Contact" required value="<?php echo $row['contact'];?>" /></p>
<div style="text-align:center">
<div style="width:400px;border-radius:6px;margin:0px auto">
<table border="1">
<tr>
<td colspan="2">Select Subject:</td>
</tr>
<tr>
<td>Bahasa Melayu</td>
<td><input type="checkbox" name="subject" value="Bahasa Melayu"></td>
</tr>
<tr>
<td>English</td>
<td><input type="checkbox" name="subject" value="English"></td>
</tr>
<tr>
<td>Mathematics</td>
<td><input type="checkbox" name="subject" value="Mathematics"></td>
</tr>
<tr>
<td>Science</td>
<td><input type="checkbox" name="subject" value="Science"></td>
</tr>
<tr>
<td>Sejarah</td>
<td><input type="checkbox" name="subject" value="Sejarah"></td>
</tr>
<tr>
<td>Geography</td>
<td><input type="checkbox" name="subject" value="Geography"></td>
</tr>
<tr>
<td>Additional Mathematics</td>
<td><input type="checkbox" name="subject" value="Additional Mathematics"></td>
</tr>
<tr>
<td>Chemistry</td>
<td><input type="checkbox" name="subject" value="Chemistry"></td>
</tr>
<tr>
<td>Physics</td>
<td><input type="checkbox" name="subject" value="Physics"></td>
</tr>
<tr>
<td>Biology</td>
<td><input type="checkbox" name="subject" value="Biology"></td>
</tr><tr>
<td>Principle Of Accounting</td>
<td><input type="checkbox" name="subject" value="Principle Of Accounting"></td>
</tr><tr>
<td>Ekonomi Asas</td>
<td><input type="checkbox" name="subject" value="Ekonomi Asas"></td>
</tr><tr>
<td>Perdagangan</td>
<td><input type="checkbox" name="subject" value="Perdagangan"></td>
</tr>
</table>
</div>
</form>
HTML input should be
<input type="checkbox" name="subject[]" value="Subject1">
<input type="checkbox" name="subject[]" value="Subject2">
In PHP have many options.
Option 1:
save subjects as string
$subjects = implode(',', $_POST['subject']);
retrieve as string and convert to array
$subjects = explode(',', $field);
Option 2: can save as JSON and retrieve as JSON and decode it.
this is my code inc html form:
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$host="localhost";//host name
$username="root"; //database username
$word="";//database word
$db_name="tuichk";//database name
$tbl_name="data"; //table name
$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string
$id=$_REQUEST['id'];
$name =$_REQUEST['name'];
$stu_ic = $_REQUEST['stu_ic'];
$address = $_REQUEST['address'];
$contact = $_REQUEST['contact'];
$checkbox1=$_REQUEST['subject'];
$subjects = implode(',', $_POST['subject']);
$subjects = explode(',', $field);
$chk="";
$update="update data set name='".$name."', stu_ic='".$stu_ic."', address='".$address."', contact='".$contact."', sub='".$checkbox1."' where id='".$id."'";
mysql_query($update) or die(mysql_error());
$status = "Record Updated Successfully. </br></br><a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
<div>
<form name="form" method="post" action="">
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['id'];?>" />
<p><input type="text" name="name" placeholder="Enter Name" required value="<?php echo $row['name'];?>" /><input type="text" name="stu_ic" placeholder="Enter Student IC" required value="<?php echo $row['stu_ic'];?>" /></p>
<p><input type="text" name="address" placeholder="Enter Address" required value="<?php echo $row['address'];?>" /><input type="text" name="contact" placeholder="Enter Contact" required value="<?php echo $row['contact'];?>" /></p>
<div style="text-align:center">
<div style="width:400px;border-radius:6px;margin:0px auto">
<table border="1">
<tr>
<td colspan="2">Select Subject:</td>
</tr>
<tr>
<td>Bahasa Melayu</td>
<td><input type="checkbox" name="subject[]" value="Bahasa Melayu"></td>
</tr>
<tr>
<td>English</td>
<td><input type="checkbox" name="subject[]" value="English"></td>
</tr>
<tr>
<td>Mathematics</td>
<td><input type="checkbox" name="subject[]" value="Mathematics"></td>
</tr>
<tr>
<td>Science</td>
<td><input type="checkbox" name="subject[]" value="Science"></td>
</tr>
<tr>
<td>Sejarah</td>
<td><input type="checkbox" name="subject[]" value="Sejarah"></td>
</tr>
<tr>
<td>Geography</td>
<td><input type="checkbox" name="subject[]" value="Geography"></td>
</tr>
<tr>
<td>Additional Mathematics</td>
<td><input type="checkbox" name="subject[]" value="Additional Mathematics"></td>
</tr>
<tr>
<td>Chemistry</td>
<td><input type="checkbox" name="subject[]" value="Chemistry"></td>
</tr>
<tr>
<td>Physics</td>
<td><input type="checkbox" name="subject[]" value="Physics"></td>
</tr>
<tr>
<td>Biology</td>
<td><input type="checkbox" name="subject[]" value="Biology"></td>
</tr><tr>
<td>Principle Of Accounting</td>
<td><input type="checkbox" name="subject[]" value="Principle Of Accounting"></td>
</tr><tr>
<td>Ekonomi Asas</td>
<td><input type="checkbox" name="subject[]" value="Ekonomi Asas"></td>
</tr><tr>
<td>Perdagangan</td>
<td><input type="checkbox" name="subject[]" value="Perdagangan"></td>
</tr>
</table>
</div>
</form>
<p><input name="submit" type="submit" value="Update" /></p>
</form>
<?php } ?>

i need to insert array values into mysql php table, where separated with comma in a single row

This is my table. I am developing billing application in core php mysql. Each row insert into table means need more memory, so I need to insert an one customer bill in a single row with all values separated by comma,
<table id="options-table" border="1">
<tbody><tr>
<th>S.NO</th>
<th>Product Name</th>
<th>Qty</th>
<th>Rate ($)</th>
<th>Total ($)</th>
<th>Option</th>
</tr>
<tr>
<td><span id="snum">1.</span></td>
<td><input type="text" name="productname[]" required="" class="ui-wizard-content valid"></td>
<!--<td><input type="text" style="width:80px;" name="itemcode" /></td> -->
<td><input type="text" class="qty ui-wizard-content valid" name="qty[]" required=""></td>
<td><input type="text" class="rate ui-wizard-content valid" name="rate[]" required=""></td>
<td><input type="text" class="tcost ui-wizard-content valid" name="tcost[]" readonly=""></td>
<td><input type="button" class="del btn-danger ui-wizard-content" value="Delete"></td>
</tr>
<tr>
<td><span id="snum">2.</span></td>
<td><input type="text" name="productname[]" required="" class="ui-wizard-content valid"></td>
<!--<td><input type="text" style="width:80px;" name="itemcode" /></td> -->
<td><input type="text" class="qty ui-wizard-content valid" name="qty[]" required=""></td>
<td><input type="text" class="rate ui-wizard-content valid" name="rate[]" required=""></td>
<td><input type="text" class="tcost ui-wizard-content" name="tcost[]" readonly=""></td>
<td><input type="button" class="del btn-danger" value="Delete"></td>
</tr><tr><td><span id="snum3">3.</span></td><td><input type="text" name="productname[]" required="" class="valid"></td> <td><input type="text" class="qty valid" name="qty[]" required=""></td> <td><input type="text" class="rate valid" name="rate[]" required=""></td> <td><input type="text" class="tcost" name="tcost[]" readonly=""></td><td><input type="button" class="del btn-danger" value="Delete"></td></tr><tr><td><span id="snum3">4.</span></td><td><input type="text" name="productname[]" required="" class="valid"></td> <td><input type="text" class="qty valid" name="qty[]" required=""></td> <td><input type="text" class="rate valid" name="rate[]" required=""></td> <td><input type="text" class="tcost" name="tcost[]" readonly=""></td><td><input type="button" class="del btn-danger" value="Delete"></td></tr><tr><td><span id="snum3">6.</span></td><td><input type="text" name="productname[]" required="" class="valid"></td> <td><input type="text" class="qty valid" name="qty[]" required=""></td> <td><input type="text" class="rate valid" name="rate[]" required=""></td> <td><input type="text" class="tcost valid" name="tcost[]" readonly=""></td><td><input type="button" class="add btn-success" value="Add More"></td></tr>
</tbody></table>
In the above table array values are insert into single row, PRODUCT NAME are separated with comma(,) same as QTY, RATE and TOTAL, can you suggest me.
Table structure:
S.NO Product Name Qty Rate ($) Total ($)
1. flowers 5 5 35
2. jasmine 6 5 30
3. rose 10 8 80
4. marigold 15 9 135
6.
This is example one , i tried this code work fine.
<?php
include 'connection/db_connection.php';
if(isset($_REQUEST['save'])) {
$product_name = implode(",", $_REQUEST["product"]);
print $product_name;
}
$query="insert into product (product_name) values('".$product_name."')";
$sql=mysql_query($query);
if($sql)
echo "success";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form name="myform" method="post">
<input type="text" name="product[]" value="rose">
<input type="text" name="product[]" value="garlands">
<input type="text" name="product[]" value="marigold">
<input type="text" name="product[]" value="jasmine">
<input type="text" name="product[]" value="jasmine garlands">
<input type="submit" name="save" value="Save">
</form>
</body>
</html>

How to insert bulk data with same name field in php mysql

I had tried to insert bulk data with same name field contains multiple rows. But only single row is inserted.
How to insert bulk data as different values to insert into the database.
INSERT INTO table_name (username, luck_number, test, tester) VALUES (('$username', '$luck_number', '$test', '$tester').
<tr>
<td>1</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
#nisha,In your scenario only single row is inserted because variables are same name so it's overridden, Please try below code, It will give you array of fields so you can easily create for-loop & do multiple insert with your query.
<form method="post" name="userdata">
<tr>
<td>1</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<input type="submit" name="submit">
</form>
**Note:**you can worry about the security issue letter. read the answer with the comment.
Store them in array by adding [ ] this in your input field
<tr>
<td>1</td>
<form action="" method="POST">
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr></td><input type="submit" name="submit" value="submit"/><tr></td>
</form>
<?php
//connect with your database
for($i=0;$i<count($_POST['luck_number']);$i++)
{
//set the value for variable
$luck_number=$_POST['luck_number'][$i];
$test=$_POST['test'][$i];
$tester=$_POST['tester'][$i];
//run your query
//INSERT INTO table_name (username, luck_number, test, tester) VALUES (('$username', '$luck_number', '$test', '$tester').
}
First of all change all field names by adding [] at the end.
Second step, to parsing all values you may use something like this
for($i=0; $i < $count($_GET['username']); $i++)
{
$username = $_GET['username'][$i];
$luck_number= $_GET['luck_number'][$i];
$big= $_GET['big'][$i];
$test= $_GET['test'][$i];
$tester= $_GET['tester'][$i];
// insert into database
}
The reason of inserting single row instead of multiple rows is your input field name. You are using same name in different input field so when the server gets the reply it replace the duplicate name and the last occurrence is outputted.
The thing you have to do is to use array. If you use known number of rows then you can simply use a for loop to insert data.
<tr>
<td>1</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<?php
for ($i=0; $i<count($_POST['username']); $i++)
{
mysql_query("INSERT INTO table_name (`username`, `luck_number`, `test`, `tester`) VALUES (('".$_POST['username'][$i]."', '".$_POST['luck_number'][$i]."', '".$_POST['test'][$i]."', '".$_POST['tester'][$i]."')");
}
?>
Note: Sanitizing variable is always been a good practice and strongly recommended.

html / php code validation error

I keep getting errors while trying to validate this code.
Error message examples:
e.g. 1 - Line 152, Column 42: Unescaped '<' not allowed in attributes values …>
<input type="text" name="Name" value="<?php echo htmlspecialchars($Name); ?>"…
e.g. 2 - Line 152, Column 42: attributes construct error …>
<input type="text" name="Name" value="<?php echo htmlspecialchars($Name); ?>"…
e.g. 3 - Line 152, Column 42: Couldn't find end of Start Tag input line 152 …>
<input type="text" name="Name" value="<?php echo htmlspecialchars($Name); ?>"…
The same errors occur for any lines that contains the line e.g < php echo htmlspecialchars($...)
Please let me know what I am doing wrong.
<!-- Form-->
<form name="editcontact" method="post" action="">
<table border="1" cellpadding="2">
<caption>Edit Contact</caption>
<!--ID Input-->
<tr><td><?php if ($ContactID != '') { ?>
<input type="hidden" name="ContactID" value="<?php echo htmlspecialchars($ContactID); ?>" />
<?php } ?></td></tr>
<!--Name Input-->
<tr>
<td><label for="Name">Name</label></td>
<td><input type="text" name="Name" value="<?php echo htmlspecialchars($Name); ?>" size="30" maxlength="50" tabindex="1"/></td>
</tr>
<!-- Address Input-->
<tr>
<td><label for="Address">Address</label></td>
<td><textarea name="Address" cols="45" rows="5" tabindex="2"><?php echo htmlspecialchars($Address); ?> </textarea></td>
</tr>
<!--Phone Input-->
<tr>
<td><label for="Phone">Phone</label></td>
<td><input type="text" name="Phone" value="<?php echo htmlspecialchars($Phone); ?>" size="20" maxlength="20" tabindex="3" /> </td>
</tr>
<!--Mobile Input-->
<tr>
<td><label for="Mobile">Mobile</label></td>
<td><input type="text" name="Mobile" value="<?php echo htmlspecialchars($Mobile); ?>" size="20" maxlength="20" tabindex="4" /> </td>
</tr>
<!--Email Input-->
<tr>
<td><label for="Email">Email</label></td>
<td><input type="text" name="Email" value= "<?php echo htmlspecialchars($Email); ?>" size="30" maxlength="50" tabindex="5" /></td>
</tr>
<!--Submit Button-->
<tr>
<td colspan="2" align="center"><input type="submit" name="Submit" value="Submit" onclick="return validateForm()" tabindex="6"/>
</td>
</tr>
</table>
</form>
Run the PHP and validate the HTML output. Don't try to validate PHP as if it was HTML.

Can't put PHP form in a table

I've created a form which works fine, but when I put it in a table it always thinks the form is incomplete - is there a way I can put it in a table (so it looks nice!) without this problem?
-----My HTML------
<h2 class="green">Interested in making life easier?</h2>
<form method="post" action="sendemail.php">
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>
<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
Name:
<input type="text" name="visitor" size="25" />
<br />
Address:<input type="text" name="visitoradd" size="25" />
<br />
City:<input type="text" name="visitorcity" size="15" />
<br />
Postcode:<input type="text" name="visitorpost" size="15" />
<br />
Email:<input type="text" name="visitormail" size="25" />
<br />
Telephone Number:<input type="text" name="visitortel" size="25" />
<br />
Bookkeeping/Payroll:<select name="bp" size="1">
<option value=" Bookkeeping">Bookkeeping </option>
<option value=" Payroll ">Payroll</option>
</select>
<br />
Number of transactions: <input type="text" name="transcations" size="15" />
<br />
Number of employees:<input type="text" name="employees" size="15" />
<br />
Payroll weekly/monthly:<select name="pmw" size="1">
<option value=" Weekly">Weekly</option>
<option value=" Monthly">Monthly</option>
</select>
<br />
<br />
<INPUT TYPE="image" SRC="images/btnSubmit.png" ALT="Submit Form">
<br />
</form>
-----My PHP-----
<?php
$visitor = $_POST['visitor'];
$visitoradd = $_POST['visitoradd'];
$visitorcity= $_POST['visitorcity'];
$visitorpost= $_POST['visitorpost'];
$visitormail = $_POST['visitormail'];
$visitortel = $_POST['visitortel'];
$bp = $_POST['bp'];
$transcations = $_POST['transcations'];
$employees = $_POST['employees'];
$pmw = $_POST['pmw'];
if(!$visitormail == "" && (!strstr($visitormail,"#") || !strstr($visitormail,".")))
{
echo "<h2>Please enter valid e-mail.</h2>\n";
$badinput = "<h2>Click the back button to retry.</h2>\n";
echo $badinput;
die ("Thank you.");
}
if(empty($visitor) || empty($visitormail) || empty($visitorcity) || empty($visitorpost) || empty($visitoradd) || empty($visitortel) || empty($bp) || empty($transcations) || empty($employees) || empty($pmw) || empty($pmw))
{
echo "<h2>Please fill in all fields, click the back button to retry</h2>\n";
die ("Thank you.");
}
$message = "
Name: $visitor\n
Address: $visitoradd\n
City: $visitorcity\n
Post Code: $visitorpost\n
Email: $visitormail\n
Phone Number: $visitortel\n
Bookkeeping/Payroll: $bp\n
Number of Transactions: $transcations\n
Number Of Employees: $employees\n
Payroll Weekly/Monthly: $pmw\n"
;
$subject = "Payment Details";
mail("contact#email.com",$subject,$message,$visitormail);
?>
<p align="center">
<br />
Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> )
<br />
<br />
Click here to Finish
</p>
<h2 class="green">Interested in making life easier?</h2>
<form method="post" action="sendemail.php">
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>
<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
<table>
<tr>
<td><label for="visitor">Name:</label></td>
<td><input type="text" name="visitor" size="25" /></td>
</tr>
<tr>
<td><label for="visitoradd">Address:</label></td>
<td><input type="text" name="visitoradd" size="25" /></td>
</tr>
<tr>
<td><label for="visitorcity">City:</label></td>
<td><input type="text" name="visitorcity" size="15" /></td>
</tr>
<tr>
<td><label for="visitorpost">Postcode:</label></td>
<td><input type="text" name="visitorpost" size="15" /></td>
</tr>
<tr>
<td><label for="visitormail">E-mail:</label></td>
<td><input type="text" name="visitormail" size="25" /></td>
</tr>
<tr>
<td><label for="visitortel">Telephone Number:</label></td>
<td><input type="text" name="visitortel" size="25" /></td>
</tr>
<tr>
<td><label for="bp">Bookkeeping/Payroll:</label></td>
<td>
<select name="bp" size="1">
<option value=" Bookkeeping">Bookkeeping </option>
<option value=" Payroll ">Payroll</option>
</select>
</td>
</tr>
<tr>
<td><label for="transcations">Number of transactions:</label></td>
<td><input type="text" name="transcations" size="15" /></td>
</tr>
<tr>
<td><label for="employees">Number of employees:</label></td>
<td><input type="text" name="employees" size="15" /></td>
</tr>
<tr>
<td><label for="pmw">Payroll weekly/monthly:</label></td>
<td>
<select name="pmw" size="1">
<option value=" Weekly">Weekly</option>
<option value=" Monthly">Monthly</option>
</select>
</td>
</tr>
</table>
<br />
<INPUT TYPE="image" SRC="images/btnSubmit.png" ALT="Submit Form">
<br />
</form>
Place the form tags outside of the table like so:
<form>
<TABLE>
<TR>
<TD>Name</TD>
<TD><input type="text" name="visitor" size="25" /></TD>
</TR>
<TR>
<TD>Address</TD>
<TD><input type="text" name="address" size="25" /></TD>
</TR>
</TABLE>
</form>
If you are needing to specify width, height, etc.. then use CSS.
Here is a great tutorial on it: http://www.w3schools.com/css/

Categories