How to display one image at a time? - php

I am currently implementing an upload the image function on my semester website project. However, the problem I am getting is when I upload an image and proceed to re-upload another image. the images stack on each other and when I refresh it doesn't remove all the images uploaded at all.
Image for Context:
<?php
include 'config.php';
session_start();
if (isset($_POST['submit'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$rating = $_POST['rating'];
$contact = $_POST['contact'];
$category = $_POST['category'];
$feedback = $_POST['feedback'];
$sql = "INSERT INTO feedback(email,name,contact,category,feedback) VALUES('$email','$name','$contact','$category','$feedback')";
$result = mysqli_query($conn, $sql);
if ($result) {
header('location: feedback.php');
}
$sql2 = "INSERT INTO rate(rating) VALUES('$rating')";
$result = mysqli_query($conn,$sql2);
if($result){
header('location: feedback.php');
}
}
if(isset($_POST['upload'])){
$filename = $_FILES['uploadfile']['name'];
$tempname = $_FILES['uploadfile']['tmp_name'];
$folder = "./image/".$filename;
$sql3 = "INSERT INTO image(filename)VALUES('$filename')";
mysqli_query($conn,$sql3);
move_uploaded_file($tempname,$folder);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="feedbackphp.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.css">
</head>
<body>
<section class="contact">
<div class="container-lg">
<div class="text-center mt-5">
<h2>Feedback Form</h2>
<p class="lead">We Would like to hear your Feedback!</p>
</div>
<div class="row justify-content-center my-5">
<div class="col-lg-6">
<form action="" method="post" enctype="multipart/form-data">
<label for="email" class="form-label">Email Address</label>
<input type="email" name="email" class="form-control" id="email" placeholder="Enter your Email">
<label for="name" class="form-label mt-2">Name</label>
<input type="text" name="name" class="form-control" id="name" placeholder="Enter your Name">
<label for="contact" class="form-label mt-2">Contact</label>
<input type="text" name="contact" class="form-control" id="contact" placeholder="Enter your Contact">
<label for="subject" class="form-label mt-2">Category</label>
<select class="form-select" name="category" id="subject">
<option value="">Select One</option>
<option value="Club Maintainence & Equipment">Club Maintainence & Equipment </option>
<option value="Club Cleanliness">Club Cleanliness</option>
<option value="Membership Issues">Membership Issues</option>
<option value="General/Others">General/Others</option>
</select>
<label for="rating" class="form-label mt-2">Rate Us!</label>
<div class="rateyo" id="rating"
data-rateyo-rating="0"
data-rateyo-num-stars="5"
data-rateyo-score="3">
</div>
<span class="result">0</span>
<input type="hidden" name="rating">
<br>
<label for="query" class="form-label mt-2">Feedback Message </label>
<textarea id="query" name="feedback" class="form-control" style="height:140px" placeholder="Enter Feedback Message here..."></textarea>
<label class="form-label mt-2">Upload File</label>
<div class="form-group mt-2">
<input class="form-control" type="file" name="uploadfile">
</div>
<br>
<div class="form-group">
<button class="btn btn-primary" type="submit" name="upload">Upload</button>
</div>
<div id="display-image">
<?php
$query = "SELECT * FROM image";
$result = mysqli_query($conn,$query);
while($data = mysqli_fetch_assoc($result)){
?>
<img src="./image/<?php echo $data['filename'];?>">
<?php
}
?>
</div>
<div class="mb-4 text-center mt-2">
<button type="submit" name="submit" class="btn btn-primary">Submit Feedback</button>
</div>
</form>
</div>
</div>
</div>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<script src="fbphp.js" type="text/javascript"></script>
</body>
</html>

You are selecting all images
$query = "SELECT * FROM image LIMIT 1";
Use the query about to set a limit to 1;
then using this will prevent you from using the while loop
Hope this will help

Related

Submit form with a drop-down list

I got an HTML-form with a drop-down list of products. When you select a product, you will have a new input field appear. For each product type input fields are different. For example: a book will have a new input field of weight, DVD will have a new input field of size and furniture will have inputs of HxWxL dimensions.
So here is the question: how is it better to submit a form to MySQL with different product types so that they are saved in one table? Without the use of if-else and switch statements.
P.S. I am sorry if this question might be asked before here.
P.S.S. And yes, code/name and price inputs are by default with each new product type.
Here is my database related code:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'things') or die(mysqli_error($mysqli));
if (isset($_POST['save'])) {
$code= $_POST['code'];
$name = $_POST['name'];
$price = $_POST['price'];
$mysqli->query("INSERT INTO products (code, name, price) VALUES('$code', '$name', '$price')") or die($mysqli->error);
And here is my form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add</title>
<link rel="stylesheet" href="css/add.css">
</head>
<body>
<h1>Add</h1>
<hr>
<?php require_once 'connect.php'; ?>
<div class="container">
<!--FORM-->
<form id="form" action="connect.php" method="POST">
<!--CODE-->
<div id="form-group">
<label for="code" id="code-label"> Code</label>
<input type="text" id="code" name="code" required>
</div>
<!--NAME-->
<div id="form-group">
<label for="name" id="name-label"> Name </label>
<input type="text" id="name" name="name" required>
</div>
<!--PRICE-->
<div id="form-group">
<label for="price" id="price-label"> Price </label>
<input type="number" id="price" name="price" required>
</div>
<div id="form-group">
<label for="product-type" id="product-type-label"> Type Switcher </label>
<select name="product-type" id="product-type" onchange="showHide()">
<option selected hidden value="">Type Switcher</option>
<option value="dvd">DVD</option>
<option value="furniture">Furniture</option>
<option value="book">Book</option>
</select>
</div>-->
<!--HIDDEN INPUTS-->
<div id="size" style="display: none">
<div id="form-group">
<label for="size" id="size-label"> Size </label>
<input type="number" name="size" required>
</div>
</div>
<div id="height" style="display: none">
<div id="form-group">
<label for="height" id="height-label"> Height </label>
<input type="number" name="height" required>
</div>
</div>
<div id="width" style="display: none">
<div id="form-group">
<label for="width" id="width-label"> Width </label>
<input type="number" name="width" required>
</div>
</div>
<div id="length" style="display: none">
<div id="form-group">
<label for="length" id="length-label"> Length </label>
<input type="number" name="length" required>
</div>
</div>
<div id="weight" style="display: none">
<div id="form-group">
<label for="weight" id="weight-label"> Weight </label>
<input type="number" name="weight" required>
</div>
</div>
<button type="submit" name="save">Save</button>
</form>
</div>
<script src="javascript/scripts.js"></script>
</body>
</html>

PHP form submission error

I created new inquiry form for my website. But it's giving the following error message.
Error
Sorry there was an error sending your form.
mail:Could not instantiate mail function.
Form HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PHP Contact Form Script With Validation - reusable form</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="form.css" >
<script src="form.js"></script>
</head>
<body >
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2>Contact Us</h2>
<p> Send us your message and we will get back to you as soon as possible </p>
<form role="form" method="post" id="reused_form">
<div class="row">
<div class="col-sm-6 form-group">
<label for="name"> First Name:</label>
<input type="text" class="form-control" id="firstname" name="firstname" maxlength="50">
</div>
<div class="col-sm-6 form-group">
<label for="name"> Last Name:</label>
<input type="text" class="form-control" id="lastname" name="lastname" maxlength="50">
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<label for="email"> Email:</label>
<input type="text" class="form-control" id="email" name="email" maxlength="50">
</div>
<div class="col-sm-6 form-group">
<label for="email"> Phone:</label>
<input type="tel" class="form-control" id="phone" name="phone" required maxlength="50">
</div>
</div>
<div class="row">
<div class="col-sm-12 form-group">
<label for="name"> Message:</label>
<textarea class="form-control" type="textarea" id="message" name="message" placeholder="Your Message Here" maxlength="6000" rows="7"></textarea>
</div>
</div>
<div class="row">
<div class="col-sm-12 form-group">
<button type="submit" class="btn btn-lg btn-success btn-block" id="btnContactUs">Post It! </button>
</div>
</div>
</form>
<div id="success_message" style="width:100%; height:100%; display:none; "> <h3>Sent your message successfully!</h3> </div>
<div id="error_message" style="width:100%; height:100%; display:none; "> <h3>Error</h3> Sorry there was an error sending your form. </div>
</div>
</div>
</div>
</body>
</html>
Handler.php:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['firstname','lastname', 'email','phone'])->areRequired()->maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);
$pp->sendEmailTo('name#mail.com'); // ← Your email here
echo $pp->process($_POST);
You are not defining an action for the form.
<form role="form" method="post" id="reused_form" action="Handler.php">
<!-- html goes here -->
</form>

phpMyAdmin MySQL register functionality not working

After running both services: Apache and MySQL,
When I try to register with some data, it is actually not pushed into the database which I selected. I mean MySQL Database does not recieves the input from the index.php form element.
Database Details:
hostname: localhost; database: registration_db; username: root; password: '';
table name: users
I am creating a basic project which achieves register and login functionalities with PHP and MySQL.
I use Xampp Server with PHP version 7.2.4 and Apache 2.4.33.
My project files are in the following directory: C/Xampp/htdocs/CORE
db_connect.php
<?php
$con = #mysqli_connect("localhost", "root", "", "registration_db") or die ("error connecting to db");
?>
index.php
<?php
require'db_connect.php';
$username = "";
$email = "";
$errors = array();
if (isset($_POST['username1']) && isset($_POST['pass1'])){
$username = mysqli_real_escape_string($con, $_POST['username1']);
$password_1 = mysqli_real_escape_string($con, $_POST['pass1']);
$password_2 = mysqli_real_escape_string($con, $_POST['pass2']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($con, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "email already exists");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$sql = "INSERT INTO users (id, username, email, password)
VALUES('$id', $username', '$email', '$password1')";
mysqli_query($con, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>CSR Nexus — CSR nexus is a dynamic organization focused solely on social responsibilities.</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="CSR Nexus" />
<meta name="keywords" content="CSR Nexus, CSR, Nexus" />
<meta name="author" content="CSR Nexus Team" />
<!-- Facebook and Twitter integration -->
<meta property="og:title" content=""/>
<meta property="og:image" content=""/>
<meta property="og:url" content=""/>
<meta property="og:site_name" content=""/>
<meta property="og:description" content=""/>
<meta name="twitter:title" content="" />
<meta name="twitter:image" content="" />
<meta name="twitter:url" content="" />
<meta name="twitter:card" content="" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,700" rel="stylesheet">
<!-- Animate.css -->
<link rel="stylesheet" href="css/animate.css">
<!-- Icomoon Icon Fonts-->
<link rel="stylesheet" href="css/icomoon.css">
<!-- Themify Icons-->
<link rel="stylesheet" href="css/themify-icons.css">
<!-- Bootstrap -->
<link rel="stylesheet" href="css/bootstrap.css">
<!-- Magnific Popup -->
<link rel="stylesheet" href="css/magnific-popup.css">
<!-- Owl Carousel -->
<link rel="stylesheet" href="css/owl.carousel.min.css">
<link rel="stylesheet" href="css/owl.theme.default.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="css/style.css">
<!-- Modernizr JS -->
<script src="js/modernizr-2.6.2.min.js"></script>
<!-- FOR IE9 below -->
<!--[if lt IE 9]>
<script src="js/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="gtco-loader"></div>
<div id="page">
<div class="page-inner">
<nav class="gtco-nav" role="navigation">
<div class="gtco-container">
<div class="row">
<div class="col-sm-4 col-xs-12">
<div id="gtco-logo">CSR Nexus </div>
</div>
<div class="col-xs-8 text-right menu-1">
<ul>
<li class="has-dropdown">
Organization
<ul class="dropdown">
<li>Register</li>
<li>Login</li>
</ul>
</li>
<li>Contact</li>
</ul>
</div>
</div>
</div>
</nav>
<header id="gtco-header" class="gtco-cover" role="banner" style="background-image: url(images/img_4.jpg)">
<div class="overlay"></div>
<div class="gtco-container">
<div class="row">
<div class="col-md-12 col-md-offset-0 text-left">
<div class="row row-mt-15em">
<div class="col-md-7 mt-text animate-box" data-animate-effect="fadeInUp">
<span class="intro-text-small">Welcome to CSR Nexus</span>
<h1>CSR nexus is a dynamic organization focused solely on social responsibilities.</h1>
</div>
<div class="col-md-4 col-md-push-1 animate-box" data-animate-effect="fadeInRight">
<div class="form-wrap">
<div class="tab">
<ul class="tab-menu">
<li class="active gtco-first">Sign up</li>
<li class="gtco-second">Login</li>
</ul>
<div class="tab-content">
<div class="tab-content-inner active" data-content="signup">
<form id="registration" method="POST">
<?php include('errors.php'); ?>
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<div class="row form-group">
<div class="col-md-12">
<label for="username">Username or Email</label>
<input type="text" class="form-control" id="username1" >
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label for="password">Password</label>
<input type="password" class="form-control" id="pass1" >
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label for="password2">Repeat Password</label>
<input type="password" class="form-control" id="pass2">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" name="submit" class="btn btn-primary" value="Sign up">
</div>
</div>
</form>
</div>
<div class="tab-content-inner" data-content="login">
<form id="login" method="post" action="login.php">
<?php include('errors.php'); ?>
<div class="row form-group">
<div class="col-md-12">
<label for="username">Username or Email</label>
<input type="text" class="form-control" id="username">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label for="password">Password</label>
<input type="password" class="form-control" id="password">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" class="btn btn-primary" value="Login">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</header>
<!-- jQuery -->
<script src="js/jquery.min.js"></script>
<!-- jQuery Easing -->
<script src="js/jquery.easing.1.3.js"></script>
<!-- Bootstrap -->
<script src="js/bootstrap.min.js"></script>
<!-- Waypoints -->
<script src="js/jquery.waypoints.min.js"></script>
<!-- Carousel -->
<script src="js/owl.carousel.min.js"></script>
<!-- countTo -->
<script src="js/jquery.countTo.js"></script>
<!-- Magnific Popup -->
<script src="js/jquery.magnific-popup.min.js"></script>
<script src="js/magnific-popup-options.js"></script>
<!-- Main -->
<script src="js/main.js"></script>
</body>
</html>
you are missing name attribute in your html.
Change
<input type="text" class="form-control" id="username1" >
With
<input type="text" class="form-control" id="username1" name="username1">
And you are missing action attribute in the form.your registration from shoul be
<form id="registration" method="POST" action="action.php">
<?php include('errors.php'); ?>
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<div class="row form-group">
<div class="col-md-12">
<label for="username">Username or Email</label>
<input type="text" class="form-control" id="username1" name="username1">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label for="password">Password</label>
<input type="password" class="form-control" id="pass1" name="pass1">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label for="password2">Repeat Password</label>
<input type="password" class="form-control" id="pass2" name="pass2">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" name="submit" class="btn btn-primary" value="Sign up">
</div>
</div>
</form>
You missed an ' single quote around $username
$sql = "INSERT INTO users (id, username, email, password)
VALUES('$id', $username', '$email', '$password1')";
Correted
$sql = "INSERT INTO users (id, username, email, password)
VALUES('$id', '$username', '$email', '$password1')";
You are also missing the names of all the input fields in your form as Below:
<input type="text" class="form-control" id="username1" name="username1">
<input type="password" class="form-control" id="pass1" name="pass1">
<input type="password" class="form-control" id="pass2" name="pass2">
And There also no input filed for email add filed for email:
<input type="email" class="form-control" id="email" name="email">

Form submitting but mysql query not properly functioning [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
The following code gives a signup form that i created but unfortunately though it works the mysql query fails and returns the error message unsuccessfull signup! how can i correct this?
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Theme Made By www.w3schools.com - No Copyright -->
<title>Arsiri Textile </title>
<meta charset="utf-8">
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Theme Made By www.w3schools.com - No Copyright -->
<title>Arsiri Textile </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/basic.css">
<style>
</style>
</head>
<body id="myPage" data-spy="scroll" data-target=".navbar" data-offset="60">
<div class="header" style="height:60px;background:#330d00;width:1350px">
<a class="navbar-brand" href="#myPage">ARSIRI TEXTILE |</a>
<ul class="nav navbar-nav navbar-left">
</ul>
<ul class="nav navbar-nav navbar-right">
<li>About Us</li>
<li>Contact Us</li>
<li>Login</li>
<li> Signup</li>
</ul>
</div>
<div class="image" style="height:530px; width:1350px" >
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css" />
</head>
<body>
<div class="panel panel-default">
<div class="panel-heading" style="background:#330d00;">
<h3 class="panel-title">Customer Registration</h3><br>
</div>
<div class="panel-body" >
<form class="form-horizontal" role="form" action="signupaction.php" method="post" >
<div class="form-group">
<label for="name" class="col-sm-2 control-label">First Name</label>
<div class="col-sm-10" style="width:300px">
<input type="text" class="form-control" id="name" name="firstname" required>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Last Name</label>
<div class="col-sm-10" style="width:300px">
<input type="text" class="form-control" id="name" name="lastname" required>
</div>
</div>
<div class="form-group">
<label for="Address" class="col-sm-2 control-label">Address</label>
<div class="col-sm-10" style="width:300px">
<textarea class="form-control" class="form-control" name="address"></textarea>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Contact No.</label>
<div class="col-sm-10" style="width:300px">
<input type="text" maxlength="10" class="form-control" id="name" name="contactno" required>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">E-Mail</label>
<div class="col-sm-10" style="width:300px">
<input type="email" class="form-control" id="name" name="emailaddress" required>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10" style="width:300px">
<input type="password" class="form-control" id="pass" name="password" required>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Confirm password
</label>
<div class="col-sm-10" style="width:300px">
<input type="password" class="form-control" id="confirmpass" name="cpassword" required><br>
</div>
</div>
<div class="panel-footer" style="overflow:hidden;text-align:left;">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-success btn-sm" type=submit name=submit value=Submit >
<input class="btn btn-success btn-sm" type=reset name=reset value=Cancel>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</div>
<div class="footer " style="background:#330d00; height:60px ;width:1350px ">
<p align="center" > Asiri all rights reserved</p>
</div>
The relevant php script(ignupaction.php) for the above html form is as follows
<?php
//importing db.php in the includes folder
require("includes/db.php");
$fname=$_POST["firstname"];
$lname=$_POST["lastname"];
$address=$_POST["address"];
$contact=$_POST["contactno"];
$email=$_POST["emailaddress"];
$password=$_POST["password"];
$cpassword=$_POST["cpassword"];
$sql="INSERT INTO `signup` VALUES ('$fname','$lname','$address','$contact','$email','$password',$cpassword')";
$result=mysqli_query($db,$sql);
if(!$result){
echo "Unsuccessful signup";
}
else{
echo "Successful signup";
}
?>
when the form is submitted it returns unsuccessful signup! how can i correect this?
$sql="INSERT INTO signup VALUES ('$fname','$lname','$address','$contact','$email','$password',$cpassword')";
you are missing a ' on the last variable. you have '$password',$cpassword' <----- missing '. Also change the ticks around 'signup'.
Try this :
$sql="INSERT INTO 'signup' VALUES ('$fname','$lname','$address','$contact','$email','$password','$cpassword')";

Registration form bringing back login credentials

I have made a user registration form in PHP and when I access the page the username part of the form is already filled in as my own log in credentials and the same with the password. The other parts of the form are filled in as an undefined index also.
Below shows the form and PHP code for my registration form. The form itself actually works and populates to my database.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>Ballymena Sports</title>
<!-- Bootstrap core CSS -->
<link href="bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="home2.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Ballymena Sports</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li>Log out</li>
</ul>
</div>
</nav>
<?php
include"config.php";
if(isset($_POST["submit"])){
$username=$_POST['username'];
$password=$_POST['password'];
$forename=$_POST['forename'];
$surname=$_POST['surname'];
$email=$_POST['email'];
$telephone=$_POST['telephone'];
$address1=$_POST['address1'];
$town=$_POST['town'];
$postcode=$_POST['postcode'];
$q = $db->prepare("SELECT * FROM user WHERE username = ?");
$query = $q-> execute(array($username));
$count = $q->rowCount();
if($count == 0) {
$query = $db->prepare("INSERT INTO user SET username = ?, password = ?, forename = ?, surname = ?, email = ?, telephone = ?, address1 = ?, town=?, postcode=? ");
$query = $query->execute(array($username,$password,$forename,$surname,$email,$telephone,$address1,$town,$postcode));
if($query){
echo "User successfully registered";
header("Location:home2_template.html");
return;
} else {
echo "Fail";
}
} else {
echo "User already exists";
}
}
?>
<!-- Main part of homepage -->
<div class="jumbotron">
<div class="container">
<div id="registerBody">
<h2>Register your account</h2>
<p>All fields within the registration form must be filled in</p>
</div>
<div class = "register">
<form method="POST" class="form-horizontal" action="">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" name="username" value="<?php echo $username ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password" value="<?php echo $password ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="forename" class="col-sm-2 control-label">Forename:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="forename" name="forename" value="<?php echo $forename ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="surname" class="col-sm-2 control-label">Surname:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="surname" name="surname" value="<?php echo $surname ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email:</label>
<div class="col-sm-10">
<input type="email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" class="form-control" id="email" name="email" placeholder="Match email format of 'email#provider.com'" value="<?php echo $email ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="telephone" class="col-sm-2 control-label">Telephone:</label>
<div class="col-sm-10">
<input type="text" pattern="[0-9]{11}" class="form-control" id="telephone" name="telephone" placeholder="Match telephone format of 11 digits" value="<?php echo $telephone ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="address1" class="col-sm-2 control-label">Address:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="address1" name="address1" value="<?php echo $address1 ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="town" class="col-sm-2 control-label">Town:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="town" name="town" value="<?php echo $town ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="postcode" class="col-sm-2 control-label">Postcode:</label>
<div class="col-sm-10">
<input type="text" pattern ="[A-Za-z]{1,2}[0-9Rr][0-9A-Za-z]? [0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}" class="form-control" id="postcode" name="postcode" placeholder="Match postcode format of 'XX00 0XX' "value="<?php echo $postcode ?>"required="required">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input id="button" name="submit" type="submit" value="Register" class="btn btn-primary">
</div>
</div> <!-- registration button -->
</form>
</div> <!-- registration end -->
</div> <!-- container end -->
</div> <!-- jumbo end -->
<br>
<!--<div class="form-group">
<div class="col-sm-10">
<input id="submit" name="reg" type="submit" value="Register" class="btn btn-primary">
</div>
</div> -->
<!-- end of reg -->
<div class="container">
<br>
<footer>
<p>© Ballymena Sports 2014</p>
</footer>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="../../dist/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
Cheers
Stuart
Initialize all the variables with an empty string. You problem will be
resolved.
<?php
$username="";
$password="";
$forename="";
$surname="";
$email="";
$telephone="";
$address1="";
$town="";
$postcode="";
if(isset($_POST["submit"])){
$username=$_POST['username'];
$password=$_POST['password'];
$forename=$_POST['forename'];
$surname=$_POST['surname'];
$email=$_POST['email'];
$telephone=$_POST['telephone'];
............
..........
......
..}

Categories