I'm trying to insert data from a form to my database but it doesn't seem to work. I've put an echo after the insert query so I can verify that the data was inserted but it doesn't echo what I've written. Is there a problem with my query or any part of my php?
My PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stat_system";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$fname = $lname = $mname = $contact = $age = $attain = $course = $school = $position = $exp = $ref = $batchtxt = $hiredate = $prevbpo = $remarks = $nho = $nonbpo = $holdAttain = $holdPos = "";
$error_flag = 0;
if (isset($_POST['submit'])) {
$holdAttain = (isset($_POST['cmbAttain']));
$holdPos = (isset($_POST['cmbPosition']));
if (!empty($_POST['firstname'])) {
$fname = $_POST['firstname'];
}
if (!empty($_POST['lastname'])) {
$lname = $_POST['lastname'];
}
if (!empty($_POST['middlename'])) {
$mname = $_POST['middlename'];
}
if (!empty($_POST['contact'])) {
$contact = $_POST['contact'];
}
if (!empty($_POST['age'])) {
$age = $_POST['age'];
}
if (isset($_POST['cmbAttain'])) {
$attain = $_POST['cmbAttain'];
}
if(isset($_POST['school'])) {
$school = $_POST['school'];
}
if(isset($_POST['course'])) {
$course = $_POST['course'];
}
if (isset($_POST['exp'])) {
$exp = $_POST['exp'];
}
if (!empty($_POST['remarks'])) {
$remarks = $_POST['remarks'];
}
if (isset($_POST['nonbpo'])) {
$nonbpo = $_POST['nonbpo'];
}
if (isset($_POST['prevbpo'])) {
$prevbpo = $_POST['prevbpo'];
}
if (!empty($_POST['ref'])) {
$ref = $_POST['ref'];
}
if (isset($_POST['hiredate'])) {
$hiredate = $_POST['hiredate'];
}
if (isset($_POST['batchtxt'])) {
$batchtxt = $_POST['batchtxt'];
}
if (!empty($_POST['nho'])) {
$nho = $_POST['nho'];
}
if($error_flag == 0){
$sql = mysqli_query($conn,"INSERT INTO applicants (appID, appLastName, appFirstName, appMidleName, Age, appPhoneNumber, appBatch, appExperience, appRemarks, appPreviousBPO, appSchool, appCourse, appGraduate, appNonBPO, appPosition, appHireDate, appNHO, appReferrer)
VALUES (NULL, '$lname', '$fname', '$mname', $age, '$contact', $batchtxt, '$exp', $remarks, '$school', '$course', '$attain', '$nonbpo', '$position', $hiredate, $nho, '$ref')");
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#succModal').modal('show');
});
</script>";
$fname = $lname = $mname = $contact = $age = $attain = $course = $school = $batchtxt = $ref = $hiredate = $position = $exp = $prevbpo = $remarks = $nho = $nonbpo = $holdAttain = $holdPos = "";
}
else {
print '<script type="text/javascript">';
print 'alert("Please fill in all the fields!")';
print '</script>';
}
}
mysqli_close($conn);
?>
My HTML:
<div id="addApplicant" class="addApp-marginleft" style="height:1000px">
<form id="registration" class="form-horizontal" method="post" action="index.php">
<div class="row">
<div align="center">
<h3>Add Applicant</h3>
<br>
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>First name:</small></label>
<div class="col-sm-4">
<input required type="text" name="firstname" autocomplete="off" placeholder="Firstname" id="firstname" class="form-control" value="<?php echo $fname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small> Last name:</small></label>
<div class="col-sm-4">
<input required type="text" name="lastname" autocomplete="off" id="lastname" placeholder="Lastname" class="form-control" value="<?php echo $lname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Middle name:</small></label>
<div class="col-sm-4">
<input required type="text" name="middlename" autocomplete="off" id="middlename" placeholder="middlename" class="form-control" value="<?php echo $mname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Contact number:</small></label>
<span id="errmsg"></span>
<div class="col-sm-4">
<input required type="text" name="contactnum" autocomplete="off" onkeypress="return isNumber(event)" placeholder="Contact number" id="contact" class="form-control" maxlength="11" value="<?php echo $contact;?>"/>
</div>
<?php
echo '<script>';
echo 'function isNumber(evt) {';
echo 'evt = (evt) ? evt : window.event;';
echo 'var charCode = (evt.which) ? evt.which : evt.keyCode;';
echo 'if (charCode > 31 && (charCode < 48 || charCode > 57)) {';
echo 'return false;';
echo '}';
echo 'return true;';
echo '}';
echo '</script>';
?>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Age:</small></label>
<div class="col-sm-4">
<input required type="text" autocomplete="off" placeholder="age" name="age" id="age" class="form-control" value="<?php echo $age;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Graduate:</small></label>
<div class="col-sm-4">
<select required name="cmbAttain" id="cmbAttain" class="form-control" onChange="disableCmb();">
<option value="">Choose</option>
<option value="Yes" <?php if($holdAttain == "Yes") echo "selected"; ?>>Yes</option>
<option value="No" <?php if($holdAttain == "No") echo "selected"; ?>>No</option>
</select>
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>School:</small></label>
<div class="col-sm-4">
<input type="text" name="school" autocomplete="off" id="school" placeholder="School" class="form-control" value="<?php echo $school;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Course:</small></label>
<div class="col-sm-4">
<input type="text" name="course" autocomplete="off" id="course" placeholder="Course" class="form-control" value="<?php echo $course;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Batch:</small></label>
<div class="col-sm-4">
<input type="text" name="batchtxt" autocomplete="off" id="batchtxt" placeholder="Batch" class="form-control" value="<?php echo $batchtxt;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Experience:</small></label>
<div class="col-sm-4">
<input type="text" name="exp" autocomplete="off" id="exp" placeholder="Experience" class="form-control" value="<?php echo $exp;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Previous BPO:</small></label>
<div class="col-sm-4">
<input type="text" name="prevbpo" autocomplete="off" id="prevbpo" placeholder="Previous BPO" class="form-control" value="<?php echo $prevbpo;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Non-BPO:</small></label>
<div class="col-sm-4">
<input type="text" name="nonbpo" autocomplete="off" id="nonbpo" placeholder="Non-BPO" class="form-control" value="<?php echo $nonbpo;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Remarks:</small></label>
<div class="col-sm-4">
<input type="text" name="remarks" autocomplete="off" id="remarks" placeholder="Remarks" class="form-control" value="<?php echo $remarks;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Hire Date:</small></label>
<div class="col-sm-4">
<input type="date" name="hiredate" id="hiredate" class="form-control" autocomplete="off" value="<?php echo $hiredate;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Position:</small></label>
<div class="col-sm-4">
<select required name="cmbPosition" id="cmbPosition" class="form-control" data-size="5" >
<option selected value="">Choose</option>
<option value="Customer Service Representative" <?php if($holdPos == "Customer Service Representative") echo "selected"; ?>>Customer Service Representative</option>
<option value="Image Enhancer" <?php if($holdPos == "Image Enhancer") echo "selected"; ?>>Image Enhancer</option>
</select>
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>NHO:</small></label>
<div class="col-sm-4">
<input type="date" name="nho" id="nho" class="form-control" autocomplete="off" value="<?php echo $nho;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Referrer:</small></label>
<div class="col-sm-4">
<input type="text" name="ref" autocomplete="off" id="ref" placeholder="Name of Referrer" class="form-control" value="<?php echo $ref;?>">
</div>
</div>
<div class="form-group" align="center" >
<div class="col-sm-4">
</div>
<div class="col-sm-4">
<div class="btn-group " role="group" aria-label="...">
<input id="submitbtn" type="submit" name="submit" class="btn btn btn-success" value="Submit" data-target="#succModal">
<input type="reset" name="reset" class="btn btn-warning" value="Clear">
</div>
</div>
<div class="col-sm-4">
</div>
</div>
</form>
</div>
<div class="container">
<!-- Register Success Modal -->
<div class="modal fade" id="succModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body" align="center">
<p>REGISTRATION SUCCESSFUL</p>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
use $_POST instead of $_REQUEST..
like:
$lname = $_POST['lname'];
Just at a glance, you're NULL tests should be seperated by &&, not ||. The way you have it, it's only checking whether one (any) of them contain a value.
There is some errors with you query, just replace it with
$sql = "INSERT INTO applicants (appLastName, appFirstName, appMidleName, Age, appPhoneNumber, appBatch, appExperience, appRemarks, appPreviousBPO, appSchool, appCourse, appGraduate, appNonBpo, appPosition, appHireDate, appNHO, appReferrer) VALUES ('$lname','$fname','$mname',$age,'$con',$batch,'$exp','$rem','$prevbpo','$school','$course','$gradsit', '$nbpo', '$pos', '$hdate', '$nho', '$ref')";
here, there is no need to add appid as it is auto-incremented. Also there are some missing single quotes.
Skip to send value NULL in your insert statement
where you check whether $_POST is empty, if it is empty set $error_flag=1
Inside if (isset($_POST['submit'])) { print $_POST
Hope this will help to debug. If still issue, check your error_log
Related
I'm having problem trying to edit/update my product page which has multiple images using php prepared statement. if i have only one image in the image field it will update and unlink the oldimage successfully but since i have added 4(four) more images, updating this images is giving me tough time. Please can someone correct me where i'm wrong on this?
Below are the INSERT PAGE AND EDIT PAGE
Here is the code i used to insert product to database
<?php
include 'admin_config.php';
if (isset($_POST['view'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$make = $_POST['make'];
$model = $_POST['model'];
$price = $_POST['price'];
$loc = $_POST['loc'];
$yr = $_POST['yr'];
$condi = $_POST['condi'];
$trans = $_POST['trans'];
$mileage = $_POST['mileage'];
$isfeatured = $_POST['isfeatured'];
$wheel = $_POST['wheel'];
$details = $_POST['details'];
$photo=$_FILES['image']['name'];
$upload="uploads/".$photo;
$photo2=$_FILES['image2']['name'];
$upload2="uploads/".$photo2;
$photo3=$_FILES['image3']['name'];
$upload3="uploads/".$photo3;
$photo4=$_FILES['image4']['name'];
$upload4="uploads/".$photo4;
$photo5=$_FILES['image5']['name'];
$upload5="uploads/".$photo5;
// form validation: ensure that the form is correctly filled
$sql = "INSERT INTO vehicle (id,title,make,model,price,loc,yr,condi,trans,mileage,isfeatured,wheel,details,photo,photo2,photo3,photo4,photo5) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$stmt=$conn->prepare($sql);
$stmt->bind_param("isssssisssisssssss",$id,$title,$make,$model,$price,$loc,$yr,$condi,$trans,
$mileage,$isfeatured,$wheel,$details,$upload,$upload2,$upload3,$upload4,$upload5);
$stmt->execute();
move_uploaded_file($_FILES['image']['tmp_name'], $upload);
move_uploaded_file($_FILES['image2']['tmp_name'], $upload2);
move_uploaded_file($_FILES['image3']['tmp_name'], $upload3);
move_uploaded_file($_FILES['image4']['tmp_name'], $upload4);
move_uploaded_file($_FILES['image5']['tmp_name'], $upload5);
$_SESSION['message'] = "Vehicle Added Successfully!";
}
?>
<section class="section">
<div class="row">
<div class="col-lg-12">
<?php include ('message.php'); ?>
<div class="card">
<div class="card-body">
<a href="add_property.php" class="btn btn-primary m-3" width="200px" >Add New Vehicle</a>
<hr>
<!-- No Labels Form -->
<form action="./add_vehicle.php" method="POST" class="row g-3" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<div class="col-md-6">
<label for="title" class="form-label">Title</label>
<input type="text" name="title" class="form-control">
</div>
<div class="col-md-6">
<label for="make" class="form-label">Make</label>
<input type="text" name="make" class="form-control">
</div>
<div class="col-md-6">
<label for="model" class="form-label">Model</label>
<input type="text" name="model" class="form-control">
</div>
<div class="col-md-6">
<label for="price" class="form-label">Price</label>
<input type="text" name="price" class="form-control">
</div>
<div class="col-md-6">
<label for="loc" class="form-label">Location</label>
<input type="text" name="loc" class="form-control">
</div>
<div class="col-md-6">
<label for="yr" class="form-label">Year</label>
<input type="text" name="yr" class="form-control">
</div>
<div class="col-md-6">
<label for="condi" class="form-label">Condition</label>
<select id="inputState" name="condi" class="form-select">
<option selected disabled value="">Select Condition</option>
<option value="New">New</option>
<option value="Foreign Used">Foreign Used</option>
<option value="Nigeria Used">Nigeria Used</option>
</select>
</div>
<div class="col-md-6">
<label for="trans" class="form-label">Transmission</label>
<select id="inputState" name="trans" class="form-select">
<option selected disabled value="">Select Transmission</option>
<option value="Automatic">Automatic</option>
<option value="Manual">Manual</option>
</select>
</div>
<div class="col-md-6">
<label for="mileage" class="form-label">Mileage</label>
<input type="text" name="mileage" class="form-control">
</div>
<div class="col-md-6">
<label for="isfeatured" class="form-label">isFeatured</label>
<select id="inputState" name="isfeatured" class="form-select">
<option selected disabled value="">Select isFeature</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
<div class="col-md-6">
<label for="wheel" class="form-label">Wheel</label>
<input type="text" name="wheel" class="form-control">
</div>
<div class="col-md-12">
<label for="details" class="form-label">Description</label>
<textarea id="default" class="form-control" rows="8" name="details"></textarea>
</div><br><br>
<div class="col-md-6 justify-content-center">
<label for="customFile" class="form-label">Photo</label>
<input type="file" name="image" class="custom-file">
</div><br><br>
<div class="col-md-6 justify-content-center">
<label for="customFile" class="form-label">Photo2</label>
<input type="file" name="image2" class="custom-file">
</div><br><br>
<div class="col-md-6 justify-content-center">
<label for="customFile" class="form-label">Photo3</label>
<input type="file" name="image3" class="custom-file">
</div><br><br>
<div class="col-md-6 justify-content-center">
<label for="customFile" class="form-label">Photo4</label>
<input type="file" name="image4" class="custom-file">
</div><br><br>
<div class="col-md-6 justify-content-center">
<label for="customFile" class="form-label">Photo5</label>
<input type="file" name="image5" class="custom-file">
</div><br><br>
<div class="text-center">
<input type="submit" name="view" class="btn btn-primary btn-block" value="Add Product">
<input type="reset" name="clear" class="btn btn-secondary btn-block" value="Reset Product">
</div>
</form>
<!-- End No Labels Form -->
</div>
</div>
</div>
</div>
</section>
</main><!-- End #main -->
AND HERE IS MY EDIT PAGE
<?php
include 'admin_config.php';
$update=false;
$id = '';
$title = '';
$make = '';
$model = '';
$price = '';
$loc = '';
$yr = '';
$condi = '';
$trans = '';
$mileage = '';
$isfeatured = '';
$wheel = '';
$details = '';
$photo = '';
$photo2 = '';
$photo3 = '';
$photo4 = '';
$photo5 = '';
if(isset($_GET['id'])) {
$id=$_GET['id'];
$sql="SELECT * FROM vehicle WHERE id=?";
$stmt=$conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result=$stmt->get_result();
$row=$result->fetch_assoc();
$id = $row['id'];
$title = $row['title'];
$make = $row['make'];
$model = $row['model'];
$price = $row['price'];
$loc = $row['loc'];
$yr = $row['yr'];
$condi = $row['condi'];
$trans = $row['trans'];
$mileage = $row['mileage'];
$isfeatured = $row['isfeatured'];
$wheel = $row['wheel'];
$details = $row['details'];
$photo = $row['photo'];
$photo2 = $row['photo2'];
$photo3 = $row['photo3'];
$photo4 = $row['photo4'];
$photo5 = $row['photo5'];
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$make = $_POST['make'];
$model = $_POST['model'];
$price = $_POST['price'];
$loc = $_POST['loc'];
$yr = $_POST['yr'];
$condi = $_POST['condi'];
$trans = $_POST['trans'];
$mileage = $_POST['mileage'];
$isfeatured = $_POST['isfeatured'];
$wheel = $_POST['wheel'];
$details = $_POST['details'];
$oldimage = $_POST['oldimage'];
$oldimage2 = $_POST['oldimage2'];
$oldimage3 = $_POST['oldimage3'];
$oldimage4 = $_POST['oldimage4'];
$oldimage5 = $_POST['oldimage5'];
if(isset($_FILES['image']['name'])&&($_FILES['image']['name']!="")){
$newimage="uploads/".$_FILES['image']['name'];
unlink($oldimage);
move_uploaded_file($_FILES['image']['tmp_name'], $newimage);
}
if(isset($_FILES['image2']['name2'])&&($_FILES['image2']['name2']!="")){
$newimage2="uploads/".$_FILES['image2']['name2'];
unlink($oldimage2);
move_uploaded_file($_FILES['image2']['tmp_name2'], $newimage2);
}
if(isset($_FILES['image3']['name3'])&&($_FILES['image3']['name3']!="")){
$newimage3="uploads/".$_FILES['image3']['name3'];
unlink($oldimage3);
move_uploaded_file($_FILES['image']['tmp_name'], $newimage3);
}
if(isset($_FILES['image4']['name4'])&&($_FILES['image4']['name4']!="")){
$newimage4="uploads/".$_FILES['image4']['name4'];
unlink($oldimage4);
move_uploaded_file($_FILES['image4']['tmp_name4'], $newimage4);
}
if(isset($_FILES['image5']['name5'])&&($_FILES['image5']['name5']!="")){
$newimage5="uploads/".$_FILES['image5']['name5'];
unlink($oldimage);
move_uploaded_file($_FILES['image5']['tmp_name5'], $newimage5);
}
else{
$newimage=$oldimage;
$newimage2=$oldimage2;
$newimage3=$oldimage3;
$newimage4=$oldimage4;
$newimage5=$oldimage5;
}
// form validation: ensure that the form is correctly filled
$sql = "UPDATE vehicle SET title=?,make=?,model=?,price=?,loc=?,yr=?,condi=?,trans=?,mileage=?,isfeatured=?,
wheel=?,details=?,photo=?,photo2=?,photo3=?,photo4=?,photo5=? WHERE id=?";
$stmt=$conn->prepare($sql);
$stmt
>bind_param("sssssisssisssssssi",$title,$make,$model,$price,$loc,$yr,$condi,$trans,$mileage,$isfeatured,
$wheel,$details,$upload,$upload2,$upload3,$upload4,$upload5,$id);
$stmt->execute();
$_SESSION['message'] = "Vehicle Updated Successfully!";
}
?>
<section class="section">
<div class="row">
<div class="col-lg-12">
<?php include ('message.php'); ?>
<div class="card">
<div class="card-body">
<a href="edit_vehicle.php" class="btn btn-primary m-3" width="200px" >Add New Vehicle</a>
<hr>
<!-- No Labels Form -->
<form action="./edit_vehicle.php" method="POST" class="row g-3" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<div class="col-md-6">
<label for="title" class="form-label">Title</label>
<input type="text" name="title" class="form-control" value="<?=$title; ?>">
</div>
<div class="col-md-6">
<label for="make" class="form-label">Make</label>
<input type="text" name="make" class="form-control" value="<?=$make; ?>">
</div>
<div class="col-md-6">
<label for="model" class="form-label">Model</label>
<input type="text" name="model" class="form-control" value="<?=$model; ?>">
</div>
<div class="col-md-6">
<label for="price" class="form-label">Price</label>
<input type="text" name="price" class="form-control" value="<?=$price; ?>">
</div>
<div class="col-md-6">
<label for="loc" class="form-label">Location</label>
<input type="text" name="loc" class="form-control" value="<?=$loc; ?>">
</div>
<div class="col-md-6">
<label for="yr" class="form-label">Year</label>
<input type="text" name="yr" class="form-control" value="<?=$yr; ?>">
</div>
<div class="col-md-6">
<label for="condi" class="form-label">Condition</label>
<input type="text" name="condi" class="form-control" value="<?=$condi; ?>">
</div>
<div class="col-md-6">
<label for="trans" class="form-label">Transmission</label>
<input type="text" name="trans" class="form-control" value="<?=$trans; ?>">
</div>
<div class="col-md-6">
<label for="mileage" class="form-label">Mileage</label>
<input type="text" name="mileage" class="form-control" value="<?=$mileage; ?>">
</div>
<div class="col-md-6">
<label for="isfeatured" class="form-label">isFeatured</label>
<input type="text" name="isfeatured" class="form-control" value="<?=$isfeatured; ?>">
</div>
<div class="col-md-6">
<label for="wheel" class="form-label">Wheel</label>
<input type="text" name="wheel" class="form-control" value="<?=$wheel; ?>">
</div>
<div class="col-md-12">
<label for="details" class="form-label">Description</label>
<textarea id="default" class="form-control" rows="8"><?=$details; ?></textarea>
</div><br><br>
<div class="col-md-6 justify-content-center">
<input type="hidden" name="oldimage" value="<?= $photo; ?>">
<input type="file" name="image" class="custom-file">
<img src="<?= $photo; ?>" width="150" class="img-thumbnail">
</div><br><br>
<div class="col-md-6 justify-content-center">
<input type="hidden" name="oldimage2" value="<?= $photo2; ?>">
<input type="file" name="image2" class="custom-file">
<img src="<?= $photo2; ?>" width="150" class="img-thumbnail">
</div><br><br>
<div class="col-md-6 justify-content-center">
<input type="hidden" name="oldimage3" value="<?= $photo3; ?>">
<input type="file" name="image3" class="custom-file">
<img src="<?= $photo3; ?>" width="150" class="img-thumbnail">
</div><br><br>
<div class="col-md-6 justify-content-center">
<input type="hidden" name="oldimage4" value="<?= $photo4; ?>">
<input type="file" name="image4" class="custom-file">
<img src="<?= $photo4; ?>" width="150" class="img-thumbnail">
</div><br><br>
<div class="col-md-6 justify-content-center">
<input type="hidden" name="oldimage5" value="<?= $photo5; ?>">
<input type="file" name="image5" class="custom-file">
<img src="<?= $photo5; ?>" width="150" class="img-thumbnail">
</div><br><br>
<div class="text-center">
<input type="submit" name="update" class="btn btn-primary btn-block" value="Update Vehicle">
<input type="reset" name="clear" class="btn btn-secondary btn-block" value="Reset Vehicle">
</div>
</form>
<!-- End No Labels Form -->
</div>
</div>
</div>
</div>
</section>
The else block is only connected to the last if block for image5.
Each if statement for an image should have its own else to set the corresponding $newimageX to $oldimageX.
if(isset($_FILES['image']['name'])&&($_FILES['image']['name']!="")){
$newimage="uploads/".$_FILES['image']['name'];
unlink($oldimage);
move_uploaded_file($_FILES['image']['tmp_name'], $newimage);
} else {
$newimage = $oldimage;
}
if(isset($_FILES['image2']['name'])&&($_FILES['image2']['name']!="")){
$newimage2="uploads/".$_FILES['image2']['name'];
unlink($oldimage2);
move_uploaded_file($_FILES['image2']['tmp_name'], $newimage2);
} else {
$newimage2 = $oldimage2;
}
if(isset($_FILES['image3']['name'])&&($_FILES['image3']['name']!="")){
$newimage3="uploads/".$_FILES['image3']['name'];
unlink($oldimage3);
move_uploaded_file($_FILES['image']['tmp_name'], $newimage3);
} else {
$newimage3 = $oldimage3;
}
if(isset($_FILES['image4']['name'])&&($_FILES['image4']['name']!="")){
$newimage4="uploads/".$_FILES['image4']['name'];
unlink($oldimage4);
move_uploaded_file($_FILES['image4']['tmp_name'], $newimage4);
} else {
$newimage4 = $oldimage4;
}
if(isset($_FILES['image5']['name'])&&($_FILES['image5']['name']!="")){
$newimage5="uploads/".$_FILES['image5']['name'];
unlink($oldimage);
move_uploaded_file($_FILES['image5']['tmp_name'], $newimage5);
} else {
$newimage5 = $oldimage5;
}
I'm trying to make a payment gateway integration, however, although I did everything, except I need the amount var value to be passed as a radio button field, but it is only accepting it as with input field. which makes a big problem here. Please go through the code I tried so far.
Don't worry about the merchant id or hash key.
I tried to add the amount field as radio button but it won't accept, but when added as input field it is accepting.
<?php
$MERCHANT_KEY = "####";
$SALT = "####";
$PAYU_BASE_URL = "https://secure.payu.in";
$action = '';
$posted = array();
if(!empty($_POST)) {
//print_r($_POST);
foreach($_POST as $key => $value) {
$posted[$key] = $value;
}
}
$formError = 0;
if(empty($posted['txnid'])) {
// Generate random transaction id
$txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
} else {
$txnid = $posted['txnid'];
}
$hash = '';
// Hash Sequence
$hashSequence ="key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
if(empty($posted['hash']) && sizeof($posted) > 0) {
if(
empty($posted['key'])
|| empty($posted['txnid'])
|| empty($posted['amount'])
|| empty($posted['firstname'])
|| empty($posted['email'])
|| empty($posted['phone'])
|| empty($posted['productinfo'])
|| empty($posted['surl'])
|| empty($posted['furl'])
|| empty($posted['service_provider'])
) {
$formError = 1;
} else {
//$posted['productinfo'] = json_encode(json_decode('[{"name":"tutionfee","description":"","value":"500","isRequired":"false"},{"name":"developmentfee","description":"monthly tution fee","value":"1500","isRequired":"false"}]'));
$hashVarsSeq = explode('|', $hashSequence);
$hash_string = '';
foreach($hashVarsSeq as $hash_var) {
$hash_string .= isset($posted[$hash_var]) ? $posted[$hash_var] : '';
$hash_string .= '|';
}
$hash_string .= $SALT;
$hash = strtolower(hash('sha512', $hash_string));
$action = $PAYU_BASE_URL . '/_payment';
}
} elseif(!empty($posted['hash'])) {
$hash = $posted['hash'];
$action = $PAYU_BASE_URL . '/_payment';
}
?>
<script>
var hash = '<?php echo $hash ?>';
function submitPayuForm() {
if(hash == '') {
return;
}
var payuForm = document.forms.payuForm;
payuForm.submit();
}
</script>
<body onload="submitPayuForm()">
<?php if($formError) { ?>
<span style="color:red">Please fill all mandatory fields.</span>
<br/>
<br/>
<?php } ?>
<section style="padding-top:50px;padding-bottom:50px"class="section-padding banner-6 parallax-bg bg-fixed overlay light-9" data-stellar-background-ratio="0.5">
<div class="container">
<div class="text-center mb-80">
<h2 class="section-title text-uppercase"><u style="text-decoration: none;border-bottom: 1px solid #673ab7;border-top: 1px solid #673ab7;border-right: 1px solid #673ab7;border-left: 1px solid #673ab7;padding-left:3px;padding-right:3px;">    Register Here   </u></h2>
</div>
<div class="row" style="
border-top-color: #673ab7;
border-right-color: #673ab7;
border-bottom-color:#673ab7;
border-left-color: #673ab7;
border-width:1px;
border-style: solid;
text-align:center;">
<div class="col-md-3"></div>
<div class="col-md-6" >
<form name="payuForm" action="<?php echo $action; ?>" method="post" name="payuForm">
<input type="hidden" name="key" value="<?php echo $MERCHANT_KEY ?>" />
<input type="hidden" name="hash" value="<?php echo $hash ?>"/>
<input type="hidden" name="txnid" value="<?php echo $txnid ?>" />
<input type="hidden" name="service_provider" value="payu_paisa" size="64" />
<div class="row">
<div class="col-md-6">
<div class="input-field">
<label class="sr-only" for="email">Username/Email</label>
<input type="email" name="email" id="email" value="<?php echo (empty($posted['email'])) ? '' : $posted['email']; ?>" class="validate">
<label for="email" data-error="wrong" data-success="right">Email</label>
</div>
</div>
<div class="col-md-6">
<div class="input-field">
<input type="password" name="password" value="<?php echo (empty($posted['password'])) ? '' : $posted['password']; ?>" class="validate" id="name">
<label for="name">Password</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="input-field">
<input type="text" name="firstname" id="firstname" value="<?php echo (empty($posted['firstname'])) ? '' : $posted['firstname']; ?>" class="validate">
<label for="name">Name</label>
</div>
</div>
<div class="col-md-6">
<div class="input-field">
<input name="phone" value="<?php echo (empty($posted['phone'])) ? '' : $posted['phone']; ?>" type="tel" class="validate">
<label for="phone">Phone Number</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="input-field">
<input id="phone" type="tel" name="productinfo" value="<?php echo (empty($posted['productinfo'])) ? '' : $posted['productinfo'] ?>" class="validate">
<label for="phone">Your Profession</label>
</div>
</div>
<div class="col-md-6">
<div class="input-field">
<input id="website" type="text" name="address" value="<?php echo (empty($posted['address'])) ? '' : $posted['address']; ?>" class="validate">
<label for="website">Address</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<center>
<div class="input-field">
<input id="sedan" type="radio" value="<?php echo (empty($posted['amount'])) ? '' : $posted['amount'] ?>1" name="amount" >Sedan
<label for="sedan">.</label>
</div>
</center>
</div>
<div class="col-md-3">
<center>
<div class="input-field">
<input id="suv" type="radio" value="<?php echo (empty($posted['amount'])) ? '' : $posted['amount'] ?>1" name="amount" >SUV
<label for="suv">.</label>
</div>
</center>
</div>
<div class="col-md-3">
<center>
<div class="input-field">
<input id="sedansuv" type="radio" value="<?php echo (empty($posted['amount'])) ? '' : $posted['amount'] ?>2" name="amount" >Both
<label for="sedansuv">.</label>
</div>
</center>
</div>
</div>
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<center>
<div class="input-field" id="sedanonly">
<input name="amount" type="text" value="<?php echo (empty($posted['amount'])) ? '' : $posted['amount'] ?>" />
<label for="sedan">Total (in Rupees)</label>
</div>
</center>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="input-field">
<input type="text" name="surl" readonly value="http://sgdcontest.com/success.php" size="64" />
<label for="phone">For Office Use Only</label>
</div>
</div>
<div class="col-md-6">
<div class="input-field">
<input type="text" name="furl" readonly value="http://sgdcontest.com/failure.php" size="64" />
<label for="website">For Office Use Only</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<center>
<?php if(!$hash) { ?>
<button type="submit" style="width:140px"name="submit" class="waves-effect waves-light btn pink mt-30" value="Submit" />Register</button>
<?php } ?>
</center>
</div>
</div>
</form>
</div>
</div>
</div>
</section>`
I have a problem with my php-code. On the first site is a list with all the data. The table of the data is correctly displayed. In the last column of the table is a link placed to the next page which should hand over the id of the row.
Here's the link:
print 'Ändern';
But I can't now get the data into the input fields for edit. The form show up correct, but i have in every input field this error:
Warning: Illegal string offset 'vorname' in /home_pr5/d/e/deniseli.ch/htdocs/www.deniseli.ch/T .... tor/editsr.php on line 130S
Here's the editsr.php:
<?php
session_start();
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");
$id = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM users WHERE id = $id");
$result = $statement->execute(array('id' => ['id']));
$user = $statement->fetch();
include("templates/header.inc.php");
if(isset($_GET['save'])) {
$save = $_GET['save'];
if($save == 'personal_data') {
$vorname = trim($_POST['vorname']);
$nachname = trim($_POST['nachname']);
$adresse = trim($_POST['adresse']);
$plz = trim($_POST['plz']);
$ort = trim($_POST['ort']);
$geburtstag = trim($_POST['geburtstag']);
$handy = trim($_POST['handy']);
$liga = trim($_POST['liga']);
$verein = trim($_POST['verein']);
$bank = trim($_POST['bank']);
$iban = trim($_POST['iban']);
if($vorname == "" || $nachname == "" || $adresse == "" || $plz == "" || $ort == "" || $handy == "" || $liga == "" || $verein == "") {
$error_msg = "Bitte alle Angaben ausfüllen.";
} else {
$statement = $pdo->prepare("UPDATE users SET vorname = :vorname, nachname = :nachname, adresse = :adresse, plz = :plz, ort = :ort, geburtstag = :geburtstag, handy = :handy, liga = :liga, verein = :verein, bank = :bank, iban = :iban, id = :id, updated_at=NOW() WHERE id = $id");
$result = $statement->execute(array('vorname' => $vorname, 'nachname'=> $nachname,'adresse' => $adresse, 'plz' => $plz, 'ort' => $ort, 'geburtstag' => $geburtstag, 'handy' => $handy, 'liga' => $liga, 'verein' => $verein,'bank' => $bank, 'iban' => $iban, 'id' => $user['id'] ));
$success_msg = "Daten erfolgreich gespeichert.";
}
} else if($save == 'email') {
$passwort = $_POST['passwort'];
$email = trim($_POST['email']);
$email2 = trim($_POST['email2']);
if($email != $email2) {
$error_msg = "Die eingegebenen E-Mail-Adressen stimmten nicht überein.";
} else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_msg = "Bitte eine gültige E-Mail-Adresse eingeben.";
} else if(!password_verify($passwort, $user['passwort'])) {
$error_msg = "Bitte korrektes Passwort eingeben.";
} else {
$statement = $pdo->prepare("UPDATE users SET email = :email WHERE id = $id");
$result = $statement->execute(array('email' => $email));
$success_msg = "E-Mail-Adresse erfolgreich gespeichert.";
}
} else if($save == 'passwort') {
$passwortAlt = $_POST['passwortAlt'];
$passwortNeu = trim($_POST['passwortNeu']);
$passwortNeu2 = trim($_POST['passwortNeu2']);
if($passwortNeu != $passwortNeu2) {
$error_msg = "Die eingegebenen Passwörter stimmten nicht überein.";
} else if($passwortNeu == "") {
$error_msg = "Das Passwort darf nicht leer sein.";
} else if(!password_verify($passwortAlt, $user['passwort'])) {
$error_msg = "Bitte korrektes Passwort eingeben.";
} else {
$passwort_hash = password_hash($passwortNeu, PASSWORD_DEFAULT);
$statement = $pdo->prepare("UPDATE users SET passwort = :passwort WHERE id = $id");
$result = $statement->execute(array('passwort' => $passwort_hash));
$success_msg = "Passwort erfolgreich gespeichert.";
}
}
}
?>
<div class="container main-container">
<h1>Schiedsrichter Profil bearbeiten</h1>
<?php
if(isset($success_msg) && !empty($success_msg)):
?>
<div class="alert alert-success">
×
<?php echo $success_msg; ?>
</div>
<?php
endif;
?>
<?php
if(isset($error_msg) && !empty($error_msg)):
?>
<div class="alert alert-danger">
×
<?php echo $error_msg; ?>
</div>
<?php
endif;
?>
<div>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">Übersicht</li>
<li role="presentation">Persönliche Daten</li>
<li role="presentation">E-Mail</li>
<li role="presentation">Passwort</li>
</ul>
<!-- Übersicht-->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<br>
<form action="?save=personal_data&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
<div class="form-group">
<label for=inputVorname class="col-sm-2 control-label">Vorname</label>
<div class="col-sm-10">
<input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputNachname" class="col-sm-2 control-label">Nachname</label>
<div class="col-sm-10">
<input class="form-control" id="inputNachname" name="nachname" type="text" value="<?php echo htmlentities($user['nachname']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputAdresse" class="col-sm-2 control-label">Adresse</label>
<div class="col-sm-10">
<input class="form-control" id="inputAdresse" name="adresse" type="text" value="<?php echo htmlentities($user['adresse']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputPLZ" class="col-sm-2 control-label">PLZ</label>
<div class="col-sm-10">
<input class="form-control" id="inputPLZ" name="plz" type="text" value="<?php echo htmlentities($user['plz']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputOrt" class="col-sm-2 control-label">Ort</label>
<div class="col-sm-10">
<input class="form-control" id="inputOrt" name="ort" type="text" value="<?php echo htmlentities($user['ort']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputGeburtstag" class="col-sm-2 control-label">Geburtsdatum</label>
<div class="col-sm-10">
<input class="form-control" id="inputGeburtstag" name="geburtstag" type="text" value="<?php echo htmlentities($user['geburtstag']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">E-Mail</label>
<div class="col-sm-10">
<input class="form-control" id="inputEmail" name="email" type="email" value="<?php echo htmlentities($user['email']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputHandy" class="col-sm-2 control-label">Handy</label>
<div class="col-sm-10">
<input class="form-control" id="inputHandy" name="handy" type="text" value="<?php echo htmlentities($user['handy']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputLiga" class="col-sm-2 control-label">Liga</label>
<div class="col-sm-10">
<input class="form-control" id="inputLiga" name="liga" type="text" value="<?php echo htmlentities($user['liga']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputVerein" class="col-sm-2 control-label">Verein</label>
<div class="col-sm-10">
<input class="form-control" id="inputVerein" name="verein" type="text" value="<?php echo htmlentities($user['verein']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputBank" class="col-sm-2 control-label">Bankname</label>
<div class="col-sm-10">
<input class="form-control" id="inputBank" name="bank" type="text" value="<?php echo htmlentities($user['bank']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputIban" class="col-sm-2 control-label">IBAN</label>
<div class="col-sm-10">
<input class="form-control" id="inputIban" name="iban" type="text" value="<?php echo htmlentities($user['iban']); ?>" readonly>
</div>
</div>
</form>
</div>
<!-- Persönliche Daten-->
<div role="tabpanel" class="tab-pane" id="data">
<br>
<form action="?save=personal_data&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
<div class="form-group">
<label for="inputVorname" class="col-sm-2 control-label">Vorname</label>
<div class="col-sm-10">
<input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputNachname" class="col-sm-2 control-label">Nachname</label>
<div class="col-sm-10">
<input class="form-control" id="inputNachname" name="nachname" type="text" value="<?php echo htmlentities($user['nachname']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputAdresse" class="col-sm-2 control-label">Adresse</label>
<div class="col-sm-10">
<input class="form-control" id="inputAdresse" name="adresse" type="text" value="<?php echo htmlentities($user['adresse']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPLZ" class="col-sm-2 control-label">PLZ</label>
<div class="col-sm-10">
<input class="form-control" id="inputPLZ" name="plz" type="text" value="<?php echo htmlentities($user['plz']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputOrt" class="col-sm-2 control-label">Ort</label>
<div class="col-sm-10">
<input class="form-control" id="inputOrt" name="ort" type="text" value="<?php echo htmlentities($user['ort']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputGeburtstag" class="col-sm-2 control-label">Geburtsdatum</label>
<div class="col-sm-10">
<input class="form-control" id="inputGeburtstag" name="geburtstag" type="text" value="<?php echo htmlentities($user['geburtstag']); ?>" placeholder="01.01.2000">
</div>
</div>
<div class="form-group">
<label for="inputHandy" class="col-sm-2 control-label">Handy</label>
<div class="col-sm-10">
<input class="form-control" id="inputHandy" name="handy" type="text" value="<?php echo htmlentities($user['handy']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputLiga" class="col-sm-2 control-label">Liga</label>
<div class="col-sm-10">
<input class="form-control" id="inputLiga" name="liga" type="text" value="<?php echo htmlentities($user['liga']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputVerein" class="col-sm-2 control-label">Verein</label>
<div class="col-sm-10">
<input class="form-control" id="inputVerein" name="verein" type="text" value="<?php echo htmlentities($user['verein']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputBank" class="col-sm-2 control-label">Bankname</label>
<div class="col-sm-10">
<input class="form-control" id="inputBank" name="bank" type="text" value="<?php echo htmlentities($user['bank']); ?>" placeholder="Postfinance">
</div>
</div>
<div class="form-group">
<label for="inputIban" class="col-sm-2 control-label">IBAN</label>
<div class="col-sm-10">
<input class="form-control" id="inputIban" name="iban" type="text" value="<?php echo htmlentities($user['iban']); ?>" placeholder="CHxx xxxx xxxx xxxx xxxx x">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Speichern</button>
<a class="btn btn-danger" href='internal.php'>Abbrechen</a>
</div>
</div>
</form>
</div>
<!-- Änderung der E-Mail-Adresse -->
<div role="tabpanel" class="tab-pane" id="email">
<br>
<p>Zum Änderen deiner E-Mail-Adresse gib bitte dein aktuelles Passwort sowie die neue E-Mail-Adresse ein.</p>
<form action="?save=email&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
<div class="form-group">
<label for="inputPasswort" class="col-sm-2 control-label">Passwort</label>
<div class="col-sm-10">
<input class="form-control" id="inputPasswort" name="passwort" type="password" required>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">E-Mail</label>
<div class="col-sm-10">
<input class="form-control" id="inputEmail" name="email" type="email" value="<?php echo htmlentities($user['email']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputEmail2" class="col-sm-2 control-label">E-Mail (wiederholen)</label>
<div class="col-sm-10">
<input class="form-control" id="inputEmail2" name="email2" type="email" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Speichern</button>
<a class="btn btn-danger" href='spielliste.php'>Abbrechen</a>
</div>
</div>
</form>
</div>
<!-- Änderung des Passworts -->
<div role="tabpanel" class="tab-pane" id="passwort">
<br>
<p>Zum Änderen deines Passworts gib bitte dein aktuelles Passwort sowie das neue Passwort ein.</p>
<form action="?save=passwort&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
<div class="form-group">
<label for="inputPasswort" class="col-sm-2 control-label">Altes Passwort</label>
<div class="col-sm-10">
<input class="form-control" id="inputPasswort" name="passwortAlt" type="password" required>
</div>
</div>
<div class="form-group">
<label for="inputPasswortNeu" class="col-sm-2 control-label">Neues Passwort</label>
<div class="col-sm-10">
<input class="form-control" id="inputPasswortNeu" name="passwortNeu" type="password" required>
</div>
</div>
<div class="form-group">
<label for="inputPasswortNeu2" class="col-sm-2 control-label">Neues Passwort (wiederholen)</label>
<div class="col-sm-10">
<input class="form-control" id="inputPasswortNeu2" name="passwortNeu2" type="password" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Speichern</button>
<a class="btn btn-danger" href='spielliste.php'>Abbrechen</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php
include("templates/footer.inc.php")
?>
UPDATE: new code editsr.php. Works fine with the tabs, only the changes displayed after refresh the site.
Look at your code carefully
on the top you are using the $user variable like below
$id = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$result = $statement->execute(array('id' => ['id']));
$user = $statement->fetch(); // here
and again on the bottom of php code you are using $user variable like below
$id = $_GET['id'];
$user = "SELECT * FROM users WHERE id = :id"; // here
?>
on the input you are trying to get like htmlentities($user['vorname']);
<div class="form-group">
<label for=inputVorname class="col-sm-2 control-label">Vorname</label>
<div class="col-sm-10">
<input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" readonly>
</div>
</div>
that's the problem of error in every input :)
New Code with only one form:
<?php
session_start();
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");
$id = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM users WHERE id = $id");
$result = $statement->execute(array('id' => ['id']));
$user = $statement->fetch();
include("templates/header.inc.php");
if(isset($_GET['save'])) {
$save = $_GET['save'];
if($save == 'personal_data') {
$vorname = trim($_POST['vorname']);
$nachname = trim($_POST['nachname']);
$adresse = trim($_POST['adresse']);
$plz = trim($_POST['plz']);
$ort = trim($_POST['ort']);
$geburtstag = trim($_POST['geburtstag']);
$handy = trim($_POST['handy']);
$email = trim($_POST['email']);
$liga = trim($_POST['liga']);
$verein = trim($_POST['verein']);
$bank = trim($_POST['bank']);
$iban = trim($_POST['iban']);
$passwortNeu = trim($_POST['passwortNeu']);
if($vorname == "" || $nachname == "" || $adresse == "" || $plz == "" || $ort == "" || $handy == "" || $liga == "" || $verein == "" || $email == "" || $passwortNeu == "" ){
$error_msg = "Bitte alle Angaben ausfüllen.";
} else {
$passwort_hash = password_hash($passwortNeu, PASSWORD_DEFAULT);
$statement = $pdo->prepare("UPDATE users SET vorname = :vorname, nachname = :nachname, adresse = :adresse, plz = :plz, ort = :ort, geburtstag = :geburtstag, handy = :handy, liga = :liga, verein = :verein, bank = :bank, iban = :iban, passwort = :passwort, email = :email, updated_at=NOW() WHERE id = $id");
$result = $statement->execute(array('vorname' => $vorname, 'nachname'=> $nachname,'adresse' => $adresse, 'plz' => $plz, 'ort' => $ort, 'geburtstag' => $geburtstag, 'handy' => $handy, 'liga' => $liga, 'verein' => $verein,'bank' => $bank, 'iban' => $iban, 'passwort' => $passwort_hash, 'email' => $email));
$success_msg = "Daten erfolgreich gespeichert.";
}
}
}
?>
<div class="container main-container">
<h1>Schiedsrichter Profil bearbeiten</h1>
<?php
if(isset($success_msg) && !empty($success_msg)):
?>
<div class="alert alert-success">
×
<?php echo $success_msg; ?>
</div>
<?php
endif;
?>
<?php
if(isset($error_msg) && !empty($error_msg)):
?>
<div class="alert alert-danger">
×
<?php echo $error_msg; ?>
</div>
<?php
endif;
?>
<div>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">Persönliche Daten</li>
</ul>
<!-- Persönliche Daten-->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="data">
<br>
<form action="?save=personal_data" method="post" class="form-horizontal">
<div class="form-group">
<label for="inputVorname" class="col-sm-2 control-label">Vorname</label>
<div class="col-sm-10">
<input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputNachname" class="col-sm-2 control-label">Nachname</label>
<div class="col-sm-10">
<input class="form-control" id="inputNachname" name="nachname" type="text" value="<?php echo htmlentities($user['nachname']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputAdresse" class="col-sm-2 control-label">Adresse</label>
<div class="col-sm-10">
<input class="form-control" id="inputAdresse" name="adresse" type="text" value="<?php echo htmlentities($user['adresse']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPLZ" class="col-sm-2 control-label">PLZ</label>
<div class="col-sm-10">
<input class="form-control" id="inputPLZ" name="plz" type="text" value="<?php echo htmlentities($user['plz']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputOrt" class="col-sm-2 control-label">Ort</label>
<div class="col-sm-10">
<input class="form-control" id="inputOrt" name="ort" type="text" value="<?php echo htmlentities($user['ort']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputGeburtstag" class="col-sm-2 control-label">Geburtsdatum</label>
<div class="col-sm-10">
<input class="form-control" id="inputGeburtstag" name="geburtstag" type="text" value="<?php echo htmlentities($user['geburtstag']); ?>" placeholder="01.01.2000">
</div>
</div>
<div class="form-group">
<label for="inputHandy" class="col-sm-2 control-label">Handy</label>
<div class="col-sm-10">
<input class="form-control" id="inputHandy" name="handy" type="text" value="<?php echo htmlentities($user['handy']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">E-Mail</label>
<div class="col-sm-10">
<input class="form-control" id="inputEmail" name="email" type="email" value="<?php echo htmlentities($user['email']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputLiga" class="col-sm-2 control-label">Liga</label>
<div class="col-sm-10">
<input class="form-control" id="inputLiga" name="liga" type="text" value="<?php echo htmlentities($user['liga']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputVerein" class="col-sm-2 control-label">Verein</label>
<div class="col-sm-10">
<input class="form-control" id="inputVerein" name="verein" type="text" value="<?php echo htmlentities($user['verein']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputBank" class="col-sm-2 control-label">Bankname</label>
<div class="col-sm-10">
<input class="form-control" id="inputBank" name="bank" type="text" value="<?php echo htmlentities($user['bank']); ?>" placeholder="Postfinance">
</div>
</div>
<div class="form-group">
<label for="inputIban" class="col-sm-2 control-label">IBAN</label>
<div class="col-sm-10">
<input class="form-control" id="inputIban" name="iban" type="text" value="<?php echo htmlentities($user['iban']); ?>" placeholder="CHxx xxxx xxxx xxxx xxxx x">
</div>
</div>
<div class="form-group">
<label for="inputPasswortNeu" class="col-sm-2 control-label">Neues Passwort</label>
<div class="col-sm-10">
<input class="form-control" id="inputPasswortNeu" name="passwortNeu" type="password" value="<?php echo htmlentities($user['passwort']); ?>" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Speichern</button>
<a class="btn btn-danger" href='internal.php'>Abbrechen</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php
include("templates/footer.inc.php")
?>
When i am try to update details using for loop it is updated as a empty(Null) to refer columns.
Below is my code please help me what wrong with my logic.
View Code
The No. of text fields are going to generate depends on no. of values present in table. It is executing in foreach loop
<form method="post" action="<?php echo site_url('Home/advisor_update'); ?>"
<?php
if ($advisor_emp->num_rows()>0 )
{
$i=1;
foreach($advisor_emp->result() as $data)
{
$eid = $data->id;
$cn = $data->company_name;
$fd = $data->from_date;
$td = $data->to_date;
$tb = $data->type_of_buisness;
$c1 = $data->company_addres_lane_1;
$c2 = $data->company_addres_lane_2;
$cc = $data->c_city;
$cp = $data->c_pincode;
$cs = $data->c_state;
$cun = $data->c_country; ?>
<div class="col-md-6">
<div class="form-group">
<input type="" name="eid[]" class="form-control" value="<?php echo $eid; ?>">
</div>
<div class="col-md-12">
<div class="form-group">
<label>Previous Employer</label>
<input type="text" class="form-control" name="pcmp" value="<?php echo $cn; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>From</label>
<input type="text" name="pcmpfrom" class="form-control" value="<?php echo $fd ; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>To</label>
<input type="text" name="pcmpto" class="form-control" value="<?php echo $td ; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Designation</label>
<input type="text" name="pcmpdesi" class="form-control" value="<?php echo $tb;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Company Address Line 1</label>
<input type="text" name="pcmpadr1[]" class="form-control" value="<?php echo $c1;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Company Address Line 2</label>
<input type="text" name="pcmpadr2" class="form-control" value="<?php echo $c2;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>City</label>
<input type="text" name="pcmpcit" class="form-control" value="<?php echo $cc;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Pin Code</label>
<input type="text" name="pcmppin[]" class="form-control" value="<?php echo $cp;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>State</label>
<input type="text" name="pcmpstat[]" class="form-control" value="<?php echo $cs; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Country</label>
<input type="text" name="pcmpcun[]" class="form-control" value="<?php echo $cun; ?>">
</div>
</div>
</div>
<?php
$i++;
}
}
?>
<div class="col-md-12"
<input type="submit" class="btn btn-primary" name="advisor_update" value="Update" >
</div>
</form>
Here is my Controller
In controller i'm goin to getting data from the view using for loop and sending recieved data to modal.
if ($this->input->post('advisor_update'))
{
$emp_his_id = $this->input->post('eid');
$count_eid = count($emp_his_id);
for($i=0;$i<$count_eid;$i++)
{
$emp_his_pk = $emp_his_id[$i];
$inserte['company_name']= $this->input->post('cn');
$inserte['from_date']= $this->input->post('fd');
$inserte['to_date']= $this->input->post('td');
$inserte['type_of_buisness']= $this->input->post('tb');
$inserte['company_addres_lane_1']= $this->input->post('c1');
$inserte['company_addres_lane_2']= $this->input->post('c2');
$inserte['c_city']= $this->input->post('cc');
$inserte['c_pincode']= $this->input->post('cp');
$inserte['updated_on'] = date("Y-m-d");
$up_emp_history=$this->Frontend_model->update_employe($inserte=array(),$emp_his_pk);
}
}
Here is my Modal
function update_employe($inserte=array(),$emp_his_pk)
{
$this->db->where('id',$emp_his_pk);
$result=$this->db->update("is_employment", $inserte);
return $result;
}
View Code
I have change your view codes input name
<form method="post" action="<?php echo site_url('Home/advisor_update'); ?>" >
<?php
if ($advisor_emp->num_rows()>0 )
{
foreach($advisor_emp->result() as $data)
{
$eid = $data->id;
$cn = $data->company_name;
$fd = $data->from_date;
$td = $data->to_date;
$tb = $data->type_of_buisness;
$c1 = $data->company_addres_lane_1;
$c2 = $data->company_addres_lane_2;
$cc = $data->c_city;
$cp = $data->c_pincode;
$cs = $data->c_state;
$cun = $data->c_country; ?>
<div class="col-md-6">
<div class="form-group">
<input type="" name="eid[]" class="form-control" value="<?php echo $eid; ?>">
</div>
<div class="col-md-12">
<div class="form-group">
<label>Previous Employer</label>
<input type="text" class="form-control" name="cn[]" value="<?php echo $cn; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>From</label>
<input type="text" name="fd[]" class="form-control" value="<?php echo $fd ; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>To</label>
<input type="text" name="td[]" class="form-control" value="<?php echo $td ; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Designation</label>
<input type="text" name="tb[]" class="form-control" value="<?php echo $tb;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Company Address Line 1</label>
<input type="text" name="c1[]" class="form-control" value="<?php echo $c1;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Company Address Line 2</label>
<input type="text" name="c2[]" class="form-control" value="<?php echo $c2;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>City</label>
<input type="text" name="cc[]" class="form-control" value="<?php echo $cc;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Pin Code</label>
<input type="text" name="cp[]" class="form-control" value="<?php echo $cp;?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>State</label>
<input type="text" name="cs[]" class="form-control" value="<?php echo $cs; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Country</label>
<input type="text" name="cun[]" class="form-control" value="<?php echo $cun; ?>">
</div>
</div>
</div>
<?php
}
}
?>
Controller Code
You have to run a foreach loop in controller.
if ($this->input->post('advisor_update'))
{
$emp_his_id = $this->input->post('eid');
$cn = $this->input->post('cn');
$fd = $this->input->post('fd');
$td = $this->input->post('td');
$td = $this->input->post('tb');
$c1 = $this->input->post('c1');
$c2 = $this->input->post('c2');
$cc = $this->input->post('cc');
$cp = $this->input->post('cp');
foreach($emp_his_id as $key => $eid)
{
$emp_his_pk = $eid;
$inserte = array();
$inserte['company_name']= $cn[$key];
$inserte['from_date']= $fd[$key];
$inserte['to_date']= $td[$key];
$inserte['type_of_buisness']= $tb[$key];
$inserte['company_addres_lane_1']= $c1[$key];
$inserte['company_addres_lane_2']= $c2[$key];
$inserte['c_city']= $cc[$key];
$inserte['c_pincode']= $cp[$key];
$inserte['updated_on'] = date("Y-m-d");
$up_emp_history=$this->Frontend_model->update_employe($inserte=array(),$emp_his_pk);
}
}
Hope This works for you
I'm trying to allow user to update a database of products through form but upon submit nothing happens... No errors are even fired. I have been trying to work this out for hours and maybe my eyes are getting too tire because I can't see what's wrong...
form.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once $_SERVER['DOCUMENT_ROOT'].'/ecommerce/core/init.php';
include 'includes/header.php';
if(isset($_GET['add'])){
$parentQuery = $db->query("SELECT * FROM categories WHERE parent= 0" );
if (isset($_POST['submit'])) {
if(isset($_POST['prod_name']))
{
$prod_name = sanitize($_POST)['prod_name'];
}
if(isset($_POST['child']))
{
$categories = sanitize($_POST)['child'];
}
if(isset($_POST['price']))
{
$price = sanitize($_POST)['price'];
}
if(isset($_POST['list_price']))
{
$prod_name = sanitize($_POST)['list_price'];
}
if(isset($_POST['prod_width']))
{
$prod_width = sanitize($_POST)['prod_width'];
}
if(isset($_POST['prod_depth']))
{
$prod_depth = sanitize($_POST)['prod_depth'];
}
if(isset($_POST['prod_height']))
{
$prod_height = sanitize($_POST)['prod_height'];
}
if(isset($_POST['prod_material']))
{
$prod_material = sanitize($_POST)['prod_material'];
}
if(isset($_POST['quantity']))
{
$quantity = sanitize($_POST)['quantity'];
}
if(isset($_POST['care_instructions']))
{
$care_instructions = sanitize($_POST)['care_instructions'];
}
if(isset($_POST['image_1']))
{
$image_1 = $_FILES['image_1'];
}
if(isset($_POST['image_2']))
{
$image_2 = $_FILES['image_2'];
}
if(isset($_POST['image_3']))
{
$image_3 = $_FILES['image_3'];
}
if(isset($_POST['image_4']))
{
$image_4 = $_FILES['image_4'];
}
if(isset($_POST['description']))
{
$description = sanitize($_POST)['description'];
} else {
$errors = array();
$required = array('prod_name','child','price','prod_width', 'prod_depth','prod_height', 'prod_material', 'quantity', 'description', 'care_instructions', 'image_1', 'image_2', 'image_3', 'image_4');
foreach ($required as $field) {
if ($_POST[$field] == '') {
$errors[] = 'All Fields With and Astrisk are required';
break; } else {
//upload file and insert into database
//move_uploaded_file($tmpLoc, $uploadPath);
$insertSql = "INSERT INTO `product` (`prod_name`,`child`,`price`,`list_price`,`prod_width`,`prod_depth`,`prod_height`, `prod_material`, `quantity`, `description`, `care_instructions`, `image_1`, `image_2`, `image_3`, `image_4`); VALUES ('$prod_name','$child','$price','$list_price','$prod_width', '$prod_depth','$prod_height', '$prod_material', '$quantity', '$description', '$care_instructions', '$image_1', '$image_2', '$image_3', '$image_4');";
$db->query($insertSql) or die(mysqli_error($db));
header('Location: products.php');}}}}?>
<form action="products.php?add=1" method="POST" enctype="multipart/form-data">
<div class='container_12'>
<div class="form-group col-md-3">
<label for="prod_name">Product Name*:</label>
<input type="text" name="prod_name" id="prod_name" class="form-control" value="<?=((isset($_POST['prod_name']))?sanitize($_POST['prod_name']):' ');?>">
</div>
<div class="form-group col-md-3">
<label for="parent">Parent Category*:</label>
<select class="form-control" id="parent" name="parent">
<option value=""<?=((isset($_POST['parent']) && $_POST['parent'] == '')?'selected':'');?>></option>
<?php while($parent = mysqli_fetch_assoc($parentQuery)): ?>
<option value=" <?=$parent['id'];?>"<?=((isset($_POST['parent']) && $_POST['parent'] == $parent['id'])?' select':'');?>><?=$parent['category_name'];?></option>
<?php endwhile; ?>
</select>
</div>
<div class='form-group col-md-3'>
<label for='child'>Second Category*:</label>
<select id='child' name='child' class='form-control'></select>
</div>
</div>
<div class='container_12'>
<div class='form-group col-md-3'>
<label for='list_price'>List Price(OPTIONAL): </label>
<input type="text" id="list_price" name="list_price" class="form-control" value="<?=((isset($_POST['list_price']))?sanitize($_POST['list_price']):'');?>">
</div>
<div class="form-group col-md-3">
<label for="price">Price*:</label>
<input type="text" id="price" name="price" class="form-control" value="<?=((isset($_POST['price']))?sanitize($_POST['price']):'');?>">
</div>
<div class='form-group col-md-3'>
<label for='prod_width'>Width* (in inches):</label>
<input type="text" id="prod_width" name="prod_width" class="form-control" value="<?=((isset($_POST['prod_width']))?sanitize($_POST['prod_width']):'');?>">
</div>
<div class='form-group col-md-3'>
<label for='prod_depth'>Height*(in inches):</label>
<input type="text" id="'prod_depth" name="'prod_depth" class="form-control" value="<?=((isset($_POST['prod_depth']))?sanitize($_POST['prod_depth']):'');?>">
</div>
</div>
<div class='container_12'>
<div class='form-group col-md-3'>
<label for='prod_height'>Depth*(in inches):</label>
<input type="text" id="prod_height" name="prod_height" class="form-control" value="<?=((isset($_POST['prod_height']))?sanitize($_POST['prod_height']):'');?>">
</div>
<div class='form-group col-md-3'>
<label for='prod_material'>Construction Material:</label>
<input type="text" id="prod_material" name="prod_material" class="form-control" value="<?=((isset($_POST['prod_material']))?sanitize($_POST['prod_material']):'');?>">
</div>
<div class='form-group col-md-6'>
<label>Quantity * :</label>
<input type="text" id="quantity" name="quantity" class="form-control" value="<?=((isset($_POST['quantity']))?sanitize($_POST['quantity']):'');?>">
</div>
</div>
<div class='container_12'>
<div class="form-group col-md-3"> <label for="image_1">Product Photo #1:</label>
<input type="file" name="image_1" id="image_1" class="form-control">
</div>
<div class="form-group col-md-3"> <label for="image_2">Product Photo #2:</label>
<input type="file" name="image_2" id="image_2" class="form-control">
</div>
<div class="form-group col-md-3"> <label for="image_3">Product Photo #3:</label>
<input type="file" name="image_3" id="image_3" class="form-control">
</div>
<div class="form-group col-md-3"> <label for="image_4">Product Photo#4:</label>
<input type="file" name="image_4" id="image_4" class="form-control">
</div>
</div>
<div class='container_12'>
<div class="form-group col-md-6">
<label for="description">Description:</label>
<textarea id="description" name="description" class="form-control" rows="6"><?=((isset($_POST['description']))?sanitize($_POST['description']):'');?></textarea>
</div>
<div class="form-group col-md-6">
<label for="care_instructions">Care Instructions*:</label>
<textarea id="care_instructions" name="care_instructions" class="form-control" rows="6"><?=((isset($_POST['care_instructions']))?sanitize($_POST['care_instructions']):'');?></textarea>
</div></div>
<div class='container_12'>
<div class="form-group pull-right">
<input type='submit' name='submit' value='Add Product' class='form-control btn-success pull-right'>
</div></div>
</form>