I am having issues adding some data to my DB. Down below I will post my code so far, there is something I'm probably missing but i don't understand what exactly.
I'm running this on my localhost using PHP 7.0.9 but I don't think that matters.
This is on the Profile Page:
<form method="post">
<div class="row-form">
<div class="col-lg-14"><strong>Title:</strong></div>
<div class="col-md-18"><input type="text" class="form-control" name="title"/></div>
</div>
<br>
<div class="row-form">
<div class="col-lg-14"><strong>Content:</strong></div>
<div class="col-md-18"><input type="text" class="form-control" name="content"/></div>
</div>
<br>
<div class="row-form">
<div class="col-md-14"><strong>Time:</strong></div>
<div class="col-md-18"><input type="text" class="form-control" name="time1"/></div>
</div>
<br>
<div class="row-form">
<div class="col-md-14"><strong>Date:</strong></div>
<div class="col-md-18"><input type="text" class="form-control" name="date1"/></div>
</div>
<div class="row-form">
<div class="col-md-18"><input type="hidden" class="form-control" name="author" value="<?php echo $_SESSION['username']; ?>" /></div>
</div>
<br>
<button name="addDates" class="btn btn-success">Submit</button>
</form>
And here's my PHP code :
<form method="POST">
<?php
require '#/config.php';
if (isset($_POST['addDates']))
{
if (empty($_POST['title']) || empty($_POST['content']) || empty($_POST['time1']) || empty($_POST['date1']) || empty($_POST['author']))
{
$error = 'Please verify all fields';
}
if (empty($error))
{
$SQLinsert = $odb -> prepare("INSERT INTO `dates` VALUES(NULL, :title, :content, :time1, :date1, UNIX_TIMESTAMP(), :author)");
$SQLinsert -> execute(array(':title' => $_POST['title'], ':content' => $_POST['content'], ':time1' => $_POST['time1'], ':date1' => $_POST['date1'], ':author' => $_POST['author']));
echo success('Date has been added');
}
else
{
echo error($error);
}
}
?>
</form>
Once i fill the forms and submit the page just refresh but nothing has been added on my side.
Related
I need a little help or advice. I create advanced storing form in PHP PDO. Form must have upload post image ( header image ) and gallery images ( multi image store into database in same database ). I have done with store single image into database, but im stuck with multi store images into same database. I will put my code here.
<?php require('includes/config.php');
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: index.php'); }
// Upload posted information
include('core/connection.php');
include('functions/main.php');
if($_POST){
$nameproduct = $_POST['nameProduct'];
$categoryproduct = $_POST['categoryProduct'];
$keywordproduct = $_POST['keywordProduct'];
$addressproduct = $_POST['addressProduct'];
$latproduct = $_POST['latProduct'];
$lonproduct = $_POST['lonProduct'];
$locationproduct = $_POST['locationProduct'];
$telproduct = $_POST['telProduct'];
$emailproduct = $_POST['emailProduct'];
$siteproduct = $_POST['siteProduct'];
$pdate = date("Y-m-d H:i:s");
if(empty($nameproduct) or empty($keywordproduct)){
$errors = '<div class="error2"><p> All fields are required. Please try again</p></div>';
}else{
if (isset($_FILES['post_image'])===true) {
if (empty($_FILES['post_image']['name']) ===true) {
$errors = '<div class="error2">Please Choose a Post Image</div>';
}else {
$allowed = array('jpg','jpeg','gif','png');
$file_name = $_FILES['post_image']['name'];
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['post_image']['tmp_name'];
if (in_array($file_extn, $allowed)===true) {
$file_parh = 'images/' . substr(md5(time()), 0, 10).'.'.$file_extn;
move_uploaded_file($file_temp, $file_parh);
$query = $pdo->prepare("INSERT INTO `go-wp`.`products` (`post_id`, `nameProduct`, `categoryProduct`, `keywordProduct`, `addressProduct`, `latProduct`, `lonProduct`, `locationProduct`, `telProduct`, `emailProduct`, `siteProduct`, `post_date`, `post_image`) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$query->bindValue(1, $nameproduct);
$query->bindValue(2, $categoryproduct);
$query->bindValue(3, $keywordproduct);
$query->bindValue(4, $addressproduct);
$query->bindValue(5, $latproduct);
$query->bindValue(6, $lonproduct);
$query->bindValue(7, $locationproduct);
$query->bindValue(8, $telproduct);
$query->bindValue(9, $emailproduct);
$query->bindValue(10, $siteproduct);
$query->bindValue(11, $pdate);
$query->bindValue(12, $file_parh);
$query->execute();
header('Location: view_product.php');
}
}
}
}
}
?>
Code above present store single image into database ( works nice ). But when i add into HTML code next code ( below ) i have problems. What to write, how to create nice script. I open form html <form action="" method="post" enctype="multipart/form-data">
<div class="col-md-4">
<div class="act-widget fl-wrap">
<div class="profile-edit-header fl-wrap">
<h4>Gallery product</h4>
</div>
<div class="add-list-media-wrap fuzone">
<div class="fu-text">
<span><i class="fa fa-picture-o"></i> Add multiple images</span>
</div>
<input class="input-group" class="upload" type="file" name="post_image" id="imgInput" />
</div>
</div>
</div>
Full HTML code looks like this:
<form action="" method="post" enctype="multipart/form-data">
<div class="profile-edit-container add-list-container">
<div class="profile-edit-header fl-wrap">
<h4>Information product</h4>
</div>
<div class="custom-form">
<label>Product name<i class="fa fa-briefcase"></i></label>
<input type="text" name="nameProduct" placeholder="Product name" value=""/>
<div class="row">
<div class="col-md-6">
<label>Category</label>
<select name="categoryProduct" data-placeholder="Select product category:" class="chosen-select">
<option value="Choose category" selected>Choose category:</option>
<option value="Category1">Category1</option>
<option value="Category2">Category2</option>
<option value="Category3">Category3</option>
<option value="Category4">Category4</option>
</select>
</div>
<div class="col-md-6">
<label>Keywords<i class="fa fa-key"></i></label>
<input type="text" name="keywordProduct" placeholder="Max 5 keywords" value=""/>
</div>
</div>
</div>
</div>
<div class="profile-edit-container add-list-container">
<div class="profile-edit-header fl-wrap">
<h4>Product location</h4>
</div>
<div class="custom-form">
<label>Address<i class="fa fa-map-marker"></i></label>
<input type="text" name="addressProduct" placeholder="Address" value=""/>
<div class="row">
<div class="col-md-6">
<label>Latitude:<i class="fa fa-map-marker"></i></label>
<input type="text" name="latProduct" id="lat" placeholder="Latitude" value=""/>
</div>
<div class="col-md-6">
<label>Longitude:<i class="fa fa-map-marker"></i></label>
<input type="text" name="lonProduct" id="long" placeholder="Longitude" value=""/>
</div>
</div>
<div class="map-container">
<div id="singleMap" data-latitude="40.7427837" data-longitude="-73.11445617675781"></div>
</div>
<label>Location</label>
<select name="locationProduct" data-placeholder="Location" class="chosen-select" >
<option value="Choose location" selected>Choose location:</option>
<option value="Location1">Location1</option>
<option value="Location2">Location2</option>
<option value="Location3">Location3</option>
<option value="Location4">Location4</option>
<option value="Location5">Location5</option>
<option value="Location6">Location6</option>
</select>
<label>Phone<i class="fa fa-phone"></i></label>
<input type="text" name="telProduct" placeholder="Phone" value=""/>
<label>Email<i class="fa fa-envelope-o"></i></label>
<input type="text" name="emailProduct" placeholder="Email" value=""/>
<label>Site<i class="fa fa-globe"></i></label>
<input type="text" name="siteProduct" placeholder="Site" value=""/>
</div>
</div>
<div class="col-md-4">
<div class="act-widget fl-wrap">
<div class="profile-edit-header fl-wrap">
<h4>Header image</h4>
</div>
<div class="add-list-media-wrap fuzone">
<div class="fu-text">
<span><i class="fa fa-picture-o"></i> Add header image</span>
</div>
<input class="input-group" class="upload" type="file" name="post_image" id="imgInput" />
</div>
</div>
</div>
<div class="col-md-4">
<div class="act-widget fl-wrap">
<div class="profile-edit-header fl-wrap">
<h4>Gallery image</h4>
</div>
<div class="add-list-media-wrap fuzone">
<div class="fu-text">
<span><i class="fa fa-picture-o"></i> Add gallery images</span>
</div>
<input class="input-group" class="upload" type="file" name="post_image" id="imgInput" />
</div>
</div>
</div>
<div class="custom-form">
<button name="submit" class="btn big-btn color-bg flat-btn">Add product<i class="fa fa-angle-right"></i></button>
</div>
</form>
How to generate code for storing multy images into database with this code. Thanks all
myiamge
I have 2 forms as tab in signup page when one form is submit and if have any error I want to redirect to that specific tab which have error but did not happen.
Here is my view
<?php include 'header.php'; ?>
<section class="services">
<div class="container">
<div class="signup-wrapper well" style="margin-bottom: 80px; margin-top: 80px;">
<div class="container">
<div class="services__main">
<h2 class="title title--main"><span class="title__bold">Sign Up</span><span class="line line--title"><span class="line__first"></span><span class="line__second"></span></span></h2>
</div>
<div class="aside-tabs__links">
Genrel User
Dealership
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc" >
<div class="col-md-5">
<form action="<?= base_url();?>Home/add_user" method="post" >
<!-- class="quick-form" -->
<div class="form-group">
<input type="text" class="form-control" name="fname" placeholder="Full Name" />
</div>
<?php echo form_error('fname'); ?>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" />
</div>
<?php echo form_error('email'); ?>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Password" />
</div>
<?php echo form_error('password'); ?>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone #" />
</div>
<?php echo form_error('phone'); ?>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<?php echo form_error('city'); ?>
<div class="form-group">
<input type="submit" class="btn button button--red button--main pull-right" style="margin-bottom: 10px;" value="Sign Up">
</div>
</form>
</div>
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev" style="display: none;">
<div class="col-xs-5">
<form action="<?= base_url();?>Home/add_dealer" method="post" enctype="multipart/form-data">
<h4>Comapny Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="cname" placeholder="Company Name" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="owner" placeholder="Owner Name" />
</div>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="address" placeholder="Office Location" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Office Phone" />
</div>
<div class="form-group">
<select name="business-type" class="form-control">
<option class="form-group">Products Deal</option>
<option class="form-group">Bikes</option>
<option class="form-group">Accessories</option>
<option class="form-group">Both</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="link" placeholder="social-link(optional)" />
</div>
<div class="form-group">
<button type="button" class="button button--custom--grey button--main btn" id="logo">Upload Logo</button>
<input type="file" class="form-control hidden" id="logo1" name="logo1" /><span id="mylogo">* Format must be jpg,jpeg or png</span>
</div>
<?php echo form_error('logo'); ?>
<div class="form-group">
<textarea class="form-control" name="descrp" placeholder="Description(optional)"></textarea>
</div>
<h4>Primary Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="password" placeholder="Password" />
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn button button--red button--main pull-right" value="Sign Up">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<?php include 'footer.php'; ?>
here is my controller
function add_dealer()
{
//echo "<pre>"; print_r($_FILES);
$config['upload_path'] = 'C:\xampp\htdocs\devilbirds\images\uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name'] = 'TRUE';
$config['overwrite'] = 'FALSE';
$config['wm_text'] = 'Deveil Birds';
$config['wm_type'] = 'text';
$config['wm_font_path'] = 'C:\xampp\htdocs\devilbirds/fonts/fontawesome-webfont.ttf';
$config['wm_font_size'] = '16';
$config['wm_font_color'] = 'aabbcc';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_padding'] = '20';
$this->load->library('upload', $config);
$this->image_lib->watermark();
$abc = $this->upload->do_upload('logo1');
$upload_data = $this->upload->data();
// var_dump($abc);exit();
$file_name = $upload_data['file_name'];
$this->form_validation->set_rules('cname','CName','trim|required');
$this->form_validation->set_rules('logo1','Logo1','trim');
$this->form_validation->set_rules('address','Address','trim');
$this->form_validation->set_rules('phone','Phone','trim|is_unique[bd_dealer.phone]');
$this->form_validation->set_rules('owner','Owner','trim');
$this->form_validation->set_rules('descrp','Descrp','trim');
$this->form_validation->set_rules('business-type','Business-type','trim');
$this->form_validation->set_rules('link','Link','trim');
$this->form_validation->set_rules('email','Email','trim|is_unique[bd_dealer.email]');
$this->form_validation->set_rules('password','Password','trim|min_length[5]|max_length[20]');
$this->form_validation->set_rules('city','City','trim');
if ($this->form_validation->run() == FALSE)
{
$data['error'] = $this->session->set_flashdata('errors');
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url
}
else
{
$userData = array(
'company' => $this->input->post('cname'),
'logo' => $file_name,
'location' => $this->input->post('address'),
'phone' => $this->input->post('phone'),
'name' => $this->input->post('owner'),
'description' => $this->input->post('descrp'),
'business_type' => $this->input->post('business-type'),
'social_link' => $this->input->post('link'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'city' => $this->input->post('city'));
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'user_type' => '2');
//var_dump($userData);exit();
$this->Home_m->add_dealer($userData);
$this->Home_m->users($data);
redirect('Home/login');
}
}
I have two functions for two different forms in tabs. let suppose if i post my dealer form and this form have any issue then it will redirect to the signup page and it shows the pre-active tab which is general user signup. i want to redirect to the div that contain the form or dealer signup. any help will be appreciated , thanks in advance
In your controller when you get error do this.
for form one error
$data['error'] = $this->session->set_flashdata('errors');
$data['div1'] = "div1";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
For form two error
$data['error'] = $this->session->set_flashdata('errors');
$data['div2'] = "div2";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
In your view page
<script>
$(document).ready(function(){
var div1 = "<?php $div1; ?>";
var div2 = "<?php $div2; ?>";
if(div1=='' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='div1' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='' && div2=='div2'){
$("#desc").hide();
$("#rev").show();
}
})
</script>
div1
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc">
Div2
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev">
I hope it will help you.
I have a form page with a authController for validation.
Here is the code of the form page:
<div class="main-content">
<h3>log in as student</h3>
<h3>
<!-- SHOW URL MESSAGE HERE -->
</h3>
<form action="../app/controllers/authController.php" class="login_form" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username" id="" class="input_field">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" id="" class="input_field">
</div>
<div class="form-group">
<input type="submit" name="type" id="" value="Log In" class="btn btn-info">
</div>
</form>
</div>
And here is the code form the authController:
if ($_POST['type'] == 'Log In') {
if (! Validator::notEmpty()->validate($_POST['username']) ||
! Validator::notEmpty()->validate($_POST['password'])) {
$err_msg = "One of the fields is empty";
$user->redirect('stu_login.php?type=' . $err_msg);
exit;
}
How do i show the $err_msg that is defined in the authController, on the form page where the <!-- SHOW URL MESSAGE HERE --> is?
Replace
$user->redirect('stu_login.php?type=' . $err_msg);
To
$user->redirect('stu_login.php?msg=' . $err_msg);
And use in stu_login.php like this
<h3>
<!-- ERROR OR OTHER MESSAGE -->
<?php
if(isset($_GET['msg'] != '')
echo $_GET['msg'];
?>
</h3>
I am making a register form and when you succesfully fill in all the fields, it redirects to thank_you_register.php. But I want that after a few seconds, the thank_you_register.php page automatically redirects to another page. How do I do this?
The HTML code is this:
<form class="register form" action="../app/controller/authController.php" method="POST">
<div class="form-group">
<select name="region" id="">
<option value="Americas">Americas</option>
<option value="Europe">Europe</option>
<option value="Asia">Asia</option>
</select>
</div>
<div class="form-group">
<input type="text" name="first_name" id="" placeholder="First Name">
<?php if(isset($_SESSION['first_name_error'] )) : ?>
<span class="error"><?php echo $_SESSION['first_name_error']; ?></span>
<?php unset($_SESSION['first_name_error']); endif; ?>
</div>
<div class="form-group">
<input type="text" name="last_name" id="" placeholder="Last Name">
<?php if(isset($_SESSION['last_name_error'] )) : ?>
<span class="error"><?php echo $_SESSION['last_name_error']; ?></span>
<?php unset($_SESSION['last_name_error']); endif; ?>
</div>
<div class="form-group">
<input type="email" name="email" id="" placeholder="E-mail Address">
<?php if(isset($_SESSION['email_error'] )) : ?>
<span class="error"><?php echo $_SESSION['email_error']; ?></span>
<?php unset($_SESSION['email_error']); endif; ?>
</div>
<div class="form-group">
<input type="email" name="confirm_email" id="" placeholder="Confirm E-mail Address">
<?php if(isset($_SESSION['confirm_email_error'])) : ?>
<span class="error"><?php echo $_SESSION['confirm_email_error']; ?></span>
<?php unset($_SESSION['confirm_email_error']); endif; ?>
</div>
<div class="form-group">
<input type="password" name="password" id="" placeholder="Password">
<?php if(isset($_SESSION['password_error'] )) : ?>
<span class="error"><?php echo $_SESSION['password_error']; ?></span>
<?php unset($_SESSION['password_error']); endif; ?>
</div>
<div class="form-group">
<input type="password" name="confirm_password" id="" placeholder="Confirm Password">
<?php if(isset($_SESSION['confirm_password_error'] )) : ?>
<span class="error"><?php echo $_SESSION['confirm_password_error']; ?></span>
<?php unset($_SESSION['confirm_password_error']); endif; ?>
</div>
<div class="form-group">
<input type="text" name="age" id="" placeholder="Age">
<?php if(isset($_SESSION['age_error'] )) : ?>
<span class="error"><?php echo $_SESSION['age_error']; ?></span>
<?php unset($_SESSION['age_error']); endif; ?>
</div>
<div class="form-group">
<input type="submit" name="type" value="Register" class="register button">
</div>
</form>
And the PHP code of the form is this:
if ($_POST['type'] == 'Register') {
$i=0;
$names = array('first_name', 'last_name', 'email', 'confirm_email', 'password', 'confirm_password', 'age');
foreach($names as $field) {
if (empty($_POST[$field])) {
$_SESSION[$field.'_error'] = "Field cannot be empty!";
$i++;
}
else
{
unset($_SESSION[$field.'_error']);
$user->redirect('register.php');
}
}
if (sizeof($names) == $i) {
$user->redirect('register.php');
}
if ($_POST['email'] != $_POST['confirm_email']) {
$_SESSION['confirm_email_error'] = "E-mail Address does not match!";
exit();
}
if ($_POST['password'] != $_POST['confirm_password']) {
$_SESSION['confirm_password_error'] = "Password does not match!";
exit();
}
function add($region, $first_name, $last_name, $email, $confirm_email, $password, $confirm_password, $age) {
var_dump($_POST);
$database = Database::getInstance();
$sql = "INSERT INTO `users`
(`region`, `first_name`, `last_name`, `email`, `confirm_email`, `password`, `confirm_password`, `age`)
VALUES (:region, :first_name, :last_name, :email, :confirm_email, :password, :confirm_password, :age)";
$stmt = $database->pdo->prepare($sql);
$stmt->bindParam(':region', $region);
$stmt->bindParam(':first_name', $first_name);
$stmt->bindParam(':last_name', $last_name);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':confirm_email', $confirm_email);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':confirm_password', $confirm_password);
$stmt->bindParam(':age', $age);
$stmt->execute();
}
$user->redirect('');
I haven't filled out the $user->redirect(''); because i don't know what should be in there yet.
And the HTML code for the thank_you_register page is this:
<?php require realpath(__DIR__ . '/header.php'); ?>
<div class="main-content">
<div class="messageBox">
<div class="MB_head">
<h2>Thank You!</h2>
</div>
<div class="MB_content">
<p>You are registered.</p>
</div>
</div>
</div>
In thank_you_register.php add this script
<script>setTimeout(function() { location.replace("otherpage.html")},3000);</script>
to replace the thankyouregister after 3 seconds.
NOTE: I use location.replace to not break the back button from thankyouregister
A simple solution:
In your thank_you_register.php page put this into your <head> tag
<meta http-equiv="refresh" content="5; URL=http://your_site.com/">
The 5 are the seconds.
You can use
<meta http-equiv="refresh" content="5; url=http://example.com/" />
or (via javascript)
<script type="text/javascript">
setTimeout(function() {
window.location.href = "http://example.com";
},5000);
</script>
The easiest way is to use javascript to do that
window.location = "http://www.yoururl.com";
Use JavaScript for this. It offers a much nicer user experience.
Example:
<div class="main-content">
<div class="messageBox">
<div class="MB_head">
<h2>Thank You!</h2>
</div>
<div class="MB_content">
<p>You are registered.</p>
</div>
<div>
<p>Redirecting you back to the home page...click here if you are not automatically redirected</p>
</div>
</div>
</div>
<script>
setTimeout(function () {
window.location.href = "/url/to/redirect/to";
}, 3000); // 3000 milliseconds until it calls function
</script>
You can use the following Php script to achieve what you stated above
<?php
header('Location: http://www.example1.com/');
sleep(20);
header('Location: http://www.example2.com/');
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hope someone can help. I have a profile page that I want to display the logged in users details. So far I have this on the Profile page.
<?php
/* This script pulls the existing name input and displays it when the user logs in. */
session_start();
include("db.php"); ?>
<?php include("includes/header.php") ?>
<?php include("includes/nav.php") ?>
<?php
if(logged_in()) {
$result = mysqli_query($link,$query);
$row = mysqli_fetch_array($result);
if (!$_POST['name'] && $_POST['name']=="") $error.="<br />Please enter your name";
if (!$_POST['email'] && $_POST['email']=="") $error.="<br />Please enter your email";
if (!$_POST['DOB'] && $_POST['DOB']=="") $error.="<br />Please enter your date of birth";
if (!$_POST['country'] && $_POST['country']=="") $error.="<br />Please enter your country";
if ($error) {
echo '<div class="alert alert-success alert-dismissable">'.addslashes($error).'</div>';
}
if(isset($_POST['form-control'])) {
move_uploaded_file($_FILES['file']['tmp_name'],"img/".$_FILES['file']['name']);
$query = mysqli_query("UPDATE users SET image = '".$_FILES['file']['name']."'");
}
} else {
redirect("login.php");
}
?>
<Style>
.alert{
display:none;
}
#profileimg {
height: 100px;
width: auto;
}
</Style>
<div class="container">
<h1>Edit Profile</h1>
<hr>
<div class="row">
<!-- left column -->
<div class="col-md-3">
<div class="text-center">
<img src="//placehold.it/100" class="avatar img-circle" alt="avatar" id="profileimg">
<h6>Upload a different photo...</h6>
<input class="form-control" type="file" name="name">
</div>
</div>
<!-- edit form column -->
<div class="col-md-9 personal-info">
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Profile updated.</strong>
</div>
<h3>Personal info</h3>
<form class="form-horizontal" role="form" action="edit_profile.php" method="post">
<div class="form-group">
<label class="col-lg-3 control-label name">name:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['name'];?>" type="text" name="name" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['email'];?>" type="text" name="email" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">DOB:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['DOB'];?>" type="date" name="DOB" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Country</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['country'];?>" type="text" name="country" required>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
<input class="btn btn-default" id="updated" value="Cancel" type="reset">
</div>
</div>
</form>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script>
$("#updated").click(function(){
$(".alert").hide().show('medium');
</script>
</body>
</html>
I then have another php file for the updating which is this:
<?php
session_start();
include("db.php");
$name = $_POST['name'];
$email = $_POST['email'];
$DOB = $_POST['DOB'];
$country = $_POST['country'];
$password = md5($salt.$_POST['password']);
$query = "UPDATE users SET name = '".$name."', email = '".$email."', DOB = '".$DOB."', country = '".$country."', password = '".$password."'";
$result = mysqli_query($link,$query);
header('Location: profile.php');
?>
So the short is it doesn't display or update and I am not sure why. I am new to PHP so go easy on me if this is simple, I have searched but can't seem to find the answer.
Thanks in advance.
Im also new to this but normally when I check if a SESSION id is active I use
if(isset($_SESSION['id'])) {
$query = "UPDATE users SET name = '".$name."', email = '".$email."', DOB = '".$DOB."', country = '".$country."', password = '".$password."' WHERE id='".$_SESSION['id']."'";
}
You also need to echo back the indexed rows that you are trying to query to display results
$name = row['username'];
echo $name;
There are lots of errors in your code: You are trying to upload a file in the same page whereas you send the form data to another page. How you handle form validation is also a little overhead. What I did change in the form is: I add name="save" in your submit button and added new hidden input for storing your user profile id. I am not sure what login() function did in your code, better stick to if($id){}.
Try this:
<?php
/* This script pulls the existing name input
and displays it when the user logs in. */
session_start();
include("db.php");
include("includes/header.php");
include("includes/nav.php");
$id = $_SESSION['id'];
if(loginned()) {//you can do if($id){}
$query="SELECT * FROM users WHERE id='$id' LIMIT 1";
$result = mysqli_query($link,$query);
$row = mysqli_fetch_array($result);
?>
<style>
.alert{
display:none;
}
#profileimg {
height: 100px;
width: auto;
}
</style>
<div class="container">
<h1>Edit Profile</h1>
<hr>
<div class="row">
<!-- left column -->
<!-- edit form column -->
<div class="col-md-9 personal-info">
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert"
aria-hidden="true">×</button>
<strong>Profile updated.</strong>
</div>
<h3>Personal info</h3>
<form class="form-horizontal" role="form"
action="edit_profile.php" method="post">
<div class="form-group">
<label class="col-lg-3 control-label name">name:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['name'];?>"
type="text" name="name" required>
</div>
</div>
<div class="col-md-3">
<div class="text-center">
<img src="//placehold.it/100" class="avatar
img-circle" alt="avatar" id="profileimg">
<h6>Upload a different photo...</h6>
<input class="form-control" type="file" name="name">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['email'];?>"
type="text" name="email" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">DOB:</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['DOB'];?>"
type="date" name="DOB" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Country</label>
<div class="col-lg-8">
<input class="form-control" value="<?php echo $row['country'];?>"
type="text" name="country" required>
</div>
</div>
<div class="form-group">
<input type="hidden" name="id" value="<?php echo $row['id'];?>">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" name="save"
value="Save Changes" type="submit">
<span></span>
<input class="btn btn-default" id="updated"
value="Cancel" type="reset">
</div>
</div>
</form>
</div>
<?php }else{ redirect("login.php"); } ?>
edit_profile.php First we check whether any post with a name of save is there, We validate the posted data. if validation is successful, we proceed to upload your file and then run your update query.
<?php
session_start();
include("db.php");
if(isset($_POST['save'])){
$id = isset($_POST['id'])? $_POST['id']:'';
$name = isset($_POST['name'])? $_POST['name']:'';
$email = isset($_POST['email'])? $_POST['email']:'';
$dob = isset($_POST['DOB'])? $_POST['DOB']:'';
$pass = isset($_POST['passwrd'])? md5($salt.$_POST['password']):'';
$country = isset($_POST['country'])? $_POST['country']:'';
if(empty($name)){
$error = 'Please enter your name';
}elseif(empty($email)){
$error = 'Please enter your email';
}elseif(empty($dob)){
$error = 'Please enter your date of birth';
}elseif(empty($country)){
$error = 'Please enter your country';
}elseif(empty($pass)){
$error = 'Please enter your password';
}else{
move_uploaded_file($_FILES['file']['tmp_name'],"img/".$_FILES['file']['name']);
$query = mysqli_query("UPDATE users SET image = '".$_FILES['file']['name']."'
WHERE id='$id'");
$query = "UPDATE users SET name = '$name', email = '$email',
DOB = '$DOB', country = '$country', password = '$password'
WHERE id='$id'";
$result = mysqli_query($link,$query);
header('Location: profile.php');
}
}
?>
<?php if(!empty($error)){
echo '<div class="alert alert-success
alert-dismissable">'.addslashes($error).'</div>';
}else{
echo '<div class="alert alert-success">Success</div>';
}
?>
I have added a demo here. At least this will help: