Save multiple field in db from dropdown selection - php

this is what i successfully done.
User key in the data into the text field and preview back the data.
this is what i failed to do:
capture back the data from db because the data that i select from drop down didn't store in the db. thats why i cannot print back the page after clicked submit.
it works well for other field but not for my drop down selection. is there somethings wrong with my code for this part?
this is my index.html file
<tr>
<td colspan="4" align="left" class="form-group form-inline"><b>Section
B</b></td>
</tr>
<script type="text/javascript">
function show() {
var dropdown = document.getElementById("employment");
var current_value =
dropdown.options[dropdown.selectedIndex].value;
if(current_value =="--Choose one--"){
didfv3.style.display = 'none';
didfv2.style.display = 'none';
}
if(current_value == "Unemployed"){
didfv3.style.display = 'none';
didfv2.style.display = '';
}
if(current_value == "Working"){
didfv3.style.display = '';
didfv2.style.display = 'none';
}
}
</script>
<td>employment</td>
<td colspan="4">
<select name="employment" id="employment" onChange="show();">
<option value="--Choose one--">--Choose one--</option>
<option value="Unemployed">Unemployed</option>
<option value="Working">Working</option>
</select>
<table id="didfv2" width="90%" border="0" align="center"
style="display:none">
<tr>
<td width="50%">Balance</td>
<td width="1%" align="center">:</td>
<td width="73%" ><input type="text" name="balance" readonly maxlength="10"
value="<?php echo $balance?>" size="5" > months</td>
</tr>
</table>
<table id="didfv3" width="90%" border="0" align="left"
style="display:none">
<tr>
<td class="form-group form-inline">Basic Salary</td>
<td width="0.5%" align="knk" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="basic" type="text" id="basic" maxlength="20"/></td>
</tr>
<tr>
<td>Gross Salary</td>
<td width="0.5%" align="center" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="gross" type="text" id="gross" maxlength="20"/></td>
</tr>
<tr>
<td class="form-group form-inline">Nett Salary</td>
<td width="0.5%" align="center" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="nett" type="text" id="nett" maxlength="20"/></td>
</tr>
<tr>
</table>
here is my insert.php
<?
$employment = $_POST['xxx'];
if (isset($_POST['xxx'])) {
$employment =$_POST['xxx'];
}
if($employment =='Working'){
$basic = $_POST['basicsalary00'];
$gross = $_POST['grosssalary00'];
$nett = $_POST['nettsalary00'];
}
elseif($xxx =='Unemployed'){
$balance = $_POST['balance'];
}
$sqlinsert = "INSERT INTO application
(
xxx,basicsalary00,grosssalary00,nettsalary00)
VALUES(0,'$xxx','$basic','$gross','$nett')";
?>
// here is to print back the output
<form name="formName" action="form1" method="post" class="form-group form-
inline">
<tr>
<td rowspan="2">Employment</td>
<td colspan="3">
<option value="<?php echo $employment;?>"><?php echo $employment;?>
</option>
<table id="didfv2" width="90%" border="0" align="center"
style="display:none">
<tr>
<td colspan="3">
<input type="hidden" id="balance" value="<?php echo $balance;?>" />
</td>
</tr>
</table>
<table id="didfv3" width="90%" border="0" align="center"
style="display:none">
<tr></tr>
<tr>
<td>Basic Salary</td>
<td>
<input name="basic" type="hidden" id="basic" value="<?php echo $basic;?>" />RM <?php echo $basic;?></td>
</tr>
<tr>
<td>Gross Salary</td>
<td>
<input name="gross" id="gross" type="hidden" value="<?php echo $gross;?>"
/>RM <?php echo $gross;?></td>
</tr>
<tr>
<td>Nett Salary</td>
<td>
<input name="nett" type="hidden" id="nett" value="<?php echo $nett;?>"
/>RM <?php echo $nett;?></td>
</tr>
</table>
</td>
</tr>
</form>
So there will be 4 pages,
1) for user input (index.html)
2) Preview back the input (i didnt include the file in here - successful)
3) Insert the input to db (insert.php)
4) Print back the result (code at the bottom of the insert.php...fetch from db- which i failed. Working for other field but not for drop down section)
I really need your help.. i've been stuck here for a week.... thank you

A proper dropdown should have a <select> and the <option> elements should be inside it!
In your insert.php, I can see that you have only used this line:
<option value="<?php echo $employment;?>"><?php echo $employment;?></option>
How about your try it like this:
<select readonly>
<option value="--Choose one--">--Choose one--</option>
<option <?php echo ($employment == 'Unemployed') ? 'selected' : ''; ?> value="Unemployed">Unemployed</option>
<option <?php echo ($employment == 'Working') ? 'selected' : ''; ?> value="Working">Working</option>
</select>
Here, we are checking whether any of those <option> element is same as the one that the user has submitted. If so, make it as selected
Hope it helps.
EDIT
If you want to store the value of $employment to display it later, you definitely have to store it in your db.

Related

Preserve form values from page to page

I have two PHP pages from one I input the year and after submitting year value goes in next page via form input as in image enter image description here
image just after clicking the submit button of first page
code for the first page is
<div class="col-sm-12" style="background-color:lavender;">
<form method="post" action="next.php">
<table id="emi" width="100%">
<tr>
<td width="100%">
<div align="center"><b>
<h3> Enter Fiancial Information:</h3>
</b> </td>
<table width="100%" border="0" id="emi">
<tr>
<td><strong>First Calender Year of DATA! ( i.e. 1999 ) </strong></td>
<td><input type="text" class="form-control" placeholder="Enter the First Assesment Year" name="year" pattern="\d*" maxlength="4"></td>
<td><input type="submit" value="Go" name="submit"class="btn btn-success"></td>
</tr></table>
</table></form>
</table>
I have two PHP pages from one I input the year and after submitting year value goes in next page via form input as in image enter image description here
In next page I have one form input also and after input the values in next page and after clicking compute button the value of year which we call from page one disappear. As shown in image
Image after clicking the compute button on next.php
code for the next.php which is called after the first page is
<?php
error_reporting(0);
$year=$_REQUEST['year'];
$x=$year+1;
$y=$x+1;
?>
<table id="emi"width="100%">
<tr>
<td width="40%"><strong>INCOME STATEMENT
</strong></td>
<td width="20%"><strong> 31-03-<?php echo $year;?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $x?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $y?></strong></td>
</tr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="form-group">
<table id="emi" width="100%">
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a1" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a2" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
</table>
<table id="emi" width="100%">
<tr>
<td colspan="3">
<center>
<input type="submit" value="Compute" name="submit"class="btn btn-success"></center>
</td>
</tr>
</table>
</form>
<?php
error_reporting(0);
if($_SERVER['REQUEST_METHOD']=="POST"){
$a=$_POST['a1'];
$b=$_POST['a2'];
echo $a+$b;
}?>
I have only call the value of year then plus one in it as you see 2000 2001 2002 to next page and when I enter the value of sundry it disappears the value of year and we get only plus means value of year become 0 after clicking the compute button.
When you submit the form on the second page via your "Compute" button $_REQUEST['year'] is no longer set because it was from the previous request.
One way around this would be to add a hidden input field which stores the $_REQUEST['year'] value for the next request.
Something like this should work:
<input type="hidden" name="year" value="<? echo $_REQUEST['year']; ?>">
replace your next.php with this code
<?php
error_reporting(0);
$year=$_REQUEST['year'];
if($year==null){
$year=$_POST['year'];
}
$x=$year+1;
$y=$x+1;
?>
<table id="emi"width="100%">
<tr>
<td width="40%"><strong>INCOME STATEMENT
</strong></td>
<td width="20%"><strong> 31-03-<?php echo $year;?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $x?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $y?></strong></td>
</tr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="form-group">
<table id="emi" width="100%">
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a1" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a2" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
</table>
<table id="emi" width="100%">
<tr>
<td colspan="3">
<center>
<input type="hidden" id="year" name="year" value="<?php echo($year);?>">
<input type="submit" value="Compute" name="submit"class="btn btn-success"></center>
</td>
</tr>
</table>
</form>
<?php
error_reporting(0);
if($_SERVER['REQUEST_METHOD']=="POST"){
$a=$_POST['a1'];
$b=$_POST['a2'];
echo $a+$b;
}?>

Removing array [0] in SESSION array error

As a novice in this and I'm struggling with a small project.
I’m putting together a page for INTERNAL orders in a small MTB club. My problem is if there are more than 1 order in the $_SESSION['cart'] and I try to remove the first one [0], then I get “Undefined offset: 0”.
I get that is to do with that I remove the first entry and therefor it can’t be found when it lists the array. I just can’t see how I solve it, I have googled and tried different solutions without result.
Please advice on how to get forward for me..
Below code is made for this purpose only
enter code here <?php
session_start();
if(isset($_POST['submit'])){
$total = $_POST['Amount'] * $_POST['Price'];
$cart=array(
'Amount'=>$_POST['Amount'], //Amoun ordered of clothes
'Size'=>$_POST['Size'], //Size of clothes
'Price'=>$_POST['Price'], //Price of clothes
'Product_id'=>$_POST['Product_id'],//Id of clothes for DB
'Product_Name'=>$_POST['Product_Name'], //Name of clothes
'Product_Total'=>$total //Total price
);
$_SESSION['cart'][]=$cart;
}
if(isset($_GET['Reset'])){
unset($_SESSION['cart']);
header('location:demo.php');
}
if(isset($_GET['remove'])){
$do = $_GET['do'];
unset($_SESSION['cart'][$do]);
//Redirecting After Unset SESSION
header('location:demo.php');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Session Demo/Test</title>
</head>
<body>
<table width="590" border="4" cellspacing="0" cellpadding="5" align="center" >
<tbody>
<tr>
<td width="373">Name of clothes</td>
<td width="185">
Reset Session</td>
</tr>
</tbody>
</table>
<form method="POST">
<table width="590" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td colspan="4" rowspan="5" align="center"><img src="images/Bike_Jersy_short.jpg" width="200" height="200" alt=""/></td>
<td height="120" colspan="2">
Clothes description
</td>
</tr>
<tr>
<td width="112">Price:</td>
<td>300,00 Kr.</td>
</tr>
<tr>
<td width="112">Amount:</td>
<td width="116">
<input name="Amount" type="number" id="Amount" tabindex="1" value="" size="1" required >
</td>
</tr>
<tr>
<td>Size:</td>
<td>
<select name="Size" id="Size" tabindex="2" required>
<option value=""></option>
<option value="XS">XS</option>
<option value="S">S</option>
<option value="L">L</option>
<option value="XL">XL</option>
<option value="2XL">2XL</option>
<option value="3XL">3XL</option>
<option value="4XL">4XL</option>
<option value="5XL">5XL</option>
<option value="6XL">6XL</option>
</select>
</td>
</tr>
<tr>
<td>Add:</td>
<td>
<input type="submit" name="submit" id="submit" value="Add">
<input name="Price" type="hidden" id="Price" value="300">
<input name="Product_id" type="hidden" id="Product_id" value="1">
<input name="Product_Name" type="hidden" id="Product_Name" value="Name of clothes">
</td>
</tr>
</tbody>
</table>
</form>
<p></p>
<?php
if(empty($_SESSION['cart'])){
?>
<table width="898" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td>You have no items in yet </td>
</tr>
</tbody>
</table>
<?php
}
else{
?>
<table width="900" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td width="109">Name</td>
<td width="31">Amount</td>
<td width="33">Size.</td>
<td width="63">Price</td>
<td width="55">Total</td>
<td width="45">Remove</td>
</tr>
<?php for($i = 0 ; $i < count($_SESSION['cart']) ; $i++) {
?>
<tr>
<td><?php echo $_SESSION['cart'][$i]['Product_Name'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Amount'];?>
</td>
<td><?php echo $_SESSION['cart'][$i]['Size'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Price'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Product_Total'];?></td>
<td>Remove
<?php
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
Use the array_shift() function.
Like so:
$value = array_shift($_SESSION['cart']);
This will remove the first element in the array regardless of the current index. $value is the value of the element that was removed.

How to create a jquery function to prevent duplicate entries to the database

I have created a form which takes the data and inserts it in the database. onlick of create button i want the function to check database whether the entry with employee_id already exists. if exists i want it to display the data already exists do you still want to insert, i am noob in this can anybody help me with this. the form is
<form id="myForm" name="myForm" action='insert.php' method='post' >
<input type='hidden' name='st' value=0>
<table style="text-align:center; width:100%">
<tr>
<td style="text-align:right"><label>Select SE/AE:</label></td>
<td style="text-align:left">
<?php include("configs.php");
$sql = "SELECT DISTINCT seae FROM se_ae ";?>
<select name="seae">
<option value="" selected></option>
<?php foreach ($dbo->query($sql) as $row) { ?>
<option value="<?php echo $row['seae']; ?>">
<?php echo $row['seae']; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td style="text-align:right"><label>Select Brand:</label></td>
<td style="text-align:left">
<?php //include("configs.php");
$sql = "SELECT DISTINCT `brand` FROM se_ae ";?>
<select name="brand">
<option value="" selected> </option>
<?php foreach ($dbo->query($sql) as $row) { ?>
<option value="<?php echo $row['brand']; ?>">
<?php echo $row['brand']; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td style="text-align:right"><label>Select Territory:</label></td>
<td style="text-align:left">
<?php //include("configs.php");
$sql = "SELECT DISTINCT `territory` FROM se_ae ";?>
<select name="territory">
<option value="" selected></option>
<?php foreach ($dbo->query($sql) as $row) { ?>
<option value="<?php echo $row['territory']; ?>">
<?php echo $row['territory']; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td style="text-align:right"><label for="name">Employee Name:</label></td>
<td style="text-align:left">
<input type="text" id="name" name="name"/>
</td>
</tr>
<tr>
<td style="text-align:right"><label for="number">Employee ID:</label></td>
<td style="text-align:left">
<input type="text" id="number" name="number" />
</td>
</tr>
<tr>
<td style="text-align:right"><label for="email"> Email:</label></td>
<td style="text-align:left">
<input type="text" id="email" name="email" />
</td>
</tr>
<tr>
<td style="text-align:right"><label for="contact"> Contact:</label></td>
<td style="text-align:left">
<input type="text" id="contact" name="contact" />
</td>
</tr>
<tr>
<td style="text-align:right"><label for="exist"> Exist:</label></td>
<td style="text-align:left">
<input type="radio" id="exist" name="exist" value="Active"/>Active
<input type="radio" id="exist" name="exist" value="NA"/>NA
</td>
</tr>
<tr>
<td style="text-align:right" class='swMntTopMenu'>
<input style="background-color:rgb(255,213,32)" name="Reset" type="reset" value="Reset">
</td>
<td style="text-align:left" class='swMntTopMenu'>
<input style="background-color:rgb(255,213,32)" name="submit" type="submit" value="Create">
</td>
</tr>
</table>
</form>
You can use the Jquery validation
$(function () {
$("#resturantRegistration").validate({
rules: {
number: {
required: true,
remote:"user/checkEmployee"
}
},
messages: {
number: {
required: "Please enter Employee id",
remote: $.validator.format("Employee id already in use")
}
}
});
});
PHP function
function checkEmployee(){
$emailid = $_GET['number'];
if(!empty($emailid)){
$emailRes = $this->users->is_email_available($emailid);
if ($emailRes == ''){
echo 'false';
} else {
echo 'true';
}
}
}
Model function to check with database
/**
* Check if email available for registering
*
* #param string
* #return bool
*/
function is_email_available($email)
{
$this->db->select('1', FALSE);
$this->db->where('LOWER(email)=', strtolower($email));
$this->db->or_where('LOWER(new_email)=', strtolower($email));
$query = $this->db->get($this->table_name);
return $query->num_rows() == 0;
}
above code is follow the codeigniter MVC framework you can customize this in core PHP as well

Check drop down and create if statement based on that selection

I want to create a php variable depending on what was selected in a drop down.
This is the drop down:
<td width="5" rowspan="2"><select id="ddlTime">
<option selected="selected" value="perhour" name="perhour">per hour</option>
<option value="perannum" name="perannum">per annum</option>
</select></td>
Depending if "per hour" or "per annum" was selected, I want to do the following (I'm not quite sure syntax wise if this is correct and this part is on another page):
// if per hour is selected:
$result_pharmacist = $_POST["pharmacist"];
$result_dispenser = $_POST["dispenser"];
// if per annum is selected:
$user_pharmacist = $_POST["pharmacist"];
$result_pharmacist = $user_pharmacist/37.5/52;
$user_dispenser = $_POST["dispenser"];
$result_dispenser = $user_dispenser/37.5/52;
How can this be done?
Here's my full form:
<form action="<?php the_permalink(); ?>calculations" method="post">
<h2>Savings calculator</h2>
<div class="calculator-divide"></div>
<table border="0">
<tr>
<td colspan="3"><h3>Current service costs</h3></td>
</tr>
<tr>
<td width="440"><p>Pharmacist</p></td>
<td><p style="padding-left:5px!IMPORTANT;">£
<input style="width:145px!IMPORTANT;" value="22.00" type="text" name="pharmacist" />
</p></td>
<td width="5" rowspan="2"><select id="ddlTime">
<option selected="selected" value="perhour" name="perhour">per hour</option>
<option value="perannum" name="perannum">per annum</option>
</select></td>
</tr>
<tr>
<td><p>Dispenser / Technician</p></td>
<td><p style="padding-left:5px!IMPORTANT;">£
<input style="width:145px!IMPORTANT;" value="8.00" type="text" name="dispenser" />
</p></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="3"><h3>Time taken to carry out manual dispensing tasks</h3></td>
</tr>
<tr>
<td><p>Measure 50mls dose by hand including Pharmacist check</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="1" type="text" name="measure-check" />
Minute(s)</p></td>
</tr>
<tr>
<td><p>Preparing labels from dispensary system</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="0.5" type="text" name="labels" />
Minute(s)</p></td>
</tr>
<tr>
<td><p>Write up CD register</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="2" type="text" name="cd-register" />
Minute(s)</p></td>
</tr>
<tr>
<td></td>
<td colspan="3"><div class="estimate">
<input style="margin-bottom:20px;" type="submit" value="Estimate my savings" />
</div></td>
</tr>
</table>
</form>
<select id="ddlTime" name="ddlTime">
and
if( $_POST['ddlTime']=='perhour' ){
// if per hour is selected:
$result_pharmacist = $_POST["pharmacist"];
$result_dispenser = $_POST["dispenser"];
}elseif( $_POST['ddlTime']=='perannum' ){
// if per annum is selected:
$user_pharmacist = $_POST["pharmacist"];
$result_pharmacist = $user_pharmacist/37.5/52;
$user_dispenser = $_POST["dispenser"];
$result_dispenser = $user_dispenser/37.5/52;
}
I hope this help you :
<?php
if(isset ($_POST['save']))
{
$temp = $_POST['opsi'];
echo $temp;
}
?>
<html>
<body>
<form action="newEmptyPHP.php" method="POST">
<h3> Choose Your Option</h3>
<select name="opsi">
<option value=0 selected>- Customer -</option>
<option value="perannum" name="perannum">perannum</option>
<option value="perhour" name="perhour">perhour</option>
</select> <br> <br>
<input type="submit" name="save" value="Save and Commit">
</form>
</body>
</html>

PHP inserting into database

Hey guys, im trying to insert some checkboxes into a database and im pretty sure my code is correct however i keep getting the error ERROR INSERTING: Column count doesn't match value count at row 1
Basically i am adding each checkbox to a different column in my database
Here is my code
$idextra=$_POST['extras'];
$arr_num=count($idextra);
$i=0;
while ($i < $arr_num)
{
$qu="INSERT INTO bs_reservations (deodoriser,carpet,carpetrepair,furniture,tabs,urine) VALUES ('$idextra[$i]')";
$res=mysql_query($qu) or die('ERROR INSERTING: '.mysql_error());
$i++;
}
Hey guys here is the HTML for my check boxes and contact form.
`
<tr>
<td height="30" align="right" class="align_right">Your Name*: </td>
<td>
<input type="text" name="name" id="name" value="<?php echo $name?>" onchange="checkFieldBack(this)"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Phone*: </td>
<td><input type="text" name="phone" id="phone" value="<?php echo $phone?>" onchange="checkFieldBack(this)" onkeyup="noAlpha(this)"/></td>
</tr>
<tr>
<td height="30" align="right" class="align_right">E-mail*: </td>
<td><input type="text" name="email" id="email" value="<?php echo $email?>" onchange="checkFieldBack(this);"/></td>
</tr>
<tr>
<td align="right" valign="top" class="align_right">Address*: </td>
<td><textarea name="comments" id="comments" cols="15" rows="5" onchange="checkFieldBack(this)"><?php echo $comments?></textarea></td>
</tr>
<tr>
<td width="236" height="25" align="left">Drop off at:</td>
<td width="548" height="23"><select name="dropoff">
<option value="05:00" <?php echo $dropoff=="05:00"?"selected":""?>>05:00</option>
<option value="06:00" <?php echo $dropoff=="06:00"?"selected":""?>>06:00</option>
<option value="07:00" <?php echo $dropoff=="07:00"?"selected":""?>>07:00</option>
<option value="08:00" <?php echo $dropoff=="08:00"?"selected":""?>>08:00</option>
<option value="09:00" <?php echo $dropoff=="09:00"?"selected":""?>>09:00</option>
<option value="10:00" <?php echo $dropoff=="10:00"?"selected":""?>>10:00</option>
<option value="11:00" <?php echo $dropoff=="11:00"?"selected":""?>>11:00</option>
<option value="12:00" <?php echo $dropoff=="12:00"?"selected":""?>>12:00</option>
<option value="13:00" <?php echo $dropoff=="13:00"?"selected":""?>>13:00</option>
<option value="14:00" <?php echo $dropoff=="14:00"?"selected":""?>>14:00</option>
<option value="15:00" <?php echo $dropoff=="15:00"?"selected":""?>>15:00</option>
<option value="16:00" <?php echo $dropoff=="16:00"?"selected":""?>>16:00</option>
<option value="17:00" <?php echo $dropoff=="17:00"?"selected":""?>>17:00</option>
<option value="18:00" <?php echo $dropoff=="18:00"?"selected":""?>>18:00</option>
<option value="19:00" <?php echo $dropoff=="19:00"?"selected":""?>>19:00</option>
</select>
</td>
<tr>
<td height="10" align="right" class="align_right">Deodoriser: </td>
<td>
<input type="checkbox" name="extras[]" id="deodoriser" value="Deodoriser>"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Carpet Protector (5 litre): </td>
<td>
<input type="checkbox" name="extras[]" id="carpet" value="Carpet Protector (5 litre)"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Carpet Repair Tools: </td>
<td>
<input type="checkbox" name="extras[]" id="carpetrepair" value="Carpet Repair Tools"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Furniture Moving Equipment: </td>
<td>
<input type="checkbox" name="extras[]" id="furniture" value="Furniture Moving Equipment"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Furniture Tabs: </td>
<td>
<input type="checkbox" name="extras[]" id="tabs" value="Furniture Tabs"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Urine Decontamination Treatment: </td>
<td>
<input type="checkbox" name="extras[]" id="urine" value="Urine Decontamination Treatment"/>
</td>
</tr>
`
and here is my complete php code for inserting into the data base
`$idextra=$_POST['extras'];
$arr_num=count($idextra);
$i=0;
while ($i < $arr_num)
{
$qu="INSERT INTO bs_reservations (deodoriser,carpet,carpetrepair,furniture,tabs,urine) VALUES ('{$idextra[1]}','{$idextra[2]}','{$idextra[3]}','{$idextra[4]}','{$idextra[5]}','{$idextra[6]}')";
$res=mysql_query($qu) or die('ERROR INSERTING: '.mysql_error());
$i++;
}
$q="INSERT INTO bs_reservations (dateCreated, name, email, phone, comments,status,eventID, qty,dropoff) VALUES (NOW(),'".$name."','".$email."','".$phone."','".$comments."','2','".$eventID."','".$qty."','".$dropoff."')";
$res=mysql_query($q) or die("error!");
$orderID=mysql_insert_id();`
I basically want to take all the inputs that the user selects and insert them into a data base.
You have these columns:
(deodoriser,carpet,carpetrepair,furniture,tabs,urine)
And you are inserting this:
'$idextra[$i]'
That's 6 columns and 1 value. As the error says, that's not the same.
You might have meant something like this:
('{$idextra[1]}','{$idextra[2]}','{$idextra[3]}','{$idextra[4]}','{$idextra[5]}','{$idextra[6]}')
If you want to make a string out of your array beforehand, use something like this using implode
$yourString = implode("','",$idextra);
$qu="INSERT INTO bs_reservations (deodoriser,carpet,carpetrepair,furniture,tabs,urine)
VALUES ('{$yourString}')";
echo the query to be sure it's sane :)
Instead of using array for your checkboxes, give them distinct names, e.g.
<input type="checkbox" name="carpetrepair" id="carpetrepair" value="Carpet Repair Tools"/>
And then check if any of them were checked:
$options = explode(",","deodoriser,carpet,carpetrepair,furniture,tabs,urine");
$sql = "INSERT INTO bs_reservations SET ";
foreach($options as $opt){
if (isset($_POST[$opt])) {
$sql.= "`$opt`=1,";
}
}
$sql = rtrim($sql,",");

Categories