input values didn't store in mysql database - php

Here i have create a form with login and signup.
When i run our code, it shows undefined variable in message and couldn't store user input values at the back-end.
Here is full code for your reference:
db.php:
<?php
$connection = mysqli_connect('localhost','root','','sign-log') or die(mysqli_error($connection));
?>
index.php(with css):
<?php
include('db.php');
if(isset($_POST['action']))
{
if($_POST['action']=="login")
{
$email = mysqli_real_escape_string($connection,$_POST['email']);
$password = mysqli_real_escape_string($connection,$_POST['password']);
$strSQL = mysqli_query($connection,"select name from users where email='".$email."' and password='".md5($password)."'");
$Results = mysqli_fetch_array($strSQL);
if(count($Results)>=1)
{
$message = $Results['name']." Login Sucessfully!!";
}
else
{
$message = "Invalid email or password!!";
}
}
elseif($_POST['action']=="signup")
{
$name = mysqli_real_escape_string($connection,$_POST['name']);
$email = mysqli_real_escape_string($connection,$_POST['email']);
$password = mysqli_real_escape_string($connection,$_POST['password']);
$query = "SELECT email FROM users where email='".$email."'";
$result = mysqli_query($connection,$query);
$numResults = mysqli_num_rows($result);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) // Validate email address
{
$message = "Invalid email address please type a valid email!!";
}
elseif($numResults>=1)
{
$message = $email." Email already exist!!";
}
else
{
mysql_query("insert into users(name,email,password) values('".$name."','".$email."','".md5($password)."')");
$message = "Signup Sucessfully!!";
}
}
}
echo '<script type="text/javascript" src="jquery-1.8.0.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<style type="text/css">
input[type=text]
{
border: 1px solid #ccc;
border-radius: 3px;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
width:200px;
min-height: 28px;
padding: 4px 20px 4px 8px;
font-size: 12px;
-moz-transition: all .2s linear;
-webkit-transition: all .2s linear;
transition: all .2s linear;
}
input[type=text]:focus
{
width: 400px;
border-color: #51a7e8;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.1),0 0 5px rgba(81,167,232,0.5);
outline: none;
}
input[type=password]
{
border: 1px solid #ccc;
border-radius: 3px;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
width:200px;
min-height: 28px;
padding: 4px 20px 4px 8px;
font-size: 12px;
-moz-transition: all .2s linear;
-webkit-transition: all .2s linear;
transition: all .2s linear;
}
input[type=password]:focus
{
width: 400px;
border-color: #51a7e8;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.1),0 0 5px rgba(81,167,232,0.5);
outline: none;
}
</style>
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>
</head>
<body>
<b>'.$message.'</b>
<div id="tabs" style="width: 480px;">
<ul>
<li>Login</li>
<li>Signup</li>
</ul>
<div id="tabs-1">
<form action="" method="post">
<p><input id="email" name="email" type="text" placeholder="Email"></p>
<p><input id="password" name="password" type="password" placeholder="Password">
<input name="action" type="hidden" value="login" /></p>
<p><input type="submit" value="Login" /></p>
</form>
</div>
<div id="tabs-2">
<form action="" method="post">
<p><input id="name" name="name" type="text" placeholder="Name"></p>
<p><input id="email" name="email" type="text" placeholder="Email"></p>
<p><input id="password" name="password" type="password" placeholder="Password">
<input name="action" type="hidden" value="signup" /></p>
<p><input type="submit" value="Signup" /></p>
</form>
</div>
</div>';
?>
db.sql:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(240) NOT NULL,
`email` varchar(240) NOT NULL,
`password` varchar(240) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
How to store input values at the back-end and how to fix undefined variable in message.
Can anyone help me?

I can't seem to get the point accross in a comment, so here is more complete explanation: You are mixing mysqli_* functions with the deprecated mysql_* functions. This is unrelated to the undefined variable warning you get.
The problem is here:
else
{
mysql_query("insert into users(name,email,password) values('".$name."','".$email."','".md5($password)."')");
// ^^^^^^^^^^^ not good
$message = "Signup Sucessfully!!";
}
mysqli_* and mysql_* functions are not exchangable like that, you could use both but then you would have to open a database connection for each.
So to solve your problem you need to do the same as in the previous query:
$result = mysqli_query($connection,'insert ......');

the variable $message is inside the body of IF, initialize it before isset like this
$message = '';
if(isset($_POST['action']))
{.....

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']) ){......

Make html form text field required

I have a problem concerning how to add data from a text field into a SQL table on a database. I would like the text fields to require some sort of data so there can be no blank data in the table. However, I cannot seem to get it to work.
The code below shows my problem.
The page where the user is created:
<html>
<head>
<title>Eksamensprojekt</title>
<style>
.outer {
display: table;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.login-form {
margin-left: auto;
margin-right: auto;
width: 400px;
font-family: Tahoma, Geneva, sans-serif;
}
.login-form h1 {
text-align: center;
color: #4d4d4d;
font-size: 24px;
padding: 20px 0 20px 0;
}
.login-form input[type="password"],
.login-form input[type="text"] {
width: 100%;
padding: 15px;
border: 1px solid #dddddd;
margin-bottom: 15px;
box-sizing:border-box;
}
.login-form input[type="submit"] {
width: 100%;
padding: 15px;
background-color: #535b63;
border: 0;
box-sizing: border-box;
cursor: pointer;
font-weight: bold;
color: #ffffff;
margin: 0;
}
</style>
</head>
<body>
<div class="outer">
<div class="middle">
<div class="login-form">
<h1>Associhunt</h1>
<form method="get">
<input type="text" name="firstname" placeholder="First name"><br>
<span class="error">* <?php echo $nameErr;?></span>
<input type="text" name="email" placeholder="E-mail"><br>
<input type="text" name="tlfnr" placeholder="Phone"><br>
<input type="text" name="position" placeholder="Current job position"><br>
<input type="text" name="username" placeholder="Username"><br>
<input type="text" name="password" placeholder="Password"><br>
<input type="submit" value="Opret bruger" name="opretbruger">
</form>
</div>
<div class="login-form">
<form method="get" action="../login/">
<input type="submit" value="Already have an account?">
</form>
</div>
</div>
</div>
<?php
$conn = new mysqli($servername, $username, $password,$dbnavn);
if ($conn->connect_error) die("Fejl: " . $conn->connect_error);
if (isset($_GET['opretbruger']))
{
$firstname=$_GET['firstname'];
if ($firstname===NULL) die();
$lastname=$_GET['lastname'];
if ($lastname===NULL) die();
$email=$_GET['email'];
if ($email===NULL) die();
$tlfnr=$_GET['tlfnr'];
if ($tlfnr===NULL) die();
$position=$_GET['position'];
if ($position===NULL) die();
$username=$_GET['username'];
if ($username===NULL) die();
$password=$_GET['password'];
if ($password===NULL) die();
$sql="INSERT INTO UserData
(
firstname,
lastname,
email,
tlfnr,
position,
username,
password
)
VALUES ('".$firstname."','".$lastname."','".$email."','".$tlfnr."','".$position."','".$username."','".$password."')";
$conn->query($sql);
if ($conn->affected_rows >0 )
{
echo "Bruger oprettet!";
}
else
{
echo "Bruger ikke oprettet!";
};
};
?>
</body>
</html>
The table creation:
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE UserData (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
tlfnr INT(8) NOT NULL,
position VARCHAR(50),
username VARCHAR(40) NOT NULL,
password VARCHAR(40) NOT NULL,
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table UserData created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
You can use HTML input required attribute - for example, change:
<input type="text" name="email" placeholder="E-mail">
to
<input type="text" name="email" placeholder="E-mail" required>
Have you look at the required attribute ?
Below your code with the attribute added :
<html>
<head>
<title>Eksamensprojekt</title>
<style>
.outer {
display: table;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.login-form {
margin-left: auto;
margin-right: auto;
width: 400px;
font-family: Tahoma, Geneva, sans-serif;
}
.login-form h1 {
text-align: center;
color: #4d4d4d;
font-size: 24px;
padding: 20px 0 20px 0;
}
.login-form input[type="password"],
.login-form input[type="text"] {
width: 100%;
padding: 15px;
border: 1px solid #dddddd;
margin-bottom: 15px;
box-sizing:border-box;
}
.login-form input[type="submit"] {
width: 100%;
padding: 15px;
background-color: #535b63;
border: 0;
box-sizing: border-box;
cursor: pointer;
font-weight: bold;
color: #ffffff;
margin: 0;
}
</style>
</head>
<body>
<div class="outer">
<div class="middle">
<div class="login-form">
<h1>Associhunt</h1>
<form method="get">
<input type="text" name="firstname" required placeholder="First name"><br>
<span class="error">* <?php echo $nameErr;?></span>
<input type="text" name="email" required placeholder="E-mail"><br>
<input type="text" name="tlfnr" required placeholder="Phone"><br>
<input type="text" name="position" required placeholder="Current job position"><br>
<input type="text" name="username" required placeholder="Username"><br>
<input type="text" name="password" required placeholder="Password"><br>
<input type="submit" value="Opret bruger" name="opretbruger">
</form>
</div>
<div class="login-form">
<form method="get" action="../login/">
<input type="submit" value="Already have an account?">
</form>
</div>
</div>
</div>
<?php
$conn = new mysqli($servername, $username, $password,$dbnavn);
if ($conn->connect_error) die("Fejl: " . $conn->connect_error);
if (isset($_GET['opretbruger']))
{
$firstname=$_GET['firstname'];
if ($firstname===NULL) die();
$lastname=$_GET['lastname'];
if ($lastname===NULL) die();
$email=$_GET['email'];
if ($email===NULL) die();
$tlfnr=$_GET['tlfnr'];
if ($tlfnr===NULL) die();
$position=$_GET['position'];
if ($position===NULL) die();
$username=$_GET['username'];
if ($username===NULL) die();
$password=$_GET['password'];
if ($password===NULL) die();
$sql="INSERT INTO UserData
(
firstname,
lastname,
email,
tlfnr,
position,
username,
password
)
VALUES ('".$firstname."','".$lastname."','".$email."','".$tlfnr."','".$position."','".$username."','".$password."')";
$conn->query($sql);
if ($conn->affected_rows >0 )
{
echo "Bruger oprettet!";
}
else
{
echo "Bruger ikke oprettet!";
};
};
?>
</body>
</html>

Duplicating Empty Data in SQL when refreshing

I've been working on a system that allows a user to insert data, which will pass into a MySql database using SQL querieshowever, I've noticed that every time the page is refreshed, empty data is inserted into the database. Connection, queries, PHP, HTML and every other code works. The redirection works as intended but I realise my system just needs a slight correction. Am I doing something wrong?
<?php
$con = mysqli_connect("localhost","root","","system");
$error = 0;
$errormessage = "";
if( isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_GET['email'];
$password = $_POST['password'];
$password = hash('sha256' , $password);
}else{
$errormessage = $errormessage . "Please Fill all Fields";
$error++;
}
if($errors == 0){
$sql = "INSERT INTO User
(name, email, password)
VALUES
('$name','$email','$password')";
mysqli_query($con, $sql) or die (mysqli_error($con));
$errormessage = "Data Successfully Entered";
}
?>
<!DOCTYPE html>
<html>
<body>
<header>
<div style="background-color:black"><div style="text-align:center;"> <h1><style="color:Black;">Register</h1></div></div> </header>
<style>
input[type=text], select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 5px solid magenta;
border: 5px solid magenta;
border-radius: 4px;
box-sizing: border-box;
align:center;
}
input[type=button] {
width: 50%;
background-color: #4CAF50;
color: white;
padding: 14px 40px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
align:center;
}
input[type=password], select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 5px solid magenta;
border: 5px solid magenta;
border-radius: 4px;
box-sizing: border-box;
align:center;
}
input[type=email], select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 5px solid magenta;
border: 5px solid magenta;
border-radius: 4px;
box-sizing: border-box;
align:center;
}
input[type=submit] {
width: 50%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
align:center;
}
input[type=submit]:hover {
background-color: green;
}
div {
margin-left:auto;
margin-right:auto;
width: 90%;
border: 5px solid lime;
border-radius: 5px;
background-color: #ECF0F1;
padding: 20px;
}
</style>
</nav>
<div>
<form method="Post">
<label for="name">
<div style="text-align:center;"><input type= "button" id="RegisterPage" value = "Go to Register Page" onclick="document.location.href='example.php'">
<input type= "button" id="LoginPage" value = "Go to Login Page" onclick="document.location.href='http://localhost/example2.php'"><p>Your Name</label><br>
<input type="text" id="name" name="name" placeholder="Enter Name" required><br>
<label for="email">E-Mail:</label><br>
<input type="email" id="email" name="email" placeholder="Your e-mail" required><br>
<label for="password">Password:</label><br>
<input type="password" name="password" placeholder="At least 6 characters" required><br>
<input type="submit" name = "submit" value="Create your Account ">
</form></div>
<footer><div style="background-color:aqua; border: 5px solid black;">
</form></div>
</footer>
</nav>
</body>
</html>
You can do a ON DUPLICATE KEY UPDATE with your insert statement to update the database records instead of entering more. Also, make sure to setup your primary keys in your DB.
$sql = "INSERT INTO User
(name, email, password)
VALUES
('$name','$email','$password')
ON DUPLICATE KEY UPDATE
name = `$name`, email = `$email`, `$password`";

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.

PHP Echo Returns Blank Page

I have "checks" in here and if something happens, it will echo a phrase like The username you supplied is already in use or whatever. When it DOES display it though, it'll only display it on a blank page with that text. What is wrong here?
PHP
<?php
include('./dbconnect/global.php');
if ($_POST['register']) {
$username = mysql_real_escape_string(strip_tags($_POST['username']));
$password = mysql_real_escape_string(strip_tags($_POST['password']));
$email = mysql_real_escape_string(strip_tags($_POST['email']));
if (!$username||!$password||!$email)
echo "PLease Fill in The Required Fields";
else {
//check if username is taken
$check = mysql_query("SELECT * FROM users WHERE username='$username'");
if (mysql_num_rows($check) >= 1)
echo "The Username you Supplied Is Already in Use! <a href='./register'>Back</a>";
$check2 = mysql_query("SELECT * FROM users WHERE email='$email'");
if (mysql_num_rows($check) >= 1)
echo "The Email you Supplied Is Already in Use! <a href='./register'>Back</a>";
else {
$password2 = md5($password);
$register = mysql_query("value and stuff)") or die(mysql_error());
echo "Thanks for registering, $username! <a href='./index'>Home</a>";
}
}
}
else {
?>
HTML
<html>
<head>
<title>CoreCrafters</title>
<link rel="stylesheet" type="text/css" href="./css/main.css">
</head>
<body>
<?php include('./include/nbar.php') ?>
<div id="main">
<div id="register">
<div id="wrapper">
<form action="register" method="POST">
Username
<br />
<input type="username" name="username">
<br />
Password
<br />
<input type="password" name="password">
<br />
Email
<br />
<input type="email" name="email">
<br /><br />
<input type="submit" name="register" value="Register">
</form>
</div>
</div>
</div>
<?php include('./include/footer.php') ?>
</body>
</html>
Ending PHP (From ELSE at end)
<?php
}
?>
CSS
body {
margin: 0;
padding: 0;
background-color: #EEE;
}
#headerbar {
width: 100%;
background-color: #000;
}
a {
color: #069;
font-weight: bold;
text-decoration: none;
transition: all 0.2s linear;
-o-transition: all 0.2s linear;
-ms-transition: all 0.2s linear;
-moz-transition: all 0.2s linear;
-webkit-transition: all 0.2s linear;
}
a:hover {
color: #c00;
}
#nav {
padding: 0;
width: 100%;
float: left;
list-style: none;
margin: 0 0 3em 0;
background-color: #f2f2f2;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
#nav li {
float: left;
}
#nav li a {
color: #069;
display: block;
font-weight: bold;
padding: 8px 15px;
text-decoration: none;
transition: all 0.2s linear;
border-right: 1px solid #ccc;
-o-transition: all 0.2s linear;
-ms-transition: all 0.2s linear;
-moz-transition: all 0.2s linear;
-webkit-transition: all 0.2s linear;
}
#nav li a:hover {
color: #c00;
background-color: #fff;
}
#footer {
width: 94%;
padding: 10px;
margin-top: 10px;
margin-left: auto;
padding-left: 0px;
text-align: center;
margin-right: auto;
padding-right: 0px;
border: 1px solid #DDD;
background-color: #FFF;
}
#main {
width: 94%;
margin-top: 50px;
min-height: 500px;
margin-left: auto;
margin-right: auto;
border: 1px solid #DDD;
background-color: #FFF;
}
#register {
width: 15%;
margin-top: 20px;
margin-left: auto;
margin-right: auto;
text-align: center;
background-color: #CCC;
border: 1px solid #AAA;
}
#logbar {
width: 150px;
float: right;
min-height: 36px;
padding-left: 5px;
padding-right: 50px;
background-color: #DDD;
}
As requested, I posted most of the page onto here.
Your webpage has no content except that echo, so it is echo-ing it on a blank page because you told it to.
Update: Code
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8"> <!-- UXSS UTF-7 (IE6) prevention -->
<title>CoreCrafters</title>
<link rel="stylesheet" type="text/css" href="./css/main.css">
</head>
<body>
<?php #include('./include/nbar.php'); ?>
<div id="main">
<div id="register">
<div id="wrapper">
<form method="POST">
Username<br /><input type="username" name="username"><br />
Password<br /><input type="password" name="password"><br />
Email<br /><input type="email" name="email"><br /><br />
<input type="submit" name="register" value="Register">
</form>
</div>
<?php
#include('./dbconnect/global.php');
if (isset($_POST['register'])) {
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
die('Please fill out the required fields.');
} else {
$query = mysql_query("SELECT * FROM users WHERE username = '" . mysql_real_escape_string($_POST['username']) . "'");
if (mysql_num_rows($query)) { die('The username you provided is already in use!'); }
$query = mysql_query("SELECT * FROM users WHERE email = '" . mysql_real_escape_string($_POST['email']) . "'");
if (mysql_num_rows($query)) { die('The email you provided is already in use!'); }
/** Insert values **/
echo 'Thanks for registering, ' . htmlentities($_POST['username'], ENT_QUOTES) . '!'; /** Normal XSS prevention **/
}
}
?>
</div>
</div>
<?php #include('./include/footer.php'); ?>
</body>
</html>
I believe you are missing semicolons for your include statements (inside the HTML block). Such as:
<?php include('./include/footer.php'); ?>
If that doesn't solve it, you should check your error log. A completely blank page is often what you get when the PHP parser was unable to parse your code.

Categories