im trying to insert only populated ID fields to MySQL, meaning if two out of three are populated, only two to be inserted. I got blind with many code lines, and can't see where Im making a mistake.
Logic is that I'll search for the serial number, once selected it will populate row ID from the serial number into the ID field.
I want to pass ID field value to the database. However, below code is submitting all ID fields even the empty ones (where serial number is not selected), and I dont need to pass records for not populated ID field.
Where am I making a mistake?
Thanks
$idCount = count($_POST['assetsn_id']);
echo $idCount;
for($i=0; $i < $idCount; ++$i) {
$assetsn_id = $_POST['assetsn_id'][$i];
$assetsn_location_address = $_POST['assetsn_location_address'];
$assetsn_location_rack = $_POST['assetsn_location_rack'];
$assetsn_location_shelf = $_POST['assetsn_location_shelf'];
$assetsn_location_bin = $_POST['assetsn_location_bin'];
$assetsn_location_createdby = $_SESSION['user']['id'];
$assetsn_location_userlogid = $_SESSION['user']['userlogid'];
$sql = "INSERT INTO asset_serial_locations SET assetsn_id=?, assetsn_location_address=?, assetsn_location_rack=?, assetsn_location_shelf=?, assetsn_location_bin=?, assetsn_location_createdby=?, assetsn_location_userlogid=?";
$result = modifyRecord($sql, 'sssssss', [$assetsn_id, $assetsn_location_address, $assetsn_location_rack, $assetsn_location_shelf, $assetsn_location_bin, $assetsn_location_createdby, $assetsn_location_userlogid]);
if ($result) {
$_SESSION['success_msg'] = "Location updated successfully!";
header("location: " . BASE_URL . "workshop/location/");
exit(0);
} else {
$_SESSION['error_msg'] = "Something went wrong. Could not update locations.".'<br />'.mysqli_error($conn);
}
}
Serial number #1
<input class="form-control form-control-lg" type="search" name="" autocomplete="off" placeholder="search for serial number..." />
<input class="form-control form-control-lg" type="text" name="assetsn_id[]" autocomplete="off" placeholder="ID" /><br /><br />
Serial number #2
<input class="form-control form-control-lg" type="search" name="" autocomplete="off" placeholder="search for serial number..." />
<input class="form-control form-control-lg" type="text" name="assetsn_id[]" autocomplete="off" placeholder="ID" /><br /><br />
Serial number #3
<input class="form-control form-control-lg" type="search" name="" autocomplete="off" placeholder="search for serial number..." />
<input class="form-control form-control-lg" type="text" name="assetsn_id[]" autocomplete="off" placeholder="ID" />
Use array_filter to remove nulls values from an array :
$_POST['assetsn_id'] = array_filter($_POST['assetsn_id']);
$idCount = count($_POST['assetsn_id']);
echo $idCount;
for($i=0; $i < $idCount; ++$i) {
....
}
Here what array_filter can remove :
$a = array(0, '0', NULL, FALSE, '', array());
var_dump(array_filter($a));
// array()
for example, I receive two inputs value1 and value2 and I want this input for different functions. Like addition, subtraction and multiplication.
my code
<?php
$x = $_POST['fnum'];
$y = $_POST['snum'];
if (isset($_POST['add'])) {
$sum = $x + $y;
echo "Result:<input type='text' value='$sum'/>";
}
if (isset($_POST['sub'])) {
$sub = $x - $y;
echo "Result:<input type='text' value='$sub'/>";
}
if (isset($_POST['mul'])) {
$mul = $x * $y;
echo "Result:<input type='text' value='$mul'/>";
}
<body>
<form method="post">
Enter first number <input type="text" name="fnum" />
<hr />
Enter second number <input type="text" name="snum" />
<hr />
<input type="submit" name="add" value="ADD" />
<input type="submit" name="sub" value="Subtract" />
<input type="submit" name="mul" value="Multiply" />
</form>
</body>
In this it is asking me to feed input for each operation separately
Good, Just use the posted value in your form input like -
Enter first number <input type="text" name="fnum" value="<?php echo #$_POST['fnum'];?>"/><hr/>
Enter second number <input type="text" name="snum" value="<?php echo #$_POST['snum'];?>"/><hr/>
So that user don't need to put the same value again and when press the other buttons the form will automatically submit with the previous values.
Note: Remember, you need to check all the posted value is set or not and use proper conditions of POST method. Have a look at the given example of your problem as solution, I give it here to give you a proper guide.
Example:
<?php
$x = 0;
$y = 0;
if(isset($_POST['submit'])) {
$x = $_POST['fnum'];
$y = $_POST['snum'];
$operator = "";
if($_POST['submit'] == 'ADD') {
$operator = "+";
} elseif($_POST['submit'] == 'Subtract') {
$operator = "-";
} elseif($_POST['submit'] == 'Multiply') {
$operator = "*";
}
$result = $x . $operator . $y;
}?>
Your form will be-
<form method="post">
Enter first number <input type="text" name="fnum" value="<?php echo $x;?>"/><hr/>
Enter second number <input type="text" name="snum" value="<?php echo $y;?>"/><hr/>
<input type="submit" name="submit" value="ADD"/>
<input type="submit" name="submit" value="Subtract"/>
<input type="submit" name="submit" value="Multiply"/>
</form>
Result:
<input type='text' value='<?php echo (isset($result) ? $result : "-";)?>'/>
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";
}
?>
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 have an error that I can't figure out...
Om my webpage there is a form that the user has the ability to add some new input fields to. If the user is submitting the form, then the optional fields is empty when the php-file is handing them, why?
HTML:
<form method="post" action="newRequest.php">
<input type="text" name="title" />
<input type="hidden" name="fname" value="0" />
<input type="checkbox" name="fname" value="1"/>
<input type="hidden" name="ename" value="0" />
<input type="checkbox" name="ename" value="1" />
<input type="hidden" name="seat" value="0" />
<input type="checkbox" name="seat" value="1" />
<input type="hidden" name="fields" value="0" />
<input type="text" id="fields" name="fields" />
<input type="submit" />
</form>
PHP:
if (strlen($_POST[title]) > 2) {
$toDb[title] = $_POST[title];
} else {
error('title');
}
$toDb[fname] = $_POST[fname];
$toDb[ename] = $_POST[ename];
$toDb[seat] = $_POST[seat];
if ($_POST[fields] > 0) {
$i = 0;
while ($i < $_POST[fields]) {
$toDb[optional][$i] = $_POST[optional-$i];
$i++;
}
$toDb[optional] = serialize($toDb[optional]);
} else {
$toDb[optional] = 0;
}
newEvent($toDb,$dbh);
JQuery that is adding dynamical fields:
$(document).ready(function() {
$('#fields').focusout(function(){
var fields = $('#fields').val();
var i = 0;
while(i < fields) {
$('#fields').after("Valfritt fält "+(i+1)+":<input type='text' name='optional"+i+"' />");
i++;
}
})
})
You should quote array indexes. It should be
$toDb['optional'][$i] = $_POST['optional'.$i];
You are missing commas in $_POST
$toDb['fname'] = $_POST['fname'];
$toDb['ename'] = $_POST['ename'];
$toDb['seat'] = $_POST['seat'];
Here is your modified code
if (strlen($_POST['title']) > 2) {
$toDb['title'] = $_POST['title'];
} else {
error('title');
}
$toDb['fname'] = $_POST['fname'];
$toDb['ename'] = $_POST['ename'];
$toDb['seat'] = $_POST['seat'];
if (count($_POST) > 0) {
$i = 0;
while ($i < count($_POST)) {
$toDb['optional'][$i] = $_POST['optional-'.$i];
$i++;
}
$toDb['optional'] = serialize($toDb['optional']);
} else {
$toDb['optional'] = 0;
}
newEvent($toDb,$dbh);
Also use count() to check if $_POST has values > 0.
I faced the same problem and I solved it using Javascript, like this :
add a new text field every time a button is pressed