how to insert into database with multiple array in php - php

hi guys can someone help me how to this. I have here the problems I can't solve it
for example
data for manny
<input type="text" name="name[]" value="Manny" >
<input type="text" name="course[]" value="BSIT" >
<input type="text" name="level[]" value="2nd year" >
<input type="text" name="course[]" value="BSCS" >
<input type="text" name="level[]" value="3rd year" >
<input type="text" name="course[]" value="BSENG" >
<input type="text" name="level[]" value="4th year" >
data for Floyd
<input type="text" name="name[]" value="Floyd" >
<input type="text" name="course[]" value="ABM" >
<input type="text" name="level[]" value="Grade 11" >
<input type="text" name="course[]" value="STEM" >
<input type="text" name="level[]" value="Grade 12" >
Manny hold 3 classes while Floyd hold 2 classes. so how can I insert it into database I guess use an array is suit for this problem but I don't know how. please help me with this

I think it will make more sense to use an array if the structure "data for Manny" and "data for Floyd" are the same. Not like when you are having "GRADE" in "data for Floyd" and "YEAR" in "data for Manny".
Moreover, what exactly are you trying to achieve? You don't want to treat Manny and Floyd data separately?
You can have a check and see if this is going to help.
Manny Data
<input type="text" name="Manny[name]" value="Manny"><br>
<select name="Manny[year1]">
<option value="BSIT">BSIT</option>
</select><br>
<select name="Manny[year2]">
<option value="BSCS">BSCS</option>
</select><br>
<select name="Manny[year3]">
<option value="BSENG">BSENG</option>
</select><br>
Floyd Data
<input type="text" name="Floyd[name]" value="Floyd"><br>
<select name="Floyd[Grade 11]">
<option value="ABM">ABM</option>
</select><br>
<select name="Floyd[Grade 12]">
<option value="STEM">STEM</option>
</select><br>
=====RESULT=====
Array(
[Manny] => Array
(
[name] => Manny
[year1] => BSIT
[year2] => BSCS
[year3] => BSENG
)
[Floyd] => Array
(
[name] => Floyd
[Grade 11] => ABM
[Grade 12] => STEM
)
)
$_POST["Manny"] contains all Manny's data.
$_POST["Floyd"] contains all Floyd's data.

Related

Display html inputted data with PHP

Below is my HTML and PHP code. I'm trying to display my the inputted data, however, I'm getting nothing. What is wrong with my code?
HTML
<form action="welcome.php" method="POST">
<fieldset>
<legend> Personal Details: </legend>
<label for="fname>"></label>
<input type="text" name="Name" id="fname" required autofocus placeholder="First Name" pattern="[a-zA-Z]{3,}" title="Please enter more than three letters">
<label for="lname>"></label>
<input type="text" name="Last Name" id="lname" required autofocus placeholder="Last Name" pattern="[a-zA-Z]{3,}" title="Please enter more than three letters">
<label for="email">Email: </label>
<input type="text" name="email" id="email" required placeholder="Your school email" pattern="[a-zA-Z]{3,}#[a-zA-Z]{3,}[.]{1}[a-zA-Z{2} title="Please enter a valid email address>
<label for="phone">Phone: </label>
<input type="tel" name="phone" id="phone" required placeholder="Please enter in your phone number" pattern="[0-9]{4} [0-9]{3} [0-9]{3}" title="Please enter in a phone number in this format: #### ### ###">
<select name="country" required>
<option value=""> </option>
<option value="US">US</option>
<option value="UK">UK</option>
<option value="AUS">AUS</option>
</select>
</fieldset>
<br>
<fieldset>
<legend> Booking Details: </legend>
<input type="date" name="date" min="2018-10-07" max="2018-10-31">
<input type=time min=9:00 max=17:00 step=900>
<br>
<br>
<label for="dorm">Dormitory: </label>
<br>
<select name="dorm" required>Dormitory
<option value="Cypress">Cypress Hall</option>
</select>
<br>
<br>
<label for="floor">Floor: </label>
<br>
<select name="floor" required>Floor:
<option value=""></option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
</select>
<br>
<br>
<label for="roomnumber">Room Number: </label>
<br>
<select name="roomnumber">Room Number:
<option value=""></option>
<option value="22">22</option>
</select>
<br>
<br>
<label for="roomletter">Room Letter: </label>
<br>
<select name="roomletter" required="">Room Letter
<option value=""></option>
<option value="A">A</option>
<option value="B"> B</option>
</select>
<br>
<br>
<label for="bedroomcleaning">Bedroom: </label><br>
<input type="checkbox" id="box-4" onclick="checkPrice()" value="4">Dust all ceiling fans/light/fixtures within reach.<br>
<input type="checkbox" id="box-5" onclick="checkPrice()" value="5"> Change sheets and/or fold clothes if requested by client.<br>
<input type="checkbox" id="box-6" onclick="checkPrice()" value="6"> Straighten up, put toys away, make beds, fold clothes and put on bed. Straighten papers and put in a pile. DO NOT THROW AWAY ANY PERSONAL ITEMS!<br><br>
<label for="bathroomcleaning">Bathroom: </label><br>
<input type="checkbox" id="box-1" onclick="checkPrice()" value="1"> Clean bowl and wipe down toilet cover, seat, under seat, base and behind the base.<br>
<input type="checkbox" id="box-2" onclick="checkPrice()" value="2"> Clean all mirrors.<br>
<input type="checkbox" id="box-3" onclick="checkPrice()" value="3"> Clean countertops and backsplashes.<br>
<label for="bathroomprice" id="price">Total Price: </label>
<br>
<br>
<input type="submit">
</fieldset>
</form>
PHP
Welcome <?php echo $_POST["Name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
Your phone number is: <?php echo $_POST["phone"]; ?>
Check your pattern for the email input:
[a-zA-Z]{3,}#[a-zA-Z]{3,}[.]{1}[a-zA-Z{2}
[a-zA-Z]{3,}#[a-zA-Z]{3,}[.]{1}[a-zA-Z]{2}
Also:
<input type=time min=9:00 max=17:00 step=900>
<input type="time" min="9:00" max="17:00" step="900">
As said by Jon in the comments, you need a <form> div.
<form action="yourPhpPage.php" method="POST">
<label ...>...</label>
<input ... />
...
<input type="submit" value="Send" />
</form>
The action attribute tells which page to call when submitting form.
The method tells how to send data (GET or POST mainly)
The <input type="submit" /> is the submit button. When you click it the page indicated in action will be called with the values of the different inputs. The value of the submit input is what is shown on it.
Eventually you can use a submit <button> :
<button type="submit">What you want to be displayed on it.</button>
I prefer this one because it's easier to modify its style and behavior respectively with CSS and JS. But it's not the point.
Hope it helps you !Jean-Marc.

PHP Get values from Multiple form arrays

I have the following form, how can i get the data from multiple[] arrays in the form and combine them into 3 data sets eg name[], quantity[], unit[]
<input type="text" name="name[]" class="form-control" placeholder="Name" />
<input type="text" name="name[]" class="form-control" placeholder="Name" />
<input type="text" name="name[]" class="form-control" placeholder="Name" />
<input type="text" name="quantity[]" class="form-control" />
<input type="text" name="quantity[]" class="form-control" />
<input type="text" name="quantity[]" class="form-control" />
<select name="unit[]" >
<option value="">--Unit--</option>
<option value="g">g</option>
</select>
<select name="unit[]" >
<option value="">--Unit--</option>
<option value="g">g</option>
</select>
<select name="unit[]" >
<option value="">--Unit--</option>
<option value="g">g</option>
</select>
The PHP Manual at http://php.net/manual/en/tutorial.forms.php answers your question.
Example #1 A simple HTML form
>
>
>
> <form action="action.php" method="post">
> <p>Your name: <input type="text" name="name" /></p>
> <p>Your age: <input type="text" name="age" /></p>
> <p><input type="submit" /></p>
> </form>
>
>
>
There is nothing special about this form. It is a straight HTML form
with no special tags of any kind. When the user fills in this form and
hits the submit button, the action.php page is called. In this file
you would write something like this:
Example #2 Printing data from our form
>
> Hi <?php echo htmlspecialchars($_POST['name']); ?>.
> You are <?php echo (int)$_POST['age']; ?> years old.
>
>
A sample output of this script may be:
Hi Joe. You are 22 years old.
Apart from the htmlspecialchars() and (int) parts, it should be
obvious what this does.
Do var_dump($_POST) to view all results.
in $_POST['name'] (or quantity or unit) u have an array with 3 results.

php create array with key by html input

am trying to create array with key by using html input
here's my html input
<form method="post" action="">
<input type="text" name="name[][you]" value="" />
<input type="text" name="name[][he]" value="" />
<input type="text" name="name[][she]" value="" />
<input type="text" name="name[][you]" value="" />
<input type="text" name="name[][he]" value="" />
<input type="text" name="name[][she]" value="" />
<button type="submit">go</button>
</form>
my outbut is
Array ( [0] => Array ( [you] => jhon ) [1] => Array ( [he] => joy ) [2] => Array ( [she] => sarah ) [3] => Array ( [you] => samm ) [4] => Array ( [he] => petter ) [5] => Array ( [she] => susan ) )
but i want the array to be like this
Array( [0]=> array ( [you] => jhon [he] => joy [she] => sarah )[1]=> array ( [you] => pitter [he] => tom [she] => suszan ) )
is there away to do that
try like this ==>
<form method="post" action="">
<input type="text" name="name[0][you]" value="" />
<input type="text" name="name[0][he]" value="" />
<input type="text" name="name[0][she]" value="" />
<input type="text" name="name[1][you]" value="" />
<input type="text" name="name[1][he]" value="" />
<input type="text" name="name[1][she]" value="" />
<button type="submit">go</button>
</form>
OR
<form method="post" action="">
<?php $n = 2; // how many interval you want
for ($i = 0; $i < $n; $i++) {
?>
<input type="text" name="name[<?php echo $i; ?>][you]" value="" />
<input type="text" name="name[<?php echo $i; ?>][he]" value="" />
<input type="text" name="name[<?php echo $i; ?>][she]" value="" />
<?php } ?>
</form>
If you want to output an array with two children then set the keys manually, name[0] for the first three inputs and name[1] for the last three.
Everytime you write name="[][key]" php increment automatic the key.
if you write syntax like [] php increment the index of array.
Small explation
For example:
If you write an array like this
$array[] = "msg1";
$array[] = "msg2";
$array[] = "msg3";
$array length will be 2 (3 elements because it starts from 0) and it is same as
$array[0] = "msg1";
$array[1] = "msg2";
$array[2] = "msg3";
This is different from above
$array[0] = "msg1";
$array[1] = "msg2";
$array[1] = "msg3";
This array will have only 1 length (2 elements only)
Solution of your question is :
<form method="post" action="">
<input type="text" name="name[0][you]" value="" />
<input type="text" name="name[0][he]" value="" />
<input type="text" name="name[0][she]" value="" />
<input type="text" name="name[1][you]" value="" />
<input type="text" name="name[1][he]" value="" />
<input type="text" name="name[1][she]" value="" />
<button type="submit">go</button>
</form>

inserting status for four inputs from select php

I have four HTML inputs and a select option. i want to insert the status for the three inputs = "0" and for the selected one = "1". kindly help me and tell some thing about mysql query for
<input placeholder="Choice A:" name="a">
<input placeholder="Choice B:" name="b">
<input placeholder="Choice C:" name="c">
<input placeholder="Choice D:" name="d">
<select class="form-control" name="select">
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
i want the status for a is 1 and for the remaing 0 in Database :
I don't know what you want to achieve but as per your comments you are looking for this:
HTML:
<form method="post" action="">
<input type="text" placeholder="Choice A:" name="a">
<input type="text" placeholder="Choice B:" name="b">
<input type="text" placeholder="Choice C:" name="c">
<input type="text" placeholder="Choice D:" name="d">
<select class="form-control" name="select">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<input type="submit" name="submit" value="Submit Now">
</form>
PHP:
if(count($_POST) > 0){
$updateZero = array();
$updateOne = array();
$selectArr = array('A','B','C','D'); // default array
foreach ($selectArr as $key => $value) {
if($value == $_POST['select']){
$updateOne[] = $value; // store those selected
}
else{
$updateZero[] = $value; // store those not selected
}
}
echo "<pre>";
print_r($updateOne); // selected array
print_r($updateZero); // remaining array
}
Result:
Array
(
[0] => A
)
Array
(
[0] => B
[1] => C
[2] => D
)
Now you can use these array in MYSQL Statement as you need.

html form inputs array PHP

I have a problem with my cloned form:
The form has 4 inputs and I need to separate the variables and put them together by people.
My inputs:
<input type="text" name="fName[]">
<input type="text" name="lName[]">
<input type="number" name="age[]">
<input type="text" name="city[]">
My PHP:
$fNameArray = $_POST['fName'];
$lNameArray = $_POST['lName'];
$ageArray = $_POST['age'];
$cityArray = $_POST['city'];
I really do not understand how two-dimensional arrays work, to group people to complete the form, as in the following example:
first name: siddharta, last name: naranjo, age: 29, city: mexico
first name: xxxxx, last name: xxxxxx, age: xx, city: xxxx.
Some of these answers are wrong if I understand you.
First of all if you use the naming convention data[]fname then it will not group surely?
Secondly, the blank bracket will have it constantly move to a new array.
You will end up with a load of arrays with one element in them.
You will need to number or ID the arrays to keep them together.
<?php
if(isset($_POST['data'])){
$results = $_POST['data'];
echo "<pre>";
print_r($results);
echo "<pre>";
}
?>
<form action="" method="post">
<!-- User 1 -->
<input type="text" name="data[0][fName]" value="test 1">
<input type="text" name="data[0][lName]" value="test 1">
<input type="number" name="data[0][age]" value="21">
<input type="text" name="data[0][city]" value="test 1">
<br>
<!-- User 2 -->
<input type="text" name="data[1][fName]" value="test 2">
<input type="text" name="data[1][lName]" value="test 2">
<input type="number" name="data[1][age]" value="22">
<input type="text" name="data[1][city]" value="test 2">
<br>
<!-- User 3 -->
<input type="text" name="data[2][fName]" value="test 3">
<input type="text" name="data[2][lName]" value="test 3">
<input type="number" name="data[2][age]" value="23">
<input type="text" name="data[2][city]" value="test 3">
<br>
<input type="submit" value="Go">
</form>
This produces this:
Array
(
[0] => Array
(
[fName] => test 1
[lName] => test 1
[age] => 21
[city] => test 1
)
[1] => Array
(
[fName] => test 2
[lName] => test 2
[age] => 22
[city] => test 2
)
[2] => Array
(
[fName] => test 3
[lName] => test 3
[age] => 23
[city] => test 3
)
)
Which is what it looks like you want?
Your html is ok
If i understand you right take a look at this
<?php
if($_POST && isset($_POST['fName'])) {
$people = count($_POST['fName']);
for($i=0; $i<$people; $i++) {
echo "<p>first name: {$_POST['fName'][$i]}, last name: {$_POST['lName'][$i]} ...</p>";
}
}
Use keys for 2 dimensional array.
<input type="text" name="fName[0]">
<input type="text" name="lName[0]">
<input type="number" name="age[0]">
<input type="text" name="city[0]">
<input type="text" name="fName[1]">
<input type="text" name="lName[1]">
<input type="number" name="age[1]">
<input type="text" name="city[1]">

Categories