Posting dynamic inputs and inserting into MySQL database - php

This is my schedule.php page, an example of the user selecting the specific day for the time.
Once the user clicks on to add, the confirmation page will show up. But unfortunately i got these errors.
In mySQL database, the values that suppose to belong to "1700 - 1800" appeared on the "1400 - 1600" row.
This is my code for the schedule.php page,
<form name="Create New schedule" class="form-horizontal" method="post" action="handleSchedule.php">
<div class="form-group">
<div class="col-sm-4">
<label for="AcadInst">Academic Institution</label>
<select class="form-control" id="AcadInst" type="text" class="form-control" placeholder="Institution Name" name="academicInstitution">
<option>Institution Name</option>
<option>Singapore Polytechnic (SP)</option>
<option>Ngee Ann Polytechnic (NP)</option>
<option>Temasek Polytechnic (TP)</option>
<option>Republic Polytechnic (RP)</option>
<option>Nanyang Polytechnic (NYP)</option>
<option>Others(Please specify)</option>
</select>
</div>
<div class="col-sm-4">
<label>Level of Teaching</label>
<input list="LvTeaching" type="text" class="form-control" placeholder="Teaching Stage" name="levelofteaching">
<datalist id="LvTeaching">
<option value="Undergraduate Teaching">
<option value="Postgraduate Teaching">
<option value="Continuing Education">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Type of Teaching</label>
<input list="TyTeaching" type="text" class="form-control" placeholder="Mode of Teaching" name="typeofteaching">
<datalist id="TyTeaching">
<option value="Clinical Teaching">
<option value="Academic Teaching">
<option value="Talk">
<option value="Others (Please specify)">
</datalist>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<label for="startdate">Start Date</label>
<input type="date" class="form-control" placeholder="Select Date" name="startdate">
</div>
<div class="col-sm-4">
<label for="enddate">End Date</label>
<input type="date" class="form-control" placeholder="Select Date" name="enddate" id="noEndDate">
<input onclick="document.getElementById('noEndDate').disabled = true;" type="checkbox" name="type">No end date
</div>
</div>
<div class="form-group">
<div class="panel-body table-responsive">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Start Time</th>
<th>End Time</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th><input type="button" class="btn btn-warning" value="Add Timeslot" onClick="addRow('dataTable')" /></th>
</tr>
</thead>
<tbody id="dataTable">
<tr>
<td><input type="text" class="form-control" name="startTime[0]"></td>
<td><input type="text" class="form-control" name="endTime[0]"></td>
<td><input type="checkbox" name="Monday[0]" value="1"></td>
<td><input type="checkbox" name="Tuesday[0]" value="1"></td>
<td><input type="checkbox" name="Wednesday[0]" value="1"></td>
<td><input type="checkbox" name="Thursday[0]" value="1"></td>
<td><input type="checkbox" name="Friday[0]" value="1"></td>
<td><input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" /></td>
</tr>
</tbody>
</table>
<br/>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<input type="submit" value="Add" class="btn btn-success">
</div>
</div>
</form>
My codes for handleSchedule.php,
<?php
$con = getDbConnect();
$academicInstitution = $_POST['academicInstitution'];
$levelofteaching = $_POST['levelofteaching'];
$typeofteaching = $_POST['typeofteaching'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
if ($_POST['startTime']) {
foreach ($_POST["startTime"] as $key => $value) {
$endTime = $_POST["endTime"][$key];
$monday = $_POST["Monday"][$key];
$tuesday = $_POST["Tuesday"][$key];
$wednesday = $_POST["Wednesday"][$key];
$thursday = $_POST["Thursday"][$key];
$friday = $_POST["Friday"][$key];
$sql = "INSERT INTO timetableschedule (academicInstitution, lvteaching, tyteaching, startdate, endate, startTime, endTime, Monday, Tuesday, Wednesday, Thursday, Friday) " .
"VALUES ('$academicInstitution', '$levelofteaching', '$typeofteaching', '$startdate', '$enddate', '$value', '$endTime', '$monday', '$tuesday', '$wednesday', '$thursday', '$friday')";
mysqli_query($con, $sql);
}
}
echo "1 record added";
mysqli_close($con);
}
?>
I am suspecting that the unchecked checkbox are causing the problem. Do anyone have any idea to solve it?

Unchecked checkboxes don't get submitted to the server. So $_POST['Monday'] is just an array of all the checked boxes. The indexes of these parameters won't match up with the text boxes.
The solution is to put explicit indexes in the names:
<tr>
<td><input type="text" class="form-control" name="startTime[0]"></td>
<td><input type="text" class="form-control" name="endTime[0]"></td>
<td><input type="checkbox" name="Monday[0]" value="1"></td>
<td><input type="checkbox" name="Tuesday[0]" value="1"></td>
<td><input type="checkbox" name="Wednesday[0]" value="1"></td>
<td><input type="checkbox" name="Thursday[0]" value="1"></td>
<td><input type="checkbox" name="Friday[0]" value="1"></td>
<td><input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" /></td>
</tr>
When you add a new row, you need to increment the index.
In the PHP, you then need to check whether the parameter is set.
$monday = isset($_POST["Monday"][$key]) ? 1 : 0;

Related

Error To Database when Leave uncheck [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
this is the output
<?php
include_once('connection.php');
$StdID = $_REQUEST['txtID'];
$StdImage = $_REQUEST['txtImage'];
$FullName = $_REQUEST['txtFullname'];
$Address = $_REQUEST['txtAdd'];
$Bday = $_REQUEST['txtBday'];
$Grade = $_REQUEST['txtGrade'];
$Height = $_REQUEST['txtHeight'];
$Weight = $_REQUEST['txtWeight'];
$BloodPressure = $_REQUEST['txtBlood'];
$Gname = $_REQUEST['txtGname'];
$Contact = $_REQUEST['txtContact'];
$BCG = $_REQUEST['chkBCG'];
$Cardiac = $_REQUEST['chkCardiac'];
$Asthma1 = $_REQUEST['chkAsthma1'];
$Alcohol = $_REQUEST['chkAlcohol'];
$OPV1 = $_REQUEST['chkOpv1'];
$Thyroid = $_REQUEST['chkThyroid'];
$Chicken = $_REQUEST['chkChicken'];
$Tobacco = $_REQUEST['chkYosi'];
$OPV2 = $_REQUEST['chkOPV2'];
$Diabetes = $_REQUEST['chkDiabetes'];
$Measles = $_REQUEST['chkMeasles1'];
$Opv3 = $_REQUEST['chkOpv3'];
$Hypertension = $_REQUEST['chkHypertension'];
$Mumps = $_REQUEST['chkMumps1'];
$Food1 = $_REQUEST['txtFood1'];
$DPT1 = $_REQUEST['chkDpt1'];
$Tuberculosis = $_REQUEST['chkTuber'];
$Ulcer = $_REQUEST['chkUlcer'];
$Food2 = $_REQUEST['txtFood2'];
$DPT2 = $_REQUEST['chkdpt2'];
$Asthma2 = $_REQUEST['chkAsthma2'];
$Dengue = $_REQUEST['chkdengue'];
$Food3 = $_REQUEST['txtfood3'];
$Dpt3 = $_REQUEST['chkDpt3'];
$Kidney = $_REQUEST['chkKidney'];
$Head = $_REQUEST['chkHead'];
$Measles2 = $_REQUEST['chkMeasles2'];
$Cancer = $_REQUEST['chkCancer'];
$Std = $_REQUEST['chkstd'];
$Scar = $_REQUEST['chkScar'];
$Hepa = $_REQUEST['chkHepa'];
$Hypertension2 = $_REQUEST['chkHypertension2'];
$Mole = $_REQUEST['chkMole'];
$Std2 = $_REQUEST['chkStd2'];
$Kidney2 = $_REQUEST['chkKidney2'];
$Tattoo = $_REQUEST['chkTattoo'];
$OthersImmu = $_REQUEST['txtothersimmu'];
$Birthmark = $_REQUEST['txtBirthmark'];
$OthersImmu2 = $_REQUEST['txtothersimmu2'];
$OthersIll = $_REQUEST['txtothersill'];
$OthersImmu3 = $_REQUEST['txtothersimmu3'];
$OthersIll2 = $_REQUEST['txtothersill2'];
$Mens = $_REQUEST['txtmens'];
$sql = "INSERT INTO tbl_medics VALUES ('$StdID', '$StdImage', '$FullName', '$Address', '$Bday', '$Grade', '$Height', '$Weight', '$BloodPressure', '$Gname','$Contact', '$BCG', '$Cardiac', '$Asthma1', '$Alcohol', '$OPV1', '$Thyroid', '$Chicken','$Tobacco', '$OPV2', '$Diabetes', 'Measles', '$Opv3', '$Hypertension', '$Mumps', '$Food1', '$DPT1', '$Tuberculosis', '$Ulcer', '$Food2', '$DPT2', '$Asthma2', '$Dengue', '$Food3', '$Dpt3', '$Kidney', '$Head', '$Measles2', '$Cancer', '$Std', '$Scar', '$Hepa', '$Hypertension2', '$Mole', '$Std2', '$Kidney2', '$Tattoo', '$OthersImmu', '$Birthmark', '$OthersImmu2', '$OthersIll', '$OthersImmu3', '$OthersIll2', '$Mens')";
if ($conn->query($sql) === TRUE) {
header("Location: MedicRecords.php?SuccessfullyAdded");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
This is my Input
<form action="AddMedicRecords.php" method="POST" enctype="multipart/form-data" style="border:1px solid #ccc">
<?php $id = $_GET['id'];
$sql = "SELECT * FROM tblstdpro where StdID = '$id'";
$result = mysqli_query($conn,$sql);
$count = 0;
while($row = mysqli_fetch_array($result)){
?>
<div class="box-body">
<div class="form-group">
<label><b>Student Image Location</b></label>
<input type="text" class="form-control" value="<?php echo $row['StdImage'];?>" name="txtImage" type="readonly" readonly></br>
<label><b>LRN</b></label>
<input type="text" class="form-control" value="<?php echo $row['StdID'];?>" name="txtID" required></br>
<label><b>Full Name</b></label>
<input type="text" class="form-control" value="<?php echo $row['Lname'];?>, <?php echo $row['Fname'];?> <?php echo $row['Mname'];?>" name="txtfullname" required></br>
<label><b>Address</b></label></br>
<input type="text" class="form-control" value="<?php echo $row['Street']; ?> , <?php echo $row['Barangay']; ?> <?php echo $row['Munic']; ?>, <?php echo $row['Province']; ?>" name="txtadd" required></br>
<label><b>Birthday</b></label>
<input type="text" class="form-control" value="<?php echo $row['Bday'];?>" name="txtbday" required></br>
<label><b>Grade/ Course</b></label></br>
<input type="text" class="form-control" value="<?php echo $row['Track'];?> - <?php echo $row['YearLvl'];?>" name="txtgrade" required></br>
<label><b>Height</b></label>
<input type="" class="form-control" placeholder="Enter Height" name="txtheight" required></br>
<label><b>Weight</b></label>
<input type="" class="form-control" placeholder="Enter Weight" name="txtweight" required></br>
<label><b>Blood Pressure</b></label>
<input type="" class="form-control" placeholder="Enter BP" name="txtblood" required></br>
</br>
<label><b><h3>*Person to be Notified in Case of Emergency</h3></b></label>
</br>
<label><b>Name:</b></label>
<input type="text" class="form-control" value="<?php echo $row['Mother'];?>" name="txtGname" required></br>
<label><b>Contact No.</b></label>
<input type="text" class="form-control" class="form-control" value="<?php echo $row['Contact'];?>" name="txtContact"></br>
<label><h3>*Kindly Check the Box Provided on the Left Side</h3></label>
<table class="table table-hover">
<thead>
<tr>
<th>IMMUNIZATION</th>
<th>FAMILY HISTORY</th>
<th>PREVIOUS ILLNESS</th>
<th>PERSONAL HISTORY</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="chkBCG" value="BCG"> BCG</td>
<td><input type="checkbox" name="chkCardiac" value="Cardiac Disease"> Cardiac Disease</td>
<td><input type="checkbox" name="chkAsthma1" value="Asthma"> Asthma</td>
<td><input type="checkbox" name="" value="chkAlcohol"> Alcohol Use</td>
</tr>
<tr>
<td><input type="checkbox" name="chkOpv1" value="OPV 1"> OPV 1</td>
<td><input type="checkbox" name="chkThyroid" value="Thyroid Disease"> Thyroid Disease</td>
<td><input type="checkbox" name="chkChicken" value="Chicken Pox"> Chicken Pox</td>
<td><input type="checkbox" name="chkYosi" value="Tobacco Use"> Tobacco Use</td>
</tr>
<tr>
<td><input type="checkbox" name="chkOpv2" value="OPV 2"> OPV 2</td>
<td><input type="checkbox" name="chkDiabetes" value="Diabetes Mellitus"> Diabetes Mellitus</td>
<td><input type="checkbox" name="chkMeasles1" value="Measles"> Measles</td>
<td>Allergy to Food, Drugs,Etc..</td>
</tr>
<tr>
<td><input type="checkbox" name="chkOpv3" value="OPV 3"> OPV 3</td>
<td><input type="checkbox" name="chkHypertension" value="Hypertension"> Hypertension</td>
<td><input type="checkbox" name="chkMumps" value="Mumps"> Mumps</td>
<td><input type="text" class="form-control" class="form-control" placeholder="Enter Allergy to Food, Drugs,Etc.." name="txtFood1"> </td>
</tr>
<td><input type="checkbox" name="ChkDpt1" value="DPT1"> DPT 1</td>
<td><input type="checkbox" name="chkTuber" value="Tuberculosis"> Tuberculosis</td>
<td><input type="checkbox" name="chkUlcer" value="Peptic Ulcer"> Peptic Ulcer</td>
<td><input type="text" class="form-control" class="form-control" placeholder="Enter Allergy to Food, Drugs,Etc.." name="txtFood2"> </td>
</tr>
<td><input type="checkbox" name="chkDpt2" value="DPT 2"> DPT 2</td>
<td><input type="checkbox" name="chkAstma2" value="Asthma"> Asthma</td>
<td><input type="checkbox" name="chkDengue" value="Dengue"> Dengue</td>
<td><input type="text" class="form-control" class="form-control" placeholder="Enter Allergy to Food, Drugs,Etc.." name="txtFood3"> </td>
</tr>
<tr>
<td><input type="checkbox" name="chkDpt3" value="DPT 3"> DPT 3</td>
<td><input type="checkbox" name="chkKidney" value="Kidney Disease"> Kidney Disease</td>
<td><input type="checkbox" name="chkHead" value="Head Injury"> Head Injury</td>
<td>Any Identification Mark:</td>
</tr>
<tr>
<td><input type="checkbox" name="chkMeasles2" value="Measles"> Measles</td>
<td><input type="checkbox" name="chkCancer" value="Cancer"> Cancer</td>
<td><input type="checkbox" name="chkStd" value="STD"> STD</td>
<td><input type="checkbox" name="chkScar" value="Scar"> 1. Scar</td>
</tr>
<tr>
<td><input type="checkbox" name="chkHepa" value="HEPA-B"> HEPA-B</td>
<td><input type="checkbox" name="chkSkin" value="Skin Disease"> Skin Disease</td>
<td><input type="checkbox" name="chkHypertension2" value="Hypertension"> Hypertension</td>
<td><input type="checkbox" name="chkMole" value="Mole"> 2. Mole</td>
</tr>
<tr>
<td>Others:</td>
<td><input type="checkbox" name="chkStd2" value="STD"> STD</td>
<td><input type="checkbox" name="chkKidney2" value="Kidney Problem"> Kidney Problem</td>
<td><input type="checkbox" name="chkTattoo" value="Tattoo"> 3. Tattoo</td>
</tr>
<tr>
<td><input type="text" class="form-control" class="form-control" placeholder="Enter Others" name="txtOthersImmu"> </td>
<td></td>
<td>Others:</td>
<td><input type="checkbox" name="txtBirthmark" value="Birthmark"> 4. Birthmark</td>
</tr>
<tr>
<td><input type="text" class="form-control" class="form-control" placeholder="Enter Others" name="txtOthersImmu2"> </td>
<td></td>
<td><input type="text" class="form-control" class="form-control" placeholder="Enter Others" name="txtOthersIll"> </td>
<td></td>
</tr>
<tr>
<td><input type="text" class="form-control" class="form-control" placeholder="Enter Others" name="txtOthersImmu3"> </td>
<td></td>
<td><input type="text" class="form-control" class="form-control" placeholder="Enter Others" name="txtOthersIll2"> </td>
<td></td>
</tr>
</tbody>
</table>
<label><b>FOR FEMALE ONLY: Date of Last Menstrual Period:</b></label>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" placeholder="Enter Birthday" class="form-control" data-inputmask="'alias': 'mm/dd/yyyy'" data-mask name="txtMens" >
</div>
<!-- /.input group -->
</div>
<!-- /.form group -->
<div class="clearfix">
<button type="submit" name="submit" class="btn btn-block btn-primary btn-lg">Add Student</button>
<button type="button" class="btn btn-block btn-danger btn-lg">Cancel</button>
</form></br></br>
</div>
</form>
<?php
}
?>
Errors Says this
Notice: Undefined index: txtFullname in C:\wamp64\www\TestingThesis\AddMedicRecords.php on line 9
Error: INSERT INTO tbl_medics VALUES ('014-321', 'StdImage/014-321.jpg', '', '', '', '', '', '', '', 'Mommy','097576346', 'BCG', '', '', '', '', '', '','', '', '', 'Measles', '', '', '', '', '', '', '', '', '', '', '', '', '', 'Kidney Disease', '', '', '', '', '', '', '', 'Mole', '', '', '', '', '', '', '', '', '', '')
Data truncated for column 'StdID' at row 1
How can this be done when there are so many uncheck it errors bet when i check it all it goes in, how can i put a default value if it is uncheck?
The major issue is, when you are dealing with a checkbox, then you have to check whether it is checked or not like:
$value = ''; // Default value
if( isset($_REQUEST['checkbox_name']) )
{
$value = $_REQUEST['checkbox_name'];
}
apart form that, your code is open to SQL Injections. And you are inserting data into table without providing column names. Any changes to the table will break the whole thing.
Input name is wrong, change it to txtFullname instead of txtfullname.
<label><b>Full Name</b></label> <input type="text" class="form-control" value="<?php echo $row['Lname'];?>, <?php echo $row['Fname'];?> <?php echo $row['Mname'];?>" name="txtFullname" required>
Also i fixed all you invalid html tags:
<?php
$id = $_GET['id'];
$sql = "SELECT * FROM tblstdpro where StdID = '$id'";
$result = mysqli_query($conn,$sql); $count = 0;
while($row = mysqli_fetch_array($result)){ ?>
<form action="AddMedicRecords.php" method="POST" enctype="multipart/form-data" style="border:1px solid #ccc">
<div class="box-body">
<div class="form-group"> <label><b>Student Image Location</b></label> <input type="text" class="form-control" value="<?php echo $row['StdImage'];?>" name="txtImage" readonly><br>
<label><b>LRN</b></label> <input type="text" class="form-control" value="<?php echo $row['StdID'];?>" name="txtID" required><br>
<label><b>Full Name</b></label> <input type="text" class="form-control" value="<?php echo $row['Lname'];?>, <?php echo $row['Fname'];?> <?php echo $row['Mname'];?>" name="txtFullname" required><br>
<label><b>Address</b></label><br> <input type="text" class="form-control" value="<?php echo $row['Street']; ?> , <?php echo $row['Barangay']; ?> <?php echo $row['Munic']; ?>, <?php echo $row['Province']; ?>" name="txtadd" required><br>
<label><b>Birthday</b></label> <input type="text" class="form-control" value="<?php echo $row['Bday'];?>" name="txtbday" required><br>
<label><b>Grade/ Course</b></label><br> <input type="text" class="form-control" value="<?php echo $row['Track'];?> - <?php echo $row['YearLvl'];?>" name="txtgrade" required><br>
<label><b>Height</b></label> <input type="" class="form-control" placeholder="Enter Height" name="txtheight" required><br>
<label><b>Weight</b></label> <input type="" class="form-control" placeholder="Enter Weight" name="txtweight" required><br>
<label><b>Blood Pressure</b></label> <input type="" class="form-control" placeholder="Enter BP" name="txtblood" required><br> <br>
<label><h3>*Person to be Notified in Case of Emergency</h3></label> <br>
<label><b>Name:</b></label> <input type="text" class="form-control" value="<?php echo $row['Mother'];?>" name="txtGname" required><br>
<label><b>Contact No.</b></label> <input type="text" class="form-control" value="<?php echo $row['Contact'];?>" name="txtContact"><br>
<label><h3>*Kindly Check the Box Provided on the Left Side</h3></label>
<table class="table table-hover">
<thead>
<tr>
<th>IMMUNIZATION</th>
<th>FAMILY HISTORY</th>
<th>PREVIOUS ILLNESS</th>
<th>PERSONAL HISTORY</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="chkBCG" value="BCG"> BCG</td>
<td><input type="checkbox" name="chkCardiac" value="Cardiac Disease"> Cardiac Disease</td>
<td><input type="checkbox" name="chkAsthma1" value="Asthma"> Asthma</td>
<td><input type="checkbox" name="" value="chkAlcohol"> Alcohol Use</td>
</tr>
<tr>
<td><input type="checkbox" name="chkOpv1" value="OPV 1"> OPV 1</td>
<td><input type="checkbox" name="chkThyroid" value="Thyroid Disease"> Thyroid Disease</td>
<td><input type="checkbox" name="chkChicken" value="Chicken Pox"> Chicken Pox</td>
<td><input type="checkbox" name="chkYosi" value="Tobacco Use"> Tobacco Use</td>
</tr>
<tr>
<td><input type="checkbox" name="chkOpv2" value="OPV 2"> OPV 2</td>
<td><input type="checkbox" name="chkDiabetes" value="Diabetes Mellitus"> Diabetes Mellitus</td>
<td><input type="checkbox" name="chkMeasles1" value="Measles"> Measles</td>
<td>Allergy to Food, Drugs,Etc..</td>
</tr>
<tr>
<td><input type="checkbox" name="chkOpv3" value="OPV 3"> OPV 3</td>
<td><input type="checkbox" name="chkHypertension" value="Hypertension"> Hypertension</td>
<td><input type="checkbox" name="chkMumps" value="Mumps"> Mumps</td>
<td><input type="text" class="form-control" placeholder="Enter Allergy to Food, Drugs,Etc.." name="txtFood1"> </td>
</tr>
<td><input type="checkbox" name="ChkDpt1" value="DPT1"> DPT 1</td>
<td><input type="checkbox" name="chkTuber" value="Tuberculosis"> Tuberculosis</td>
<td><input type="checkbox" name="chkUlcer" value="Peptic Ulcer"> Peptic Ulcer</td>
<td><input type="text" class="form-control" placeholder="Enter Allergy to Food, Drugs,Etc.." name="txtFood2"> </td>
</tr>
<td><input type="checkbox" name="chkDpt2" value="DPT 2"> DPT 2</td>
<td><input type="checkbox" name="chkAstma2" value="Asthma"> Asthma</td>
<td><input type="checkbox" name="chkDengue" value="Dengue"> Dengue</td>
<td><input type="text" class="form-control" placeholder="Enter Allergy to Food, Drugs,Etc.." name="txtFood3"> </td>
</tr>
<tr>
<td><input type="checkbox" name="chkDpt3" value="DPT 3"> DPT 3</td>
<td><input type="checkbox" name="chkKidney" value="Kidney Disease"> Kidney Disease</td>
<td><input type="checkbox" name="chkHead" value="Head Injury"> Head Injury</td>
<td>Any Identification Mark:</td>
</tr>
<tr>
<td><input type="checkbox" name="chkMeasles2" value="Measles"> Measles</td>
<td><input type="checkbox" name="chkCancer" value="Cancer"> Cancer</td>
<td><input type="checkbox" name="chkStd" value="STD"> STD</td>
<td><input type="checkbox" name="chkScar" value="Scar"> 1. Scar</td>
</tr>
<tr>
<td><input type="checkbox" name="chkHepa" value="HEPA-B"> HEPA-B</td>
<td><input type="checkbox" name="chkSkin" value="Skin Disease"> Skin Disease</td>
<td><input type="checkbox" name="chkHypertension2" value="Hypertension"> Hypertension</td>
<td><input type="checkbox" name="chkMole" value="Mole"> 2. Mole</td>
</tr>
<tr>
<td>Others:</td>
<td><input type="checkbox" name="chkStd2" value="STD"> STD</td>
<td><input type="checkbox" name="chkKidney2" value="Kidney Problem"> Kidney Problem</td>
<td><input type="checkbox" name="chkTattoo" value="Tattoo"> 3. Tattoo</td>
</tr>
<tr>
<td><input type="text" class="form-control" placeholder="Enter Others" name="txtOthersImmu"> </td>
<td></td>
<td>Others:</td>
<td><input type="checkbox" name="txtBirthmark" value="Birthmark"> 4. Birthmark</td>
</tr>
<tr>
<td><input type="text" class="form-control" placeholder="Enter Others" name="txtOthersImmu2"> </td>
<td></td>
<td><input type="text" class="form-control" placeholder="Enter Others" name="txtOthersIll"> </td>
<td></td>
</tr>
<tr>
<td><input type="text" class="form-control" placeholder="Enter Others" name="txtOthersImmu3"> </td>
<td></td>
<td><input type="text" class="form-control" placeholder="Enter Others" name="txtOthersIll2"> </td>
<td></td>
</tr>
</tbody>
</table> <label><b>FOR FEMALE ONLY: Date of Last Menstrual Period:</b></label>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"> <i class="fa fa-calendar"></i> </div> <input type="date" placeholder="Enter Birthday" class="form-control" data-inputmask="'alias': 'mm/dd/yyyy'" data-mask name="txtMens"> </div>
<!-- /.input group -->
</div>
<!-- /.form group -->
<div class="clearfix"> <button type="submit" name="submit" class="btn btn-block btn-primary btn-lg">Add Student</button> <button type="button" class="btn btn-block btn-danger btn-lg">Cancel</button> <br><br> </div>
</div>
</div>
</form>
<?php }?>

Want to search by product_id and fetch all the data based on product_id in form field

I'm building an Inventory management system but stuck with a search query.
I've tables named:
products
product_purchases
product_sales
Now, want to get product details from "products" table by searching "product_id" and fetch/show the product details in the appropriate HTML fields then add/insert the product details data into "product_purchases" table.
Similarly, want to get product details from "product_purchases" table by searching "product_purchase_id" and fetch/show the product details in the appropriate HTML fields then add/insert the product details data into "product_sales" table.
The problem is - I used $.post method to query my product details but it won't work.
Here I just added the screenshot of my purchase
code of my purchase page:
<form action="" method="post" class="form-search">
<div class="input-group">
<input type="text" class="form-control" name="product_id" id="product_id" placeholder="Search a product by ID">
<span class="input-group-btn">
<button name="searchProduct" value="Search" class="btn btn-primary">Search</button>
</span>
</div>
</form>
<form action="" method="post">
<fieldset id="general-settings">
<legend><h3 class="fields-heading">Item details</h3></legend>
<?php
if (isset($_POST['addPurchase'])){
$product_id = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_id']));
$product_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_name']));
$supplier_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['supplier_name']));
$brand_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['brand_name']));
$group_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['group_name']));
$unit_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['unit_price']));
$quantity = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['quantity']));
$bonus_quantity = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['bonus_quantity']));
$total_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['total_price']));
$total_bonus_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['total_bonus_price']));
$unit_sale_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['unit_sale_price']));
if ( empty($product_id) && empty($product_name) && empty($supplier_name) && empty($brand_name) && empty($group_name) && empty($unit_price) && empty($quantity) && empty($bonus_quantity) && empty($total_price) && empty($total_bonus_price) && empty($unit_sale_price) ){
echo "<p class='text-error'>Star (*) marks are required fields and cannot be empty!</p>";
}else{
$insert = $db->insert("purchases", "product_id, product_name, supplier_id, brand_id, group_id, unit_price, quantity, bonus_quantity, total_price, total_bonus_price, unit_sale_price", "'$product_id', '$product_name', '$supplier_name', '$brand_name', '$group_name', '$unit_price', '$quantity', '$bonus_quantity', '$total_price', $total_bonus_price, '$unit_sale_price'");
if($insert){ ?>
<script>
alert("Product added successfully!");
window.location.assign("inventory-purchase.php");
</script>
<?php
}else{
echo "<p class='text-error'>Product not added!</p>";
}
}
}
?>
<table class="table table-responsive table-settings">
<tbody>
<tr>
<div class="form-group">
<td><label for="product_id">Product ID <span class="required">*</span></label></td>
<td>:</td>
<td><input type="text" class="form-control" name="product_id" id="product_id"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="product_name">Name <span class="required">*</span></label></td>
<td>:</td>
<td><input type="text" class="form-control" name="product_name" id="product_name"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="supplier_name">Supplier <span class="required">*</span></label></td>
<td>:</td>
<td>
<select class="form-control" name="supplier_name" id="supplier_name">
<option>Select a supplier</option>
<?php $getsuppliers = $db->select("supplier_id, supplier_name", "suppliers");
if ($getsuppliers) {
while ( $getsplr = $getsuppliers->fetch_assoc() ){
?>
<option value="<?php echo $getsplr['supplier_id']; ?>"><?php echo $getsplr['supplier_name']; ?></option>
<?php } }?>
</select>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="brand_name">Brand <span class="required">*</span></label></td>
<td>:</td>
<td>
<select class="form-control" name="brand_name" id="brand_name">
<option>Select a brand</option> <?php $getbrands = $db->select("brand_id, brand_name", "brands");
if ($getbrands) {
while ( $getbrnd = $getbrands->fetch_assoc() ){
?>
<option value="<?php echo $getbrnd['brand_id']; ?>"><?php echo $getbrnd['brand_name']; ?></option>
<?php } }?>
</select>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="group_name">Group <span class="required">*</span></label></td>
<td>:</td>
<td>
<select class="form-control" name="group_name" id="group_name">
<option>Select a group</option>
<?php $getgroups = $db->select("group_id, group_name", "groups");
if ($getgroups) {
while ( $getgrp = $getgroups->fetch_assoc() ){
?>
<option value="<?php echo $getgrp['group_id']; ?>"><?php echo $getgrp['group_name']; ?></option>
<?php } }?>
</select>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="unit_price">Unit price <span class="required">*</span></label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="text" class="form-control" name="unit_price" id="unit_price">
</div>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="quantity">Quantity <span class="required">*</span></label></td>
<td>:</td>
<td><input type="text" class="form-control" name="quantity" id="quantity"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="bonus_quantity">Bonus Quantity</label></td>
<td>:</td>
<td><input type="text" class="form-control" name="bonus_quantity" id="bonus_quantity"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="total_price">Total price</label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input readonly type="text" class="form-control" name="total_price" id="total_price">
</div>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="total_bonus_price">Total bonus price</label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input readonly type="text" class="form-control" name="total_bonus_price" id="total_bonus_price">
</div>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="unit_sale_price">Unit sale price <span class="required">*</span></label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="text" class="form-control" name="unit_sale_price" id="unit_sale_price">
</div>
</td>
</div>
</tr>
<tr>
<td colspan="3"><input type="submit" name="addPurchase" value="Add purchase" class="btn btn-primary btn-lg btn-register"></td>
</tr>
</tbody>
</table>
</fieldset>
</form>
and sale product page.
ocde of my sales page
<form action="" method="post" class="form-search">
<div class="input-group">
<input type="text" class="form-control" name="product_id" id="product_id" placeholder="Search a product by ID">
<span class="input-group-btn">
<button name="searchProduct" value="Search" class="btn btn-primary">Search</button>
</span>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$product_id = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_id']));
$product_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_name']));
$unit_sale_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['unit_sale_price']));
$quantity = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['quantity']));
$total_sale_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['total_sale_price']));
if ( empty($product_id) && empty($quantity) ){
echo "<p class='text-error'>Star (*) marks are required fields and cannot be empty!</p>";
}else{
$insert = $db->insert("sales", "product_id, product_name, unit_sale_price, quantity, total_sale_price", "'$product_id', '$product_name', '$unit_price', '$quantity', '$total_sale_price'");
if($insert){ ?>
<script>
alert("Product sent to invoice successfully!");
window.location.assign("inventory-sale.php");
</script>
<?php
}else{
echo "<p class='text-error'>Product not sent to invoice!</p>";
}
}
}
?>
<form action="" method="post">
<fieldset id="general-settings">
<legend><h3 class="fields-heading">Item details</h3></legend>
<table class="table table-responsive table-settings">
<tbody>
<tr>
<div class="form-group">
<td><label for="product_id">Product ID <span class="required">*</span></label></td>
<td>:</td>
<td><input type="text" class="form-control" name="product_id" id="product_id" placeholder="Search an item by ID"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="product_name">Product name</label></td>
<td>:</td>
<td><input type="text" readonly class="form-control" name="product_name" id="product_name" value=""></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="unit_sale_price">Unit sale price</label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="number" readonly class="form-control" name="unit_sale_price" id="unit_sale_price" value="">
</div>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="quantity">Quantity <span class="required">*</span></label></td>
<td>:</td>
<td><input type="number" class="form-control" name="quantity" id="quantity" placeholder="Enter quantity"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="total_sale_price">Total price</label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input readonly type="text" disabled class="form-control" name="total_sale_price" id="total_sale_price">
</div>
</td>
</div>
</tr>
<tr>
<td colspan="3"><input type="submit" name="sale_invoice" value="Sent to invoice" class="btn btn-primary btn-lg btn-register"></td>
</tr>
</tbody>
</table>
</fieldset>
</form>
FYI: I'm working with PHP OOP, but not any framework.
Anybody around to help me out of this problem?
Try to use ajax request with post method to get data and populate in your forms and archive your next step
how to use ajax with OOP
It help you get data without refreshing and populate in your form fields. Same for PHP just call your controller with ajax request then same OOP way get data from database from Model and return data in JSON format and use in your fields.
Need more information feel free to ask :)

Insert to Database on HTML Form Submit

I have a form that allows a user to enter data. At the end, there is a submit button that can be pressed that submits the data entered, into a database. I had this working a few weeks ago but had to take a detour on a few other projects. I've come back to it now and the insertion doesn't seem to be working and I am not sure why.
So I am mainly focused on the bottom of the code with the SQL statements but have provided the other HTML for your reference. Do you see any problems that I may have in my SQL statements at all?
Also note that the page itself does not load if this section of sql code is not commented out:
INSERT INTO Stage_Rebate_Agreements
(
Terms,
Tier,
`Tier Minimum`,
Multiplier,
UOM,
Retro,
Guaranteed,
Pay
)
VALUES
(
'$_POST[rows[0][0][term]]',
'$_POST[tier]',
'$_POST[rows[0][0][purchase_minimum]]',
'$_POST[rows[0][0][multiplier]]',
'$_POST[rows[0][0][uom]]',
'$_POST[rows[0][0][retro]]',
'$_POST[rows[0][0][guaranteed]]',
'$_POST[rows[0][0][paid]]'
)
But if it is not commented out, the page does not load for some reason.
If we can somehow get this fixed, it would be much appreciated!
<?php
$host="xxxxxxxx";
$dbName="xxxxxxxxx";
$dbUser="xxxxxxx";
$dbPass="xxxxxxxxxxxxx";
$dbh = new PDO("sqlsrv:Server=$host;Database=$dbName", $dbUser, $dbPass);
$usersQuery = "SELECT MR_Name, MR_POC_N, MR_POC_E, MR_POC_P FROM Stage_Rebate_Master";
$users = $dbh->query($usersQuery);
?>
<html>
<body>
<div class="wrapper">
<header>
<h2>test</h2>
</header>
<div class="container">
<form name="form1" action="confirmation.php" method="POST">
<!-- Vendor -->
<section class="desc-block-left0" align="left">
<div>
<div id="vendor">
<strong>Vendor:</strong>
</div>
<div class="align">
<select name="vendor_dropdown" id="ven" onChange="updateinput();">
<option value="">Choose a Vendor</option>
<?php foreach($users->fetchAll() as $user): ?>
<option
data-name="<?php echo $user['MR_POC_N'];?>"
data-email="<?php echo $user['MR_POC_E'];?>"
data-phone="<?php echo $user['MR_POC_P'];?>"
>
<?php echo $user['MR_Name'];?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
</section>
<!-- Program Name -->
<section class="desc-block-right0">
<div>
<div id="program_name">
<strong>Program Name:</strong><br>
</div>
<div class="align1">
<input class="textbox" type="text" id="program_name1" name="program_name" style="width: 50%">
</div>
</div>
</section>
<!-- Program Period -->
<section class="desc-block-left1">
<div>
<div id="program_period">
<strong>Program Period:</strong>
</div>
<div class="align">
<input type="text" name="program_period_dropdown" id="period" value="<?php echo date("Y");?>" readonly style="width: 50px">
</div>
</div>
</section>
<!-- Terms -->
<section class="desc-block-right1">
<div>
<div id="terms">
<strong>Terms:</strong><br>
</div>
<div class="align1">
<input type="text" id="year" name="term" style="width: 50px">
<label for="percent">%</label>
<input type="text" id="year" name="term" style="width: 50px">
<label for="percent">Net</label>
<input type="text" id="year" name="term" style="width: 50px">
</div>
</div>
</section>
<!-- Vendor Type -->
<section class="desc-block-left2">
<div>
<div id="vendor_type">
<strong>Vendor Type:</strong><br>
</div>
<div class="align">
<table align="left">
<tr align="center">
<td><input type="radio" name="ven_type" value="Coded" id="type" checked="true"></td>
<td align="left">Coded</td>
</tr>
<tr align="center">
<td><input type="radio" name="ven_type" value="Non-Coded" id="type"></td>
<td>Non-Coded</td>
</tr>
</table>
</div>
</div>
</section>
<!-- Contact Information -->
<section class="desc-block-right2" align="center">
<div>
<div id="contact_info">
<strong>Contact Information:</strong><br>
</div>
<div class="align1">
<table align="left" id="contact">
<tr align="left">
<th>Name</th>
<th>Email</th>
<th>Phone Number</th>
</tr>
<tr>
<td><input type="text" id="name" class="name" name="name"></td>
<td><input type="email" id="email" class="email" name="email"></td>
<td><input type="tel" id="tel" class="tel" name="number"></td>
<td><input type="button" class="save" name="edit" value="Save"></td>
</tr>
</table>
</div>
</div>
</section>
<!-- 400 Rebate Tables -->
<!-- 400P -->
<div align="center" id="border">
<div id="rebate_400p">
<strong>400P</strong><br>
</div>
<table id="tables" cellspacing="5">
<tr align="center" class="table_titles">
<td>Tier</td>
<td>Purchase Minimum</td>
<td>Multiplier</td>
<td>UOM</td>
<td>Retro</td>
<td>Guaranteed</td>
<td>Paid</td>
<td>Delete?</td>
<td>Add Row</td>
</tr>
<tr>
<td align="center" name="tier" id="tier">1</td>
<td><input type="text" class="rebate_tables" id="purchase_min" data-name="purchase_minimum" name="rows[0][0][purchase_minimum]"></td>
<td><input type="text" class="rebate_tables" id="multiplier" data-name="multiplier" name="rows[0][0][multiplier]"></td>
<td><input type="text" class="rebate_tables" id ="uom" data-name="uom" name="rows[0][0][uom]"></td>
<td><input type="text" class="rebate_tables" id="retro" data-name="retro" name="rows[0][0][retro]"></td>
<td><input type="text" class="rebate_tables" id="guaranteed" data-name="guaranteed" name="rows[0][0][guaranteed]"></td>
<td><input type="text" class="rebate_tables" id="paid" data-name="paid" name="rows[0][0][paid]"></td>
<td><input type="button" class="delRow" value="Delete" onclick="deleteRow(this)"></td>
<td><input type="button" class="addmoreRowsbutton" value="Add row" onclick="insRow()"></td>
</tr>
</table>
<!-- 400M -->
<div id="rebate_400m">
<strong>400M</strong><br>
</div>
<table cellspacing="5" id="tables1" style="display: none;">
<tr align="center" class="table_titles">
<td>Tier</td>
<td>Purchase Minimum</td>
<td>Multiplier</td>
<td>UOM</td>
<td>Retro</td>
<td>Guaranteed</td>
<td>Paid</td>
<td>Delete?</td>
<td>Add Rows</td>
</tr>
<tr>
<td align="center" name="tier" id="tier1">1</td>
<td><input type="text" class="rebate_tables" id="purchase_min1" data-name="purchase_minimum" name="rows[1][0][purchase_minimum]"></td>
<td><input type="text" class="rebate_tables" id="multiplier1" data-name="multiplier" name="rows[1][0][multiplier]"></td>
<td><input type="text" class="rebate_tables" id ="uom1" data-name="uom" name="rows[1][0][uom]"></td>
<td><input type="text" class="rebate_tables" id="retro1" data-name="retro" name="rows[1][0][retro]"></td>
<td><input type="text" class="rebate_tables" id="guaranteed1" data-name="guaranteed" name="rows[1][0][guaranteed]"></td>
<td><input type="text" class="rebate_tables" id="paid1" data-name="paid" name="rows[1][0][paid]"></td>
<td><input type="button" class="delRow" value="Delete" onclick="deleteRow(this)"></td>
<td><input type="button" class="addmoreRowsbutton" value="Add row" onclick="insRow1()"></td>
</tr>
</table>
<!-- Button to display table for 400M -->
<input type="button" name="row" value="+" onclick="show2();"/>
<!-- 400D -->
<div id="rebate_400d">
<strong>400D</strong><br>
</div>
<table cellspacing="5" id="tables2" style="display: none;">
<tr align="center" class="table_titles">
<td>Tier</td>
<td>Purchase Minimum</td>
<td>Multiplier</td>
<td>UOM</td>
<td>Retro</td>
<td>Guaranteed</td>
<td>Paid</td>
<td>Delete?</td>
<td>Add Rows</td>
</tr>
<tr>
<td align="center" name="tier" id="tier2">1</td>
<td><input type="text" class="rebate_tables" id="purchase_min2" data-name="purchase_minimum" name="rows[2][0][purchase_minimum]"></td>
<td><input type="text" class="rebate_tables" id="multiplier2" data-name="multiplier" name="rows[2][0][multiplier]"></td>
<td><input type="text" class="rebate_tables" id ="uom2" data-name="uom" name="rows[2][0][uom]"></td>
<td><input type="text" class="rebate_tables" id="retro2" data-name="retro" name="rows[2][0][retro]"></td>
<td><input type="text" class="rebate_tables" id="guaranteed2" data-name="guaranteed" name="rows[2][0][guaranteed]"></td>
<td><input type="text" class="rebate_tables" id="paid2" data-name="paid" name="rows[2][0][paid]"></td>
<td><input type="button" class="delRow" value="Delete" onclick="deleteRow(this)"></td>
<td><input type="button" class="addmoreRowsbutton" value="Add row" onclick="insRow2()"></td>
</tr>
</table>
<!-- Button to adisplay table for 400D -->
<input type="button" name="row" value="+" id="plus-button" onclick="show3();"/>
</div>
<!-- Products -->
<div id="align">
<p>
<div id="products">
<strong>Products:</strong><br>
</div>
<input type="radio" name="product" value="All" onclick="hide();"/ checked> All
<input type="radio" name="product" value="Category" onclick="show();"/> Category
<input type="radio" name="product" value="Sku" onclick="hide();"/> Sku
<select multiple id="area" name="category" style="display: none;">
<option value="Example A" align ="center">Example A</option>
<option value="Example B" align ="center">Example B</option>
<option value="Example C" align ="center">Example C</option>
<option value="Example D" align ="center">Example D</option>
<option value="Example E" align ="center">Example E</option>
<option value="Example F" align ="center">Example F</option>
</select>
</p>
<!-- Submit Button -->
<br>
<input type="submit" value="Submit" id="btn" name="submit">
</div>
</form>
</div>
<div class="push"></div>
</div>
<div class="footer">
<footer>
<h3>test</h3>
</footer>
</div>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$host="xxxxxxxxxxx";
$dbName="xxxxxx";
$dbUser="xxxxxxxxxxxxxx";
$dbPass="xxxxxxxxxx";
$pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
$sql = "INSERT INTO Stage_Rebate_Master (MR_Name, MR_POC_N, MR_POC_E, MR_POC_P) VALUES ('$_POST[vendor_dropdown]','$_POST[name]','$_POST[email]','$_POST[number]')";
$sql1 = "INSERT INTO Stage_Rebate_Programs (Program_Year) VALUES ('$_POST[program_period_dropdown]')";
$sql2 = "INSERT INTO Stage_Rebate_Agreements (Terms, Tier, `Tier Minimum`, Multiplier, UOM, Retro, Guaranteed, Pay) VALUES ('$_POST[rows[0][0][term]]','$_POST[tier]','$_POST[rows[0][0][purchase_minimum]]','$_POST[rows[0][0][multiplier]]','$_POST[rows[0][0][uom]]','$_POST[rows[0][0][retro]]','$_POST[rows[0][0][guaranteed]]','$_POST[rows[0][0][paid]]')";
$sql3 = "INSERT INTO Stage_Rebate_Programs (Program_Name) VALUES ('$_POST[program_name]')";
$stmt = $pdo->prepare($sql);
$stmt1 = $pdo->prepare($sql1);
$stmt2 = $pdo->prepare($sql2);
$stmt3 = $pdo->prepare($sql3);
$result = $stmt->execute();
$result1 = $stmt1->execute();
$result2 = $stmt2->execute();
$result3 = $stmt3->execute();
echo json_encode($result);
echo json_encode($result1);
echo json_encode($result2);
echo json_encode($result3);
}
?>
remove the action=confirmation.php attribute in form ,as a result of which the bottom submit code is not working
<form name="form1" method="POST">
Thats enough

how to show data in textbox in php [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I have problem in this code. When I press "show button" I want data from database shows in text box and "modify" and "delete" buttons work.
But in this code when I press show button it shows error Notice: Undefined index: first_name, I am stuck in this code. How to resolve this?
include '../../../../database/dbConnection.php';
//-------------------------------------------
$row[0]="";
$isSaveDisabled = true;
$isCreateDisabled=false;
$isModifyDiasbled=true;
$isDeleteDisabled=true;
if (isset ($_POST['create_button'])) {
$isSaveDisabled = false;
$isCreateDisabled=true;
$sql="select ifnull(max(user_id),10000)+1 from user_master";
$res= mysql_query($sql);
$row= mysql_fetch_array($res);
$row[0];
}
if(isset($_POST['modify_button']))
{
}
if(isset($_POST['delete_button']))
{
}
?>
<!--
PHP CODE ENDS FROM HERE-------------------------
-->
<!--
BOOTSTRAP START FROM HERE-------------------------
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../../bootStrap/css/bootstrap.css">
<link rel="stylesheet" href="../../../../bootStrap/css/bootstrap.min.css">
<link rel="stylesheet" href="../../../../css/StyleSheet.css">
<script src="../../../../js/ValidateKeyPress.js"></script>
<title></title>
<script>
</script>
</head>
<body>
<p id="demo"></p>
<!-- <div class="page-header1">
Hello
</div>-->
<div class="container jumbotron ">
<div class=" text-center"><h4>USER CREATION</h4></div>
<form class="form-inline " action="" method="POST" name="myForm">
<div class="form-group1">
<label class="control-label" for="ec" id="myID">ENTITY CODE</label>
<select class="form-control2" name="ecode" id="myID">
<option value="">SELECT ENTITY CODE</option>
<option>DOC</option>
<option>EMP</option>
<option>NUR</option>
<option>ORG</option>
<option>TEC</option>
<option>VEN</option>
</select>
</div>
<div class="form-group1">
<label class="control-label" for="uid">USER ID</label>
<input type="text" class="form-control2" name="userid" value="<?php echo $row[0];?>">
</div>
<hr>
<table class="table1 table-borderless table-responsive">
<tbody>
<?php
if(isset($_POST['select_button']))
{
$qrydatabind='SELECT ecode, first_name, middle_name, last_name, father_name, mother_name,
number_of_dependents, dob, gender, identification_mark, marital_status, spouse_name, mobile_number,
email_id, adhar_id, pan_number, passport_number, tin_number, dl_number FROM USER_MASTER ORDER BY user_id DESC
LIMIT 1';
$results1= mysql_query($qrydatabind) or die(mysql_error());
while( $row = mysql_fetch_array( $results1 ) ) {
}
}
?>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="finame" id="myID1" >FIRST NAME</label></td>
<td><input value="<?php echo $row["first_name"]; ?>" type="text" class=" form-control2 input-sm text-uppercase"name="firstname" onkeypress="return allCharacter(event);" ></td>
<td><label class="control-label" for="mname">MIDDLE NAME</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="middlename"></td>
<td><label class="control-label" for="lname" id="myID2">LAST NAME</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="lastname" onkeypress="return allCharacter(event);"></td>
</tr>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="fname">FATHER NAME</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="fathername"></td>
<td><label class="control-label" for="moname">MOTHER NAME</label></td>
<td><input type="text" class="form-control2 input-sm text-uppercase" name="mothername"></td>
<td><label class="control-label" for="nod">NO.OF DEPENDENTS</label></td>
<td><input type="text" class="form-control2 input-sm text-uppercase" name="nod"></td>
</tr>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="dob" id="myID3">D.O.B</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="dob" placeholder="DD/MM/YYYY" id="myID3" ></td>
<td><label class="control-label" for="gen" id="myID4">GENDER</label></td>
<td><select id="myID4" class="form-control2" name="gender">
<option value="">SELECT GENDER</option>
<option>M</option>
<option>F</option>
<option>o</option>
</select></td>
<td><label class="control-label" for="idm">IDENTIFICATION MARK</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="idmark"></td>
</tr>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="ms">MARITAL STATUS</label></td>
<td><select class=" form-control2" name="mstatus">
<option value="">SELECT STATUS</option>
<option>M</option>
<option>U</option>
</select></td>
<td><label class="control-label" for="sname">SPOUSE NAME</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="spname"></td>
<td><label class="control-label" for="mno" id="myID5">MOBILE NO.</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="mobileno" maxlength="12" onkeypress="return allnumeric(event);"></td>
</tr>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="em">EMAIL-ID</label></td>
<td><input id="myID6" type="text" class="form-control2 input-sm text-uppercase" name="email" placeholder="sample#gmail.com"></td>
<td><label class="control-label" for="adhar">ADHAR-ID</label></td>
<td><input type="text" class="form-control2 input-sm text-uppercase" name="adharid"></td>
<td><label class="control-label" for="paname">PAN NUMBER</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="pannumber"></td>
</tr>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="passno">PASSPORT NO.</label></td>
<td><input type="text" class="form-control2 input-sm text-uppercase" name="passportno"></td>
<td><label class="control-label" for="tno">TIN NUMBER</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="tinnumber"></td>
<td><label class="control-label" for="dno">DL NUMBER</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="dlnumber"></td>
</tr>
</tbody>
</table>
<div class="btn-group-sm2 text-center">
<button type="submit" class="btn btn-primary" name="create_button" <?php echo $isCreateDisabled?'disabled':'';?>>CREATE</button>
<?php
if($isCreateDisabled) {
echo '<script>document.getElementById("myID").style.color = "red";</script>';
echo '<script>document.getElementById("myID1").style.color = "#ff0000";</script>';
echo '<script>document.getElementById("myID2").style.color = "#ff0000";</script>';
echo '<script>document.getElementById("myID3").style.color = "#ff0000";</script>';
echo '<script>document.getElementById("myID4").style.color = "#ff0000";</script>';
echo '<script>document.getElementById("myID5").style.color = "#ff0000";</script>';
echo '<script>document.getElementById("myID6").style.color = "#ff0000";</script>';
}
?>
<button type="submit" class="btn btn-primary" name="modify_button"<?php echo $isModifyDiasbled?'disabled':'';?>>MODIFY</button>
<button type="submit" class="btn btn-primary" name="delete_button" <?php echo $isDeleteDisabled?'disabled':'';?>>DELETE</button>
<button type="submit" class="btn btn-primary" name="clear_button">CLEAR</button>
<button type="submit" class="btn btn-primary " name="save_button" <?php echo $isSaveDisabled?'disabled':''; ?> onclick="return validateForm();">SAVE</button>
<button type="submit" class="btn btn-primary" name="exit_button">EXIT</button>
<button type="submit" class="btn btn-default text-right" name="search_button">SEARCH</button>
<button type="submit" class="btn btn-default text-right" name="select_button">SHOW</button>
</div>
</form>
</div>
</tbody>
<div class="table-responsive">
<table class="table">
<?php
if(isset($_POST['save_button'])){
$isCreateDisabled=false;
if(isset($_POST['ecode']) && isset($_POST['firstname'])&& isset($_POST['middlename'])&& isset($_POST['lastname'])
&& isset($_POST['fathername'])&& isset($_POST['mothername'])&& isset($_POST['nod']) && isset($_POST['dob'])
&& isset($_POST['gender'])&& isset($_POST['idmark'])&& isset($_POST['mstatus'])&& isset($_POST['spname'])
&& isset($_POST['mobileno'])&& isset($_POST['email'])&& isset($_POST['adharid'])&& isset($_POST['pannumber'])
&& isset($_POST['passportno'])&& isset($_POST['tinnumber'])&& isset($_POST['dlnumber']))
{
$ecode=$_POST['ecode']; $first_name=$_POST['firstname']; $middle_name=$_POST['middlename'];
$last_name=$_POST['lastname']; $father_name=$_POST['fathername']; $mother_name=$_POST['mothername'];
$number_of_dependents=$_POST['nod']; $dob=$_POST['dob']; $gender=$_POST['gender'];
$identification_mark=$_POST['idmark']; $marital_status=$_POST['mstatus'];
$spouse_name=$_POST['spname']; $mobile_number=$_POST['mobileno']; $email_id=$_POST['email'];
$adhar_id=$_POST['adharid'];$pan_number=$_POST['pannumber']; $passport_number=$_POST['passportno'];
$tin_number=$_POST['tinnumber']; $dl_number=$_POST['dlnumber'];
}
$qry="insert into user_master(ecode, first_name, middle_name, last_name, father_name, mother_name,
number_of_dependents, dob, gender, identification_mark, marital_status, spouse_name, mobile_number,
email_id, adhar_id, pan_number, passport_number, tin_number, dl_number)
VALUES('$ecode','$first_name','$middle_name','$last_name','$father_name','$mother_name',
'$number_of_dependents',str_to_date('$dob','%d/%m/%Y'),'$gender','$identification_mark','$marital_status',
'$spouse_name','$mobile_number','$email_id','$adhar_id','$pan_number',
'$passport_number','$tin_number','$dl_number')";
$resultss= mysql_query($qry) or die(mysql_error());
if($resultss)
{
echo "<script>
alert('SuccessFully');
</script>";
}
else
{
return "Error...! Not Inserted.";
}
$qrydatabind='SELECT ecode, first_name, middle_name, last_name, father_name, mother_name,
number_of_dependents, dob, gender, identification_mark, marital_status, spouse_name, mobile_number,
email_id, adhar_id, pan_number, passport_number, tin_number, dl_number FROM USER_MASTER ORDER BY user_id DESC
LIMIT 1';
$results= mysql_query($qrydatabind) or die(mysql_error());
while( $row = mysql_fetch_array( $results ) ) {
echo
"
<div class='table-responsive'>
<table border='1' style= 'background-color: #84ed86; color: #761a9b; ' >
<thead>
<tr>
<th></th>
<th>Entity Code</th>
<th>User Id</th> <th>User Name</th> <th>Father Name</th> <th>Mother Name</th> <th>No.Of Dependents</th>
<th>D.O.B</th> <th>GENDER</th> <th>Id Mark</th> <th>MARITAL STATUS</th> <th>SPOUSE NAME</th>
<th>Mob. Number</th> <th>E-Id</th> <th>ADHAR-ID</th> <th>PAN-No.</th> <th>PASSPORT-No.</th>
<th>TIN-NO.</th> <th>DL-No.</th>
</tr>
</thead>
<tr >
<td> </td>
<td>{$row['ecode']}</td> <td> echo $row[0];</td>
<td>{$row['first_name']} {$row['middle_name']} {$row['last_name']}</td>
<td>{$row['father_name']}</td> <td>{$row['mother_name']}</td>
<td>{$row['number_of_dependents']}</td> <td>{$row['dob']}</td>
<td>{$row['gender']}</td> <td>{$row['identification_mark']}</td>
<td>{$row['marital_status']}</td> <td>{$row['spouse_name']}</td>
<td>{$row['mobile_number']}</td> <td>{$row['email_id']}</td>
<td>{$row['adhar_id']}</td> <td>{$row['pan_number']}</td>
<td>{$row['passport_number']}</td> <td>{$row['tin_number']}</td>
<td>{$row['dl_number']}</td>
</tr> </table>
</div>";
}}
?>
</table>
</div>
</body>
</html>
If edits and delete work that means your select statement is wrong. Maybe u are not properly declaring your fields in your select statement. Id look into that first. What I do when my sql is on error is open up an sql session and try doing the query straight from the sql editor.
Also,
why are you using limit 1? You are fetching a result of records for an array but you are limiting your query to one row?
EDIT:
Dont you have to select user_id in order to use ORDER BY? All you have to do is add user_id to select statement

Saving a multidimensional array into a mysql database

I am working on a online time tracking web page. But i am stuck at the part on transferring the data into the database.
<?php
/* This loop will iterate through all days. */
foreach($_POST["startTime"] as $day=>$startTimes){
/* This loop will give start & end times for a particular day, i.e. $day */
foreach($startTimes as $timeIndex=>$startTime){
$endTime = $_POST["endTime"][$day][$timeIndex];
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$sql = "INSERT INTO timetableschedule (name, day, startTime, endTime) ".
"VALUES ('$name', '$day', '$startTime', '![enter image description here][1]$endTime')";
if (!mysqli_query($con, $sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
}
}
}
?>
The form looks like this,
<table id="dataTable" class="form-control">
<label for="Monday">Monday</label>
<input type="button" value="Add Schedule" onClick="addRow('dataTable')" />
<tbody>
<tr>
<p>
<td>
<label>Start Time</label>
<input type="text" class="form-control" name="startTime[1][]">
</td>
<td>
<label>End Time</label>
<input type="text" class="form-control" name="endTime[1][]">
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="form-group">
<div class="col-sm-7">
<table id="dataTable1" class="form-control">
<label for="Monday">Tuesday</label>
<input type="button" value="Add Schedule" onClick="addRow('dataTable1')" />
<tbody>
<tr>
<p>
<td>
<label>Start Time</label>
<input type="text" class="form-control" name="startTime[2][]">
</td>
<td>
<label>End Time</label>
<input type="text" class="form-control" name="endTime[2][]">
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="form-group">
<div class="col-sm-7">
<table id="dataTable2" class="form-control">
<label for="Monday">Wednesday</label>
<input type="button" value="Add Schedule" onClick="addRow('dataTable2')" />
<tbody>
<tr>
<p>
<td>
<label>Start Time</label>
<input type="text" class="form-control" name="startTime[3][]">
</td>
<td>
<label>End Time</label>
<input type="text" class="form-control" name="endTime[3][]">
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="form-group">
<div class="col-sm-7">
<table id="dataTable3" class="form-control">
<label for="Monday">Thursday</label>
<input type="button" value="Add Schedule" onClick="addRow('dataTable3')" />
<tbody>
<tr>
<p>
<td>
<label>Start Time</label>
<input type="text" class="form-control" name="startTime[4][]">
</td>
<td>
<label>End Time</label>
<input type="text" class="form-control" name="endTime[4][]">
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="form-group">
<div class="col-sm-7">
<table id="dataTable4" class="form-control">
<label for="Monday">Friday</label>
<input type="button" value="Add Schedule" onClick="addRow('dataTable4')" />
<tbody>
<tr>
<p>
<td>
<label>Start Time</label>
<input type="text" class="form-control" name="startTime[5][]">
</td>
<td>
<label>End Time</label>
<input type="text" class="form-control" name="endTime[5][]">
</td>
</tr>
</tbody>
</table>
The database should look like this,
But the only the first row of data managed to enter the database. I am not sure where when wrong with my php codes.
Change your html to this
<td>
<label>Start Time</label>
<input type="text" class="form-control" name="time[0]['start']">
</td>
<td>
<label>End Time</label>
<input type="text" class="form-control" name="time[0]['end']">
</td>
Rest indices will be
time[1]['start']
time[1]['end']
and so on
Then your php code will be easier to read
foreach($_POST['time'] as $day => $time) {
$sql = "INSERT INTO timetableschedule (name, day, startTime, endTime) ".
"VALUES ('$name', '$day', '" . $time['start'] . "', '" . $time['end'] . "')";
}

Categories