Adding Query HTML/PHPMYADMIN - php

Okay, so i really need help about the add query. I don't know what i did wrong. Please help. This is the code for my update.php
<form action="updateprocess.php" method="POST">
Description <input type="text" id="desc" name="desc">
SWL.Tonne <input type="text" id="swltonne" name="swltonne">
Inches <input type="text" id="inches" name="inches">
Model <input type="text" id="model" name="model">
SafetyFactor <input type="text" id="safetyfactor" name="safetyfactor">
Date/LPO <input type="text" id="datelpo" name="datelpo">
Manufacturer <input type="text" id="manu" name="manu">
Certificate.Number <input type="text" id="cnumber" name="cnumber">
Opening.Stock <input type="text" id="opstock" name="opstock">
January <input type="text" id="jan" name="jan">
February <input type="text" id="feb" name="feb">
March <input type="text" id="mar" name="mar">
April <input type="text" id="apr" name="apr">
May <input type="text" id="may" name="may">
June <input type="text" id="jun" name="jun">
July <input type="text" id="jul" name="jul">
August <input type="text" id="aug" name="aug">
September <input type="text" id="sep" name="sep">
October <input type="text" id="oct" name="oct">
November <input type="text" id="nov" name="nov">
December <input type="text" id="dec" name="dec">
Total.Used <input type="text" id="totuse" name="totuse">
Available.Balance <input type="text" id="avaibal" name="avaibal">
Minimumm.Stock <input type="text" id="minstocks" name="minstocks">
FOQ/FOI <input type="text" id="foqi" name="foqi">
Comment <input type="text" id="comm" name="comm">
</div>
<div class="submit-container"> <input class="submit-button"
type="submit" value="Add">
<input class="submit-button" type="submit" value="Clear" /></div>
</form>
and this is the updateprocess.php I really dont know what to do. My database name is franklinoffshore and i used mysqli
<?php
$desc = $_POST['desc'];
$swltonne = $_POST['swltonne'];
$inches = $_POST['inches'];
$model = $_POST['model'];
$safetyfactor = $_POST['safetyfactor'];
$datelpo = $_POST['datelpo'];
$manu = $_POST['manu'];
$cnumber = $_POST['cnumber'];
$opstock = $_POST['opstock'];
$jan = $_POST['jan'];
$feb = $_POST['feb'];
$mar = $_POST['mar'];
$apr = $_POST['apr'];
$may = $_POST['may'];
$jun = $_POST['jun'];
$jul = $_POST['jul'];
$aug = $_POST['aug'];
$sep = $_POST['sep'];
$oct = $_POST['oct'];
$nov = $_POST['nov'];
$dec = $_POST['dec'];
$totuse = $_POST['totuse'];
$avaibal = $_POST['avaibal'];
$minstocks = $_POST['minstocks'];
$foqi = $_POST['foqi'];
$comm = $_POST['comm'];
//connect to the serverand database
$franklinoffshore = mysqli_connect("localhost","root","","franklinoffshore");
//query the database
{
$result = mysqli_query($franklinoffshore, "INSERT INTO inventory_hooks(Description,SWLTonne,Inches,Model,SafetyFactor,DateLPO,Manufacturer,CertificateNumber,OpeningStocks,January,February,March,April,May,June,July,August,September,October,November,December,TotalUsed,AvailableBalance,MinimumStocks,FoqFoi,Comment)
VALUES('$description','$swltonne','$inches','$model','$safetyfactor','$datelpo','$manufacturer','$cnumber','$opstock','$jan','$feb','$mar','$apr','$may','$jun','$jul','$aug','$sep','$oct','$nov','$dec','$totuse','$avaibal','$minstocks','$foqi','$comm')");
}
?>
It keeps saying that Lines (too many lines) are Undefined Index. Or was i missing something?
PS: Its an inventory list thats why its toooo much.

Try this:
<?php
$franklinoffshore = mysqli_connect("localhost","root","","franklinoffshore");
$desc = mysqli_real_escape_string($franklinoffshore, $_POST['desc']);
$swltonne = mysqli_real_escape_string($franklinoffshore,$_POST['swltonne']);
$inches = mysqli_real_escape_string($franklinoffshore,$_POST['inches']);
$model = mysqli_real_escape_string($franklinoffshore,$_POST['model']);
$safetyfactor = mysqli_real_escape_string($franklinoffshore,$_POST['safetyfactor']);
$datelpo = mysqli_real_escape_string($franklinoffshore,$_POST['datelpo']);
$manu = mysqli_real_escape_string($franklinoffshore,$_POST['manu']);
$cnumber = mysqli_real_escape_string($franklinoffshore,$_POST['cnumber']);
$opstock = mysqli_real_escape_string($franklinoffshore,$_POST['opstock']);
$jan = mysqli_real_escape_string($franklinoffshore,$_POST['jan']);
$feb = mysqli_real_escape_string($franklinoffshore,$_POST['feb']);
$mar = mysqli_real_escape_string($franklinoffshore,$_POST['mar']);
$apr = mysqli_real_escape_string($franklinoffshore,$_POST['apr']);
$may = mysqli_real_escape_string($franklinoffshore,$_POST['may']);
$jun = mysqli_real_escape_string($franklinoffshore,$_POST['jun']);
$jul = mysqli_real_escape_string($franklinoffshore,$_POST['jul']);
$aug = mysqli_real_escape_string($franklinoffshore,$_POST['aug']);
$sep = mysqli_real_escape_string($franklinoffshore,$_POST['sep']);
$oct = mysqli_real_escape_string($franklinoffshore,$_POST['oct']);
$nov = mysqli_real_escape_string($franklinoffshore,$_POST['nov']);
$dec = mysqli_real_escape_string($franklinoffshore,$_POST['dec']);
$totuse = mysqli_real_escape_string($franklinoffshore,$_POST['totuse']);
$avaibal = mysqli_real_escape_string($franklinoffshore,$_POST['avaibal']);
$minstocks = mysqli_real_escape_string($franklinoffshore,$_POST['minstocks']);
$foqi = mysqli_real_escape_string($franklinoffshore,$_POST['foqi']);
$comm = mysqli_real_escape_string($franklinoffshore,$_POST['comm']);
//connect to the serverand database
$result = mysqli_query($franklinoffshore,
"INSERT INTO inventory_hooks
(Description,SWLTonne,Inches,Model,
SafetyFactor,DateLPO,Manufacturer,
CertificateNumber,OpeningStocks,
January,February,March,April,May,
June,July,August,September,October,
November,December,TotalUsed,AvailableBalance,
MinimumStocks,FoqFoi,Comment) VALUES
('$description','$swltonne',
'$inches','$model','$safetyfactor',
'$datelpo','$manufacturer','$cnumber',
'$opstock','$jan','$feb','$mar','$apr',
'$may','$jun','$jul','$aug','$sep','$oct',
'$nov','$dec','$totuse','$avaibal','$minstocks',
'$foqi','$comm')");
if($result) {
echo "It works";
}
?>

Related

Php not receiving data from form

I have a excerpt of my form, as follows:
// If logged in shows this box (it's backed behind a include php file that makes the login check
<form action="./saveList.php" method="post" id="ListaComprasForm">
<div class="submitButtonEncap">
<button type="submit" method="POST" name="submitButton" class="btn btn-primary roundedBorder" form="ListaComprasForm" value="submit">
<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span> Save List
</button>
</div>
<input type="text" id="Limpeza0" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_0" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza1" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_1" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza2" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_2" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza3" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_3" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza4" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_4" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza5" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_5" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza6" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_6" name="L_chckb_[]" value="check"/>
</form>
This form, on submit, will submit to a DB wether or not checkboxes and TextBox fields have been filled or not. The checkboxes that have been checked or not, he'll submit them over as Y and N or nullo for the Textboxes respectively through some sorting code as follows:
if (isset($_POST['submitButton'])){
$L_chck[] = array();
$H_chck[] = array();
$V_chck[] = array();
$F_chck[] = array();
$Cong_chck[] = array();
$Cons_chck[] = array();
$Outros_chck[] = array();
$Outros = array();
for ($counter = 0; $counter <= 6; $counter++){
$L_chck[$counter] = (isset($_POST['L_checkb_'.$counter]) ? 'Y' : 'N');
$H_chck[$counter] = (isset($_POST['H_checkb_'.$counter]) ? 'Y' : 'N');
$V_chck[$counter] = (isset($_POST['V_chckb_'.$counter]) ? 'Y' : 'N');
$F_chck[$counter] = (isset($_POST['F_chckb_'.$counter]) ? 'Y' : 'N');
$Cong_chck[$counter] = (isset($_POST['Cong_chckb_'.$counter]) ? 'Y' : 'N');
$Cons_chck[$counter] = (isset($_POST['Cons_chckb_'.$counter]) ? 'Y' : 'N');
$Outros_chck[$counter] = (isset($_POST['Outros_chckb_'.$counter]) ? 'Y' : 'N');
if (empty($_POST['Outros'.$counter])) {
$Outros[$counter] = $_POST['Outros'.$counter] = 'nullo';
} else {
$Outros[$counter] = $_POST['Outros'.$counter];
}
}
for ($i=0; $i <= 4; $i++) {
/*if ((isset($_POST['Limpeza'.$i])) == false) {*/
if (empty($_POST['Limpeza'.$i])) {
$Limpeza[$i] = $_POST['Limpeza'.$i] = 'nullo';
} else {
$Limpeza[$i] = $_POST['Limpeza'.$i];
}
if (empty($_POST['Higiene'.$i])) {
$Higiene[$i] = $_POST['Higiene'.$i] = 'nullo';
} else {
$Higiene[$i] = $_POST['Higiene'.$i];
}
if (empty($_POST['Vegetais'.$i])) {
$Vegetais[$i] = $_POST['Vegetais'.$i] = 'nullo';
} else {
$Vegetais[$i] = $_POST['Vegetais'.$i];
}
if (empty($_POST['Fruta'.$i])) {
$Fruta[$i] = $_POST['Fruta'.$i] = 'nullo';
} else {
$Fruta[$i] = $_POST['Fruta'.$i];
}
if (empty($_POST['Congelados'.$i])) {
$Congelados[$i] = $_POST['Congelados'.$i] = 'nullo';
} else {
$Congelados[$i] = $_POST['Congelados'.$i];
}
if (empty($_POST['Conservas'.$i])) {
$Conservas[$i] = $_POST['Conservas'.$i] = 'nullo';
} else {
$Conservas[$i] = $_POST['Conservas'.$i];
}
}
This data is then passed on as arguments to a function to be serialized and sent to DB fields, all of them varchar(255):
savingListData($L_chck, $H_chck, $V_chck, $F_chck, $Cong_chck, $Cons_chck, $Outros_chck, $Limpeza, $Higiene, $Vegetais, $Fruta, $Congelados, $Conservas, $Outros);
So far, every piece of data, being it checked/filled or not, it transposes over to the DB as null. In the case of the checkboxes, N, and in the case of the Textboxes nullo.
I'm led to believe that something is wrong in the sorting but I'm puzzled and can't figure out why. Would really appreciate some help!!
EDIT: Added more of the form in a shortened manner. (It's a big form!)
I think you are trying to retrieving it using wrong name. Its L_chckb_[]
<input type="checkbox" class="x" id="L_chck_0" name="L_chckb_[]" value="check"/>
So you need to use the loop counter outside the name key like this:
$L_chck[$counter] = (isset($_POST['L_chckb_'][$counter]) ? 'Y' : 'N');
The problem is: the name attribute in the tag is L_chckb_ but you are trying to retrieve it by L_checkb_ so that is the issue.
Because L_checkb_ is not set and so it is giving you N.

Populate data from dropdown list using a load button

I am trying to populate afew fields using a load button when I select from the dropdown list. However, the load button is not working and the php codes look fine to me. So I am wondering which part went wrong.
I am wondering if I should put the load button at AuthorityCode or below the form. However, I have tried both methods and both doesn't work.
<strong>Authority Code: </strong>
<select name=authorityid value=authorityid>Select Authority</option>
<option value = "">Select</option><br/>
<?php
$connection = new mysqli("localhost", "username", "password", "dbname");
$stmt = $connection->prepare("SELECT AuthorityId FROM AuthorityList");
$stmt->execute();
$stmt->bind_result($authorityid);
while($stmt->fetch()){
echo "<option value = '$authorityid'>$authorityid</option>";
}
$stmt->close();
$connection->close();
?>
</select>
<?php
if(isset($_POST["loadbtn"])){
$authorityid = $_POST["authorityid"];
$conn = new mysqli("localhost", "username", "password", "dbname");
$stmt = $conn->prepare("SELECT AuthorityName, Address, TelephoneNo, FaxNo FROM AuthorityList WHERE AuthorityId = '$authorityid'");
$stmt->execute();
$result = mysqli_query($stmt, $conn);
$details = mysqli_fetch_array($result);
$savedName = $details["AuthorityName"];
$savedAddress = $details["Address"];
$savedTel = $details["TelephoneNo"];
$savedFax = $details["FaxNo"];
}
// while ($row = $result->fetch_array(MYSQLI_NUM)){
// $authorityname = $row[0];
// $address = $row[1];
// $telephone = $row[2];
// $fax = $row[3];
// }
?>
<form action="" method="post" >
<input type="submit" value="Load" name="loadbtn"><br/><br/>
<strong>Authority Name: </strong> <input type="text" name="authorityname" value="<?php echo $savedName; ?>"/><br/>
<strong>Address: </strong> <input type="text" name="address" value="<?php echo $savedAddress; ?>"/><br/>
<strong>Telephone No: </strong> <input type="text" name="telephone" value="<?php echo $savedTel; ?>"/><br/>
<strong>Fax No: </strong> <input type="text" name="fax" value="<?php echo $savedFax; ?>"/><br/>
Change form to:
<form action="window.location.reload()" method="post" >
Your select values are not getting submitted as they are not part of the form. Also your 2nd query was not running properly. I've cheanged it below. Please take a look
Try this-
<strong>Authority Code: </strong>
<form action="" method="post" >
<select name="authorityid">Select Authority
<option value = "">Select</option><br/>
<?php
$connection = new mysqli("localhost", "username", "password", "dbname");
$stmt = $connection->prepare("SELECT AuthorityId FROM AuthorityList");
$stmt->execute();
$stmt->bind_result($authorityid);
while($stmt->fetch()){
echo "<option value = '$authorityid'>$authorityid</option>";
}
$stmt->close();
$connection->close();
?>
</select>
<?php
if(isset($_POST["loadbtn"])){
$authorityid = $_POST["authorityid"];
$conn = new mysqli("localhost", "username", "password", "dbname");
$qry = "SELECT AuthorityName, Address, TelephoneNo, FaxNo FROM AuthorityList WHERE AuthorityId = '$authorityid'";
$result = $conn->query($qry);
$details = mysqli_fetch_array($result);
$savedName = $details["AuthorityName"];
$savedAddress = $details["Address"];
$savedTel = $details["TelephoneNo"];
$savedFax = $details["FaxNo"];
}
?>
<input type="submit" value="Load" name="loadbtn"><br/><br/>
<strong>Authority Name: </strong>
<input type="text" name="authorityname" value="<?php echo isset($savedName) ? $savedName : ''; ?>"/>
<br/>
<strong>Address: </strong>
<input type="text" name="address" value="<?php echo isset($savedAddress) ? $savedAddress : ''; ?>"/>
<br/>
<strong>Telephone No: </strong>
<input type="text" name="telephone" value="<?php echo isset($savedTel) ? $savedTel : ''; ?>"/>
<br/>
<strong>Fax No: </strong>
<input type="text" name="fax" value="<?php echo isset($savedFax) ? $savedFax : ''; ?>"/>
<br/>
</form>
Your error seems to be from mysql part of the code. Below you'll find a sample code that has mysql part removed from it and is running-
<!DOCTYPE html>
<html>
<head>
<title>Fix</title>
</head>
<body>
<strong>Authority Code: </strong>
<form action="" method="post" >
<select name="authorityid">Select Authority
<option value = "">Select</option><br/>
<?php
echo "<option value = '123'>123</option>";
echo "<option value = '234'>234</option>";
echo "<option value = '345'>345</option>";
echo "<option value = '456'>456</option>";
?>
</select>
<?php
if(isset($_POST["loadbtn"])){
$authorityid = $_POST["authorityid"];
if ($authorityid == '123') {
$savedName = 'nameTest';
$savedAddress = 'addressTest';
$savedTel = 'telTest';
$savedFax = 'faxTest';
}
}
?>
<input type="submit" value="Load" name="loadbtn"><br/><br/>
<strong>Authority Name: </strong>
<input type="text" name="authorityname" value="<?php echo isset($savedName) ? $savedName : ''; ?>"/>
<br/>
<strong>Address: </strong>
<input type="text" name="address" value="<?php echo isset($savedAddress) ? $savedAddress : ''; ?>"/>
<br/>
<strong>Telephone No: </strong>
<input type="text" name="telephone" value="<?php echo isset($savedTel) ? $savedTel : ''; ?>"/>
<br/>
<strong>Fax No: </strong>
<input type="text" name="fax" value="<?php echo isset($savedFax) ? $savedFax : ''; ?>"/>
<br/>
</form>
</body>
</html>
Edited: changed the mysqli_query function so that it works properly.

Group Arrays By Another Array?

I'm trying to sort one array by another array. Both these arrays get their content from a form.
Here's my form code:
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Group One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="variable[]" value="variableone" />
<input type="text" name="variable[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Group Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group Two:</label><br/>
<input type="text" name="variable[]" value="variablethree" />
<input type="text" name="variable[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
Here's the PHP code:
<?php
if (!$_POST['submit'] == "") {
foreach($_POST['groupname'] as $groupname) {
$groupnum = 1;
foreach($_POST['variable'] as $variable) {
print "$".$groupname.$groupnum." = '".$variable."';<br/>";
$groupnum++;
}
print "$".$groupname." = array(";
for ($arrnum = 1; $arrnum <= count($_POST['variable']); $arrnum++) {
print "$".$groupname.$arrnum.", ";
}
print ");<br/><br/>";
}
}
?>
This is the result I get when I submit the form:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone3 = '$variablethree';
$groupone4 = '$variablefour';
$groupone = array($groupone1, $groupone2, $groupone3, $groupone4, )
$grouptwo1 = '$variableone';
$grouptwo2 = '$variabletwo';
$grouptwo3 = '$variablethree';
$grouptwo4 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2, $grouptwo3, $grouptwo4, )
This is the result that I actually want:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone = array($groupone1, $groupone2)
$grouptwo1 = '$variablethree';
$grouptwo2 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2)
The whole thing needs to be dynamic since I want to add as many groups and variables as I want.
I've been searching for an answer for days and already asked two people who didn't know an answer. Maybe you guys can help. Thanks!
Update:
Just to clarify a few points:
So basically I want to be able to add as many input forms as I want (I use jQuery for that) to create as many groups and variables as I want, for example like this:
$groupwuteva1 = 'hello';
$groupwuteva2 = 'bye':
$randomname1 = 'green';
$randomname2 = 'blue';
$randomname3 = 'red';
$blabla1 = 'abc';
$blabla2 = 'xyz';
$blabla3 = '123';
$blabla4 = 'bla';
Whatever I use as groupname will be used in array one, e.g. I call a group "Colors" and the variables I put into the form for that group are "blue", "red" and "green". Then I would get this code:
$colors1 = 'green';
$colors2 = 'blue';
$colors3 = 'red';
I hope this clairfies some questions. And thanks a ton for all responses so far!
You can take the group name as a container to store all associated variable values in it. And later, use variable variables and implode() function to process your html form.
HTML
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Groupe One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="groupone[]" value="variableone" />
<input type="text" name="groupone[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Groupe Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="grouptwo[]" value="variablethree" />
<input type="text" name="grouptwo[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
PHP
if(isset($_POST['submit'])){
foreach($_POST['groupname'] as $value){
$arr = array();
$i = 1;
foreach($_POST[$value] as $v){
$var = $value . $i;
$$var = $v;
echo $var . " = " . $$var . "<br />";
$arr[] = $$var;
++$i;
}
$output = $value . " = array(" . implode(",", $arr) . ")";
echo $output . "<br /><br />";
}
}
Output:
groupone1 = variableone
groupone2 = variabletwo
groupone = array(variableone,variabletwo)
grouptwo1 = variablethree
grouptwo2 = variablefour
grouptwo = array(variablethree,variablefour)
try this form:
<form method="post" action="">
<?php foreach(array('groupone', 'grouptwo') as $num):?>
<div class="groupcontainer">
<br/><label>Groupe <?php echo $num;?>:</label><br/>
<input type="text" name="groupname[]" value="<?php echo $num;?>" /><br/>
<br/><label>Variable Group <?php echo $num;?>:</label><br/>
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
</div>
<br/>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit" />
</form>
and code:
if ($_POST) {
foreach($_POST['groupname'] as $groupname) {
$$groupname = array();
foreach($_POST['variable'][$groupname] as $variable) {
${$groupname}[] = $variable;
}
}
var_dump($groupone);
var_dump($grouptwo);
}

I want to "get" more than one "item"

I want to send the "fill[]" and the "item[]" and my problem is the function write cant get the values of the variables, why?
What is wrong? Im new in this stuff.
<form action="/fill_sites/fill.php" method="get">
<input type="text" size="24" maxlength="20" name="fill[0]"/>
<input type="hidden" name="item[0]value="hella_apfelschorle"/>
<input type="text" size="24" maxlength="20" name="fill[1]"/>
<input type="hidden" name="item[1]" value="volvic_wasser"/>
..... so on
<input type="submit" value="Abschicken"/>
------------------------------------------------------------>[fill/php]>
$fill[0] = $_GET["fill[0]"];
$fill[1] = $_GET["fill[1]"];
$fill[2] = $_GET["fill[2]"];
$fill[3] = $_GET["fill[3]"];
$fill[4] = $_GET["fill[4]"];
$fill[5] = $_GET["fill[5]"];
$fill[6] = $_GET["fill[6]"];
$fill[7] = $_GET["fill[7]"];
$fill[8] = $_GET["fill[8]"];
$fill[9] = $_GET["fill[9]"];
$item[0] = $_GET["item[0]"];
$item[1] = $_GET["item[1]"];
$item[2] = $_GET["item[2]"];
$item[3] = $_GET["item[3]"];
$item[4] = $_GET["item[4]"];
$item[5] = $_GET["item[5]"];
$item[6] = $_GET["item[6]"];
$item[7] = $_GET["item[7]"];
$item[8] = $_GET["item[8]"];
$item[9] = $_GET["item[9]"];
write($counterFilename[$item[]], $fill);
you can do this by following code. it will get complete array.
$item = $_Get['item'];
$fill= $_Get['fill'];
use php's foreach statement to retrieve the content of fill and item
$fills = $_GET['fill'];
foreach($fills as $fill)
{
//do something with $fill
}
and the same for $item

php add multiple input fields to mysql

I have this code:
<html>
<body>
<form id="myForm" method="post" action="add-data.php">
<input type="submit">
<input type="text" name="pollquestion">
<input type="text" name="polloption1">
<input type="text" name="polloption2">
</form>
Add option
<script>
var optionNumber = 3;
function addOption() {
var theForm = document.getElementById("myForm");
var newOption = document.createElement("input");
newOption.name = "polloption"+optionNumber+""; // poll[optionX]
newOption.type = "text";
theForm.appendChild(newOption);
optionNumber++;
}
</script>
</body>
</html>
If i add more inputs i will have something like this:
<input name="pollquestion" type="text">
<input name="polloption1" type="text">
<input name="polloption2" type="text">
<input name="polloption3" type="text">
<input name="polloption4" type="text">
<input name="polloption5" type="text">
<input name="polloption6" type="text">
The php code is something like this:
$qu = $_POST['pollquestion'];
$op1 = $_POST['polloption1'];
$op2 = $_POST['polloption2'];
$query = "INSERT into `".$db_table."` (question, option1, option2) VALUES ('" . $qu . "','" . $op1 . "','" . $op2 . "')";
How can i add this data to mysql for every added row? Thanks!
One way of many...
$query = "INSERT into `$db_table` SET `question` = '".mysql_real_escape_string($_POST['pollquestion'])."'";
foreach (range(1,6) as $idx) {
if (!empty($_POST['polloption'.$idx])) {
$query .= ", `option$idx` = '".mysql_real_escape_string($_POST['polloption'.$idx])."'";
}
}
of course the mysql_real_escape_string is important to avoid http://en.wikipedia.org/wiki/SQL_injection
First, you need to know how many options you're submitting so add another constant input to the form:
<input type="hidden" id="numOptions" name="numOptions"/>
In the addOption() function update its value (before incrementing optionNumber):
document.getElementById( "numOptions" ).value = optionNumber;
On the server side you need to create your query dynamically like so:
$options = array();
$values = array();
$numOptions = intval( $_POST[ "numOptions" ] );
for ( $i = 1; $i <= $numOptions; $i++ )
{
$options[] = "option$i";
$values [] = "'" . mysql_real_escape_string( $_POST[ "polloption$i" ] ) . "'";
}
$query = "INSERT INTO $db_table(" . implode( ',', $options ) . ") VALUES( '" .
implode( ',', $values );
Please mind the escaping of the received strings! very important to prevent SQL injections.
HTML
<input name="title" type="text">
<input name="descr" type="text">
<input name="question[1]" type="text">
<input name="option[1][1]" type="text">
<input name="option[1][2]" type="text">
<input name="option[1][3]" type="text">
<input name="right[1]" type="radio" value=1>
<input name="right[1]" type="radio" value=2>
<input name="right[1]" type="radio" value=3>
<input name="question[2]" type="text">
<input name="option[2][1]" type="text">
<input name="option[2][2]" type="text">
<input name="option[2][3]" type="text">
<input name="right[2]" type="radio" value=1>
<input name="right[2]" type="radio" value=2>
<input name="right[2]" type="radio" value=3>
PHP
$title = mysql_real_escape_string($_POST['title'])
$descr = mysql_real_escape_string($_POST['descr'])
$query = "INSERT into `polls` (title,descr) VALUES ('$title', '$descr')";
$id = $db->query($query);
foreach ($_POST['question'] as $num => $q) {
$q = mysql_real_escape_string($q)
$query = "INSERT into `poll questions` (poll,question) VALUES ($id,'$q')";
$db->query($query);
foreach ($_POST['option'][$num] as $i => $opt) {
$right = ($_POST['right'][$num]) == $i)?1:0;
$opt = mysql_real_escape_string($opt)
$num = intval($num);
$query = "INSERT into `poll options` (poll,num,option,right)
VALUES ($id,$num,'$opt',$right)";
}
}
You can iterate $_POST, matching keys with regular patterns, something like that:
foreach($_POST as $key => $value) {
preg_match('/(\w+)(\d+)/Uis', $key, $m);
if($m[1] == 'polloption') {
// concatenate new values to your query
}
}
Remembering relational databases, you have fixed number of attributes in your table. So you should add fixed number of options.

Categories