Can't put PHP form in a table - php

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/

Related

submit input text on checked box

I want to submit a form which posts the input text field when the current line checkbox is checked.
When I submit, I get the productID of the checked box but all of the productLink. I want to only get the input text of the corresponding checked box.
How do I go about this?
<form action="process.php" method="post">
<table>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
</tr><tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
</tr><tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
</tr><tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
</tr><tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
</tr>
</table>
<input type="submit" name="formSubmit" value="Submit" />
</form>
You can find a lot of solution, so in my opinion you should look this one
HTML Element Array, name="something[]" or name="something"?
You can't this way, but there are some alternative method for example, when user check to checkbox then you could call a javascript function create an array and send json stringify data your php.
another way like below;
in your html
<form method="post">
<table>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
</tr>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
</tr>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
</tr>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
</tr>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
</tr>
</table>
<input type="submit" name="formSubmit" value="Submit" />
</form>
in your php (suppose in codeigniter because you tagged it)
function some_func_name(){
$product_ids = $this->input->post('productId');
$posted_product_links = $this->input->post('productLink');
$selected_links = [];
for($product_ids as $id){
$p_link = $posted_product_links[$id];
array_push($selected_links, array('id' => $id, 'link' => $p_link));
}
}
I hope this may help you..
update your by following codes
HTML
<form action="process.php" method="post">
<table>
<tr>
<td>
<input type="checkbox" name="productId[1]" value="<?=$products->id;?>" <?= set_checkbox('productId[1]', $products->id);?> />
<input type="text" name="productLink[1]" value="<?= set_value('productLink[1]');?>"/>
</td>
</tr><tr>
<td>
<input type="checkbox" name="productId[2]" value="<?=$products->id;?>" <?= set_checkbox('productId[2]', $products->id);?> />
<input type="text" name="productLink[2]" value="<?= set_value('productLink[2]');?>" />
</td>
</tr><tr>
<td>
<input type="checkbox" name="productId[3]" value="<?=$products->id;?>" <?= set_checkbox('productId[3]', $products->id);?>/>
<input type="text" name="productLink[3]" value="<?= set_value('productLink[3]');?>" />
</td>
</tr><tr>
<td>
<input type="checkbox" name="productId[4]" value="<?=$products->id;?>" <?= set_checkbox('productId[4]', $products->id);?> />
<input type="text" name="productLink[4]" value="<?= set_value('productLink[4]');?>" />
</td>
</tr><tr>
<td>
<input type="checkbox" name="productId[5]" value="<?=$products->id;?>" <?= set_checkbox('productId[5]', $products->id);?> />
<input type="text" name="productLink[5]" value="<?= set_value('productLink[5]');?>" />
</td>
</tr>
</table>
<input type="submit" name="formSubmit" value="Submit" />
</form>
Controller
public function some_func_name(){
if($this->input->post()){
$checkbox = $this->input->post('productId');
$textbox = $this->input->post('productLink');
$selected = #array_intersect_key($textbox, $checkbox);
echo '<pre>';
print_r($selected);
}
}
codes here : https://pastebin.com/UC1UFPRF
I ended up using jquery to remove the obj when they were empty.
$(document).ready(function(){
$("form").submit(function(){
$("input").each(function(index, obj){
if($(obj).val() == "") {
$(obj).remove();
}
});
});
});

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 } ?>

set_value does not work on input type file on HMVC Codeigniter

I am using HMVC Codeigniter pattern for my project. And In my project I need to set the value on form where input type of field is file.So, I feel greatful for them who help me to solve out this problem.
My associate controller is apply.php
<?php
class Career extends Controller{
function __construct(){
parent::__construct();
$this->load->Model('career_model','',TRUE);
$this->load->model('welcome/Mwelcome','',TRUE);
//session_start();
}
finction index(){
redirect('career/apply');
}
function apply(){
$this->load->library('form_validation');
$this->load->helper('url');
$this->form_validation->set_rules('fname','First Name','required|trim');
$this->form_validation->set_rules('mname','Middle Name','required|trim');
$this->form_validation->set_rules('lname','Last Name','required|trim');
$this->form_validation->set_rules('date','Date','required|trim');
$this->form_validation->set_rules('pAddress','Permanent Address','required|trim');
$this->form_validation->set_rules('cAddress','Contact Address','required|trim');
$this->form_validation->set_rules('gender','Gender','required|trim');
if (empty($_FILES['cv']['name']))
{
$this->form_validation->set_rules('cv', 'Attach Your CV', 'required');
}
if($this->form_validation->run($this)){
$_SESSION['msg']="Your form has been submitted succesfully!!";
redirect('career/apply');
}
else
{
$_SESSION['err']="Opp! There was some wrong to fill up a form so try again!!!";
redirect('career/apply');
}
}
$data=array('body'=>'apply');
$data['pgtitle']='Apply';
$this->load->view('temp',$data);
}
}
My associate view file is apply.php
<form action="<?php echo site_url()?>career/apply" method="post" enctype="multipart/form-data" onsubmit="return confirm('Do you really want to submit the form?');">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="">Name</td>
<td width="158"><label for="fname"></label>
<input type="text" name="fname" id="fname" placeholder="First Name" class='input-row' value="<?php echo set_value('fname')?>"/><span class="required"><?php echo form_error('fname');?></span></td>
<td width="158"><label for="mname"></label>
<input type="text" name="mname" id="mname" placeholder="Middle Name" class='input-row' value="<?php echo set_value('mname')?>"/></td>
<td width="158"><label for="lname"></label>
<input type="text" name="lname" id="lname" placeholder="Last Name" class='input-row' value="<?php echo set_value('lname')?>"/><span class="required"><?php echo form_error('lname');?></span></td>
</tr>
<tr>
<td>Date of Birth</td>
<td width="158"><label for="date"></label>
<input type="text" name="date" id="date" class='input-row' value="<?php echo set_value('date')?>" placeholder="yyyy-mm-dd"/><span class="required"><?php echo form_error('date');?></span></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Gender</td>
<td><div class="customSel">
<label>
<select name="gender" id="select" class='input-row' value="">
<option value="Female"<?php echo set_select('gender', 'Female', TRUE); ?>>Female</option>
<option value="Male"<?php echo set_select('gender', 'Male', TRUE); ?>>Male</option>
<option value="Please Selection"<?php echo set_select('gender', 'Please Selection', TRUE); ?>>Please Selection</option>
<?php /*?><option value="Other"<?php echo set_select('gender', 'Other', TRUE); ?>>Other</option><?php */?>
</select></label></div><span class="error"><?php echo form_error('gender')?></span></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Address</td>
<td><label for="pAddress"></label>
<input type="text" name="pAddress" id="pAddress" placeholder="Permanent Address" class='input-row' value="<?php echo set_value('pAddress')?>"/><span class="required"><?php echo form_error('pAddress');?></span></td>
<td><label for="cAddress"></label>
<input type="text" name="cAddress" id="cAddress" placeholder="Contact Address" value="<?php echo set_value('cAddress')?>"/><span class="required"><?php echo form_error('cAddress');?></span></td>
<td> </td>
</tr>
<tr>
<td>Attach Your CV</td>
<td colspan="2"><label for="cv"></label>
<input type="file" name="cv" id="cv" value="<?php echo set_value('cv')?>"/><span class="error"><?php echo form_error('cv')?></span></td>
</tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="Submit" class="Button" />
<input type="reset" name="reset" id="reset" value="Reset" class="Button" /></td>
<td> </td>
<td> </td>
</tr>
</table>
</form >

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.

how to send my value to my database

hello I want to insert my value to the database here my structure .
<tr>
<td>
<b>Vendredi</b> </br><?php echo $date6 ?>
</td>
<td>
<input size="10" type="text" id="projvendredi" name="projvendredi"onkeypress="return handleEnter6(event, this, 'task');"/>
</td>
<td>
<input type="text" name="clientvendredi" class = "client" size="12" id ="clientvendredi" disabled />
</td>
<td>
<input type="text" name="projdesvendredi" size="30" class "desc" id ="projdesvendredi"disabled />
</td>
<td>
<span id="calculTemps5">
<input id="input6" class= "temps" type="number" name="tempsv" size="2" min="0" max="24" value="0" /></br>
</span>
</td>
</tr>
and here my button
<input type="text" id="result" readonly="readonly" name="total" />
<input type="submit" value="Terminé" id="end" />
<input type="hidden" name="data" value="<?php echo $date; ?>" />
</p>
</form>
Here is my insert.php
$date = $_POST['data'] ;
if (isSet($_POST['descprojsam']))
{
$descprojsam = $_POST['descprojsam'] ;
echo($descprojsam);
}
echo($date);
// Connect to the DB
$link = mysqli_connect("localhost","root","","cruel") or die("Error " . mysqli_error($link));
// adding new recherche
if(!empty($_POST['descprojsam']))
{
$sql = " INSERT INTO `projetstaches`(`prtId`, `prtTimeSheetId`, `prtProjetNum`, `prtDate`, `prtTaskId`, `prtTime`, `prtLocation`, `prtDescription`)
([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],'$descprojsam')";
$link->query($sql);
}
?>
I've not finish my insert cause i don't know how really to do it. I have 7 days so I gave you my friday for exemple.
PHP is case sensative, isSet($_POST['descprojsam']) should be isset($_POST['descprojsam'])

Categories