This is my first question here. I hope I will find the answer.
As the title says I am unable to add users using form I created in html and I am unable to add a user using php function into database.
db.php
<?php
$db['db_host'] = 'localhost';
$db['db_user'] = 'root';
$db['db_pass'] = '';
$db['db_name'] = 'cms';
foreach($db as $key => $value){
define(strtoupper($key),$value);
}
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
//you need to exit the script, if there is an error
exit();
}
?>
add-user.php
<?php
require_once('inc/top.php');
if(!isset($_SESSION['username'])){
header('Location:login.php');
}
else if(isset($_SESSION['username']) and $_SESSION['role'] =='author'){
header('Location:index.php');
}
?>
</head>
<body>
<div id="wrapper">
<?php require_once('inc/header.php');?>
<div class="container-fluid body-section">
<div class="row">
<div class="col-md-3">
<?php require_once('inc/sidebar.php');?>
</div>
<div class="col-md-9">
<h1><i class="fa fa-user-plus"></i> Add User<small>Add
New User</small></h1>
<hr>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-tachometer">
</i> Dashboard</a></li>
<li class="active"><i class="fa fa-user-plus">
</i>Add New User</li>
</ol>
<?php
if(isset($_POST['submit'])){
$date=time();
$first_name = mysqli_real_escape_string($con,$_POST['first-name']);
$last_name = mysqli_real_escape_string($con,$_POST['last-name']);
$username = mysqli_real_escape_string($con,strtolower($_POST['username']));
$username_trim=preg_replace('/\s*/','',$username);
$email = mysqli_real_escape_string($con,strtolower($_POST['email']));
$password = mysqli_real_escape_string($con,$_POST['password']);
$role = $_POST['role'];
$image = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
$check_query="SELECT * FROM users WHERE username='$username' or email='$email'";
$check_run=mysqli_query($con,$check_query);
$salt_query="SELECT * FROM users ORDER BY id LIMIT 1";
$salt_run=mysqli_query($con,$salt_query);
$salt_row=mysqli_fetch_array($salt_run);
$salt=$salt_row['salt'];
$password=crypt($password, $salt);
if(empty($first_name) or empty($last_name) or empty($username) or empty($email) or empty($password) or empty($image)) {
$error="All field Required";
}
else if($username!=$username_trim){
$error="Don't use spaces in username";
}
else if(mysqli_num_rows($check_run)){
$error="Username or Email Already Exist";
}
else{
$insert_query="INSERT INTO `users` (`id`, `date`, `first_name`, `last_name`, `username`, `email`, `image`, `password`, `role`) VALUES (NULL, NULL', '$first_name', '$last_name', '$username', '$email', '$image', '$password', '$role')";
if(mysqli_query($con,$insert_query)){
$msg="User has been Added";
move_uploaded_file($image_tmp,"img/$image");
}
else{
$error="user has not been Added";
}
}
}
?>
<div class="row">
<div class="col-md-8">
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="first-name">First Name:*</label>
<?php
if(isset($error)){
echo "<span class='pull-right' style='color:red;'>$error</span>";
}
else if(isset($msg)){
echo "<span class='pull-right' style='color:green;'>$msg</span>";
}
?>
<input type="text" name="first-name" value="<?php if(isset($first_name)){ echo $first_name;}?>" id="first-name" class="form-control" placeholder="First Name">
</div>
<div class="form-group">
<label for="last-name">Last Name:*</label>
<input type="text" name="last-name" value="<?php if(isset($last_name)){ echo $last_name;}?>" id="last-name" class="form-control" placeholder="Last Name">
</div>
<div class="form-group">
<label for="username">Username:*</label>
<input type="text" name="username" id="username" value="<?php if(isset($username)){ echo $username;}?>" class="form-control" placeholder="Username">
</div>
<div class="form-group">
<label for="email">Email:*</label>
<input type="text" name="email" id="email" value="<?php if(isset($email)){ echo $email;}?>" class="form-control" placeholder="Email Address">
</div>
<div class="form-group">
<label for="Password">Password:*</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<label for="role">Role:*</label>
<select name="role" id="role" class="form-control">
<option value="author">Author</option>
<option value="admin">Admin</option>
</select>
</div>
<div class="form-group">
<label for="image">Profile Picture:*</label>
<input type="file" name="image" id="image">
</div>
<input type="submit" value="Add User" name="submit" class="btn btn-primary">
</form>
</div>
<div class="col-md-4">
<?php
if(isset($check_image)){
echo "<img src='img/$check_image' width='100%'>";
}
?>
</div>
</div>
</div>
</div>
</div>
<?php require_once('inc/footer.php');?>
I have done many searches but didn't find it to work the code and add user. whenever I try to add a user as "author" or "admin" it says "user has not been added".
Thanks in advance
Please try this query where you insert:
INSERT INTO `users` (`date`, `first_name`, `last_name`, `username`, `email`, `image`, `password`, `role`) VALUES (NULL, '$first_name', '$last_name', '$username', '$email', '$image', '$password', '$role');
Solved it by removing id and date because it is auto_increment and it should not have been used into insert into function
Related
I'm new to PHP and I'm trying to create a registration form and I'm getting the
"Incorrect date value: '' for column 'birthday' at row 1
error. At first I thought it was the query but I fixed it and I still get the error, it's just a different column. I've searched everywhere, and still nothing, please help.
my add.php code
<?=
$first_name = ( !empty($_GET['f-name']) ) ? $_GET['f-name']: "";
$last_name = ( !empty($_GET['l-name']) ) ? $_GET['l-name']: "";
$email = ( !empty($_GET['email']) ) ? $_GET['email']: "";
$password = ( !empty($_GET['psw']) ) ? $_GET['psw']: "";
$birthday = ( !empty($_GET['age']) ) ? $_GET['age']: "";
$phone = ( !empty($_GET['phone']) ) ? $_GET['phone']: "";
$ministry = ( !empty($_GET['ministry']) ) ? $_GET['ministry']: "";
$query = "INSERT INTO `member` (`first_name`, `last_name`, `email`, `password`, `birthday`, `phone`, `ministry`) VALUES ('$first_name', '$last_name', '$email', '$password', '$birthday', '$phone', '$ministry')";
$hostname = 'localhost';
$db_name = 'catacumba';
$username = 'root';
$password = 'mysql';
try{
$db = new mysqli($hostname, $username, $password, $db_name);
if ($db->query($query) === TRUE) {
echo "Record Created";
} else {
echo "Error Creating record: " . $db->error;
}
//close the connection to DB
$db->close();
}catch(Exception $e)
{
$error_message = $e->getMessage();
echo "<p>Error message: $error_message </p>";
}
?>
my create_account.php form
<form class="modal-content" action="Add.php" method="post">
<div class="container">
<button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">X</button>
<h1>Create Account</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<div class="parent">
<div class="child">
<div class="parent2">
<div class="child2">
<label for="f-name"><b>First Name</b></label>
<input type="text" placeholder="First Name" name="f-name" required>
</div>
<div class="child2">
<label for="l-name"><b>Last Name</b></label>
<input type="text" placeholder="Last Name" name="l-name" required>
</div>
</div>
<label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
</div>
<div class="child">
<label for="phone"><b>Phone</b></label>
<input type="tel" placeholder="Phone" name="phone" required>
<label for="ministry"><b>Ministry</b></label>
<input type="text" placeholder="Ministry" name="ministry" required>
<label for="age"><b>Birthday</b></label>
<input type="date" placeholder="Birthday" name="age" required>
</div>
</div>
<label>
<input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
</label>
<p>By creating an account you agree to our Terms & Privacy.</p>
<div class="clearfix" id="createbtn">
<button type="submit" class="signupbtn">Sign Up</button>
</div>
</div>
</form>
Incorrect value for column is a MySQL error related to the column datatype you have chosen for birthday.
If the birthday value you are inserting is any different than the MySQL date data type 2021-02-22 17:03:24 you will get this column type error.
Make sure the birthday dates you are inserting are in the format YYYY-MM-DD HH:MM:SS.
As for using mysqli, don't. Use PDO instead.
please i'm kinda new to website development. i tried to create a registration page to work with my database but my registration form page is not responding to the php coding page.
please i need your assistance. thank
This is my registration.html page
i don't know if the error is from my registration form page
<form action="student.php" name="register" id="register" method="POST" data-aos="fade">
<div class="form-group row">
<div class="col-md-6 mb-3 mb-lg-0">
<input type="text" name="firstname" id="firstname" class="form-control" placeholder="First name" required>
</div>
<div class="col-md-6">
<input type="text" name="lastname" id="lastname" class="form-control" placeholder="Last name" required>
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<input type="text" name="studentid" id="studentid" class="form-control" placeholder="Student ID" value="" required>
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<input type="text" name="level" class="form-control" placeholder="Level" required>
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<p class="mb-0">Gender</p>
<input name="gender" type="radio" value="m" required> Male
<input name="gender" type="radio" value="f" required> Female
</div>
</div>
<div class="form-group row">
<div id="date-picker" class="col-md-12 md-form md-outline input-with-post-icon datepicker" inline="true">
<input type="text" name="dob" class="form-control" id="date9" placeholder="DD/MM/YYYY" required>
<i class="fas fa-calendar input-prefix"></i>
</div>
</div>
<script>
$('.datepicker').datepicker({
inline: true;
});
</script>
<div class="form-group row">
<div class="col-md-12">
<input type="email" name="email" class="form-control" placeholder="Email" value="" required>
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<input type="phone" name="phonenumber" id="phonenumber" class="form-control" placeholder="+234 8179 5523 71" required>
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<input type="department" name="department" class="form-control" placeholder="Department" required>
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<input type="password" name="password" value="" id="password" class="form-control" placeholder="Password" required>
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<input type="password" name="confirmpassword" id="confirmpassword" value="" class="form-control" placeholder="Confirm Password" required>
</div>
</div>
<div class="form-group row">
<div class="col-md-6">
<input type="submit" name="submit" class="btn btn-primary py-3 px-5 btn-block btn-pill" value="SUBMIT">
</div>
</div>
</form>
This is my Php page for the form
or maybe the error is from my php code. please help detect the problem guys. Thanks.
<?php
session_start();
// initializing variables
$studentid = "";
$email = "";
$errors = array();
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'oneschool');
// REGISTER USER
if (isset($_POST['submit'])) {
// receive all input values from the form
$firstname = mysqli_real_escape_string($db, $_POST['firstname']);
$lastname = mysqli_real_escape_string($db, $_POST['lastname']);
$studentid = mysqli_real_escape_string($db, $_POST['studentid']);
$level = mysqli_real_escape_string($db, $_POST['level']);
$gender = mysqli_real_escape_string($db, $_POST['gender']);
$dob = mysqli_real_escape_string($db, $_POST['dob']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$phonenumber = mysqli_real_escape_string($db, $_POST['phonenumber']);
$department = mysqli_real_escape_string($db, $_POST['department']);
$password_1 = mysqli_real_escape_string($db, $_POST['password']);
$password_2 = mysqli_real_escape_string($db, $_POST['confirmpassword']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($studentid)) { array_push($errors, "Student ID 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 student_registra WHERE studentid='$studentid' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query) or die(mysqli_error($db));
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['studentid'] === $studentid & $user['email'] === $email) {
array_push($errors, "Student Id already taken");
}
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
$query = "INSERT INTO student_registra (firstname, lastname, studentid, level, gender, dob, email, phonenumber, department, password)
VALUES('$firstname', '$lastname', '$studentid', '$level', '$gender', '$dob', '$email', '$phonenumber', '$department', '$password')";
mysqli_query($db, $query);
$_SESSION['studentid'] = $studentid;
$_SESSION['success'] = "Registration Sucessful";
header('location: index.html');
}
}
i don't know what seems to be the problem, because i run it the first time it worked, but when i shutdown my laptop and turn it on back again after my lunch, it stop working.
Instead of it to read the .php code it's rather displaying the whole .php code and i've checked the code, i can't find what seems to be the problem.
please guys, i'll need your help in fixing this or detecting the problem.
thanks
EDIT, You have a lot of useless codes that make your code so slow, Like $_SESSION['success']; This is not neccessary, Change your index.html to index.php and delete it because it do nothing, You can check session by student id You have two gender Inputs, how comes you assign one of them? This is first mistake
Secondly, Use Prepared Statements to avoid SQLI Attacks
Thirdly How comes you header a html page when you're in php page? change index.html to index.php
And Use this code instead:
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$prepared = "INSERT INTO student_registra (firstname, lastname, studentid, level, gender, dob, email, phonenumber, department, password)
VALUES('$firstname', '$lastname', '$studentid', '$level', '$gender', '$dob', '$email', '$phonenumber', '$department', '$password')";
$query = $prepared;
mysqli_query($db, $query);
$_SESSION['studentid'] = $studentid;
# Change your files index.html To index.php
header('location: index.php');
}
I'm currently trying to complete this project for school and I would really appreciate some help.
I have been trying to learn PHP so I could extract data from my HTML and put it into my MySQL (which I'm accessing through XAMP). I have a problem that says:
The requested URL was not found on this server.
The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.' when I press the register button. I don't know what this means? I have a table called usertable in the database Login so I feel like it should work.
This is my PHP code:
<?php
session_start();
$con = mysqli_connect('localhost', 'root', 'ocr2020');
mysqli_select_db($con, 'login');
$name = $_POST['user'];
$pass = $_POST['pass'];
$fname= $_POST['forename'];
$sname = $_POST['surname'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$dateofB= $_POST['dateB'];
$s= "select * from usertable where name='$name'";
$s2= "select * from usertable";
$result= mysqli_query($con,$s);
$num= mysqli_num_rows($result);
$num2= mysqli_num_rows($s2);
$id= $num2+1;
if($num==1){
echo" Username Is No Longer Available";
}else{
$reg= " insert into usertable(patientID, Forename, Surname, Username, Password, Email, Mobile,
DateOfBirth) values ('$id', '$fname', '$sname', '$name', '$pass', '$email', '$mobile', '$dateOfB')";
mysqli_query($con, $reg);
echo" Registration Successful";
}
?>
As I said, I would really appreciate any help or advice. Thanks!
Edit:
Here is the updated code having taken on everyone's comments:
<?php
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);.
session_start();
$con = mysqli_connect('localhost', 'root', 'ocr2020');
mysqli_select_db($con, 'login');
$name = $_POST['user'];
$pass = $_POST['pass'];
$fname= $_POST['forename'];
$sname = $_POST['surname'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$dateofB= $_POST['dateB'];
$s= "select * from usertable where name='$name'";
$result= mysqli_query($con,$s);
$num= mysqli_num_rows($result);
if($num>0){
echo" Username Is No Longer Available";
}else{
$reg= " insert into usertable(patientID, Forename, Surname, Username, Password, Email, Mobile, DateOfBirth) values ('$id', '$fname', '$sname', '$name', '$pass', '$email', '$mobile', '$dateOfB')";
mysqli_query($con, $reg);
echo" Registration Successful";
}
?>
Here is the code from the login.php page which is where I press the 'register button' and it creates the problem:
<html>
<section id="loginBox">
<div class="container">
<div class="login-box">
<div class="row">
<div class="col-md-6 login-left">
<h3> Login Here </h3>
<form action="registration.php" method="POST">
<div class="form-group">
<label> Username </label>
<input type="text" name="user" placeholder="Enter your username" class="form-control" required>
</div>
<div class="form-group">
<label> Password </label>
<input type="password" name="password" placeholder="Enter your password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary"> Login </button>
</form>
</div>
<div class="col-md-6 login-right">
<h3> Register Here </h3>
<form action="validation.php" method="POST">
<div class="form-group">
<label> Username </label>
<input type="text" name="user" placeholder="Enter your username" class="form-control" required>
</div>
<div class="form-group">
<label> Password </label>
<input type="password" name="password" placeholder="Enter your password" class="form-control" required>
</div>
<div class="form-group">
<label> Forename </label>
<input type="text" name="forename" placeholder="Enter your forename" class="form-control" required>
</div>
<div class="form-group">
<label> Surname </label>
<input type="text" name="surname" placeholder="Enter your surname" class="form-control" required>
</div>
<div class="form-group">
<label> Mobile</label>
<input type="text" name="mobile" placeholder="Enter your mobile" class="form-control" required>
</div>
<div class="form-group">
<label> Email </label>
<input type="text" name="email" placeholder="Enter your email" class="form-control" required>
</div>
<div class="form-group">
<label> Date of Birth</label>
<input type="date" name="dateB" placeholder="Enter your date of birth" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary"> Register </button>
</form>
</div>
</div>
</div>
</div>
</section>
</body>
</html>
I'm facing this problem in my server, while in localhost xampp it works very well with out any problem. I tried more time to solve this problem but in vain.
I already try to publish post in phpmyadmin and copy the code according to ( INSERT INTO posts (id, date, title, author, author_image, image, categories, tags, post_data, views, status) VALUES (NULL, '$date', '$title', '$author', '$author_image', '$image', '$categories', '$tags', '$post_data', '0', '$status')"; )
<?php
require_once('include/top.php');
if(!isset($_SESSION['username'])){
header('Location: login.php');
}
$session_username = $_SESSION['username'];
$session_author_image = $_SESSION['author_image'];
?>
</head>
<body>
<div id="wrapper">
<!-- NAVBAR STARTING FROM HERE --->
<?php require_once('include/header.php'); ?>
<!-- NAVBAR ENDING HERE -->
<!-- BODY CONTENT STARTING FROM HERE -->
<div class="container-fluid body-section">
<div class="row">
<div class="col-md-3">
<?php require_once('include/sidebar.php'); ?>
</div>
<div class="col-md-9">
<h1><i class="fa fa-plus-square" aria-hidden="true"></i> Add Post <small>Add New Post</small></h1><hr>
<ol class="breadcrumb">
<li><i class="fa fa-tachometer" aria-hidden="true"></i> Dashboard</li>
<li class="active"><i class="fa fa-plus-square" aria-hidden="true"></i> Add Post</li>
</ol>
<?php
if(isset($_POST['submit'])){
$date = date('Y-m-d h:ia');
$id = $_POST['id'];
$title = mysqli_real_escape_string($con, $_POST['title']);
$categories = $_POST['categories'];
$tags = mysqli_real_escape_string($con, $_POST['tags']);
$status = $_POST['status'];
$session_author_image = $_SESSION['author_image'];
$post_data = mysqli_real_escape_string($con, $_POST['post-data']);
$image = $_FILES['image']['name'];
$tmp_name = $_FILES['image']['tmp_name'];
if(empty($title) or empty($post_data) or empty($tags) or empty($image)){
$error = "All ( * ) Fields Are Required";
}
else{
$insert_query = "INSERT INTO `posts` (`id`, `date`, `title`, `author`, `author_image`, `image`, `categories`, `tags`, `post_data`, `views`, `status`) VALUES ('$id', '$date', '$title', '$author', '$session_author_image', '$image', '$categories', '$tags', '$post_data', '0', '$status')";
if(mysqli_query($con, $insert_query)){
$msg = "Post Has Been Added Successfuly";
$path = "img/$image";
$title = "";
$post_data = "";
$tags = "";
$categories = "";
$status = "";
if(move_uploaded_file($tmp_name, $path)){
copy($path, "../$path");
}
}
else{
$error = "Post Has Not Been Posted";
}
}
}
?>
<div class="row">
<div class="col-xs-12">
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title:*</label>
<?php
if(isset($msg)){
echo "<span class='pull-right' style='color:green'>$msg</span>";
}
else if(isset($error)){
echo "<span class='pull-right' style='color:red'>$error</span>";
}
?>
<input class="form-control" value="<?php if(isset($title)){echo $title;}?>" type="text" name="title" placeholder="Post Title.." >
</div>
<div class="form-group">
Media
</div>
<div class="form-group">
<textarea name="post-data" class="form-control" id="textarea" cols="30" rows="10" placeholder="Post Text Here.."><?php if(isset($post_data)){echo $post_data;}?></textarea>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="file">Post Image:*</label>
<input type="file" name="image" >
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="categories">Categories:*</label>
<select class="form-control" name="categories" id="categories">
<?php
$cat_query = "SELECT * FROM categories ORDER BY id DESC";
$cat_run = mysqli_query($con, $cat_query);
if(mysqli_num_rows($cat_run) > 0){
while($cat_row = mysqli_fetch_array($cat_run)){
$cat_name = $cat_row['category'];
echo "<option value='".$cat_name."' ".((isset($categories) and $categories == $cat_name)?"selected":"").">".ucfirst($cat_name)."</option>";
}
}
?>
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="tags">Tags:*</label>
<input value="<?php if(isset($tags)){echo $tags;}?>" class="form-control" type="text" name="tags" placeholder="Tags Separated By Comma e.g tag1, tag2" >
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="status">Status:*</label>
<select class="form-control" name="status" id="status">
<option value="draft" <?php if(isset($status) and $status == 'draft'){echo "selected";}?></optio>Draft</option>
<option value="publish" <?php if(isset($status) and $status == 'publish'){echo "selected";}?>>Publish</option>
</select>
</div>
</div>
<input type="submit" class="btn btn-primary" value="Publish Post" name="submit">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<?php require_once('include/footer.php');?>
I have created a signup form for my php website using Bootstrap but nothing happens when I click on register. Signup form is made in Bootstrap and it is not working.
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
<?php
require_once("company-db.php");
if (!isset($_POST['submit'])) {
?>
<form role="form" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<h2>Please Sign Up <small>It's free and always will be.</small></h2>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="username" id="username" class="form-control input-lg" placeholder="Username" tabindex="1">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password" tabindex="2">
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="company_name" id="company_name" class="form-control input-lg" placeholder="Company Name" tabindex="3">
</div>
<div class="form-group">
<input type="text" name="description" id="description" class="form-control input-lg" placeholder="Company Description" tabindex="4">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email Address" tabindex="4">
</div>
<div class="row">
<div class="col-xs-4 col-sm-3 col-md-3">
<span class="button-checkbox">
<button type="button" class="btn" data-color="info" tabindex="7">I Agree</button>
<input type="checkbox" name="t_and_c" id="t_and_c" class="hidden" value="1">
</span>
</div>
<div class="col-xs-8 col-sm-9 col-md-9">
By clicking <strong class="label label-primary">Register</strong>, you agree to the Terms and Conditions set out by this site, including our Cookie Use.
</div>
</div>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-12 col-md-6"><input type="submit" value="submit" class="btn btn-primary btn-block btn-lg" tabindex="7"></div>
<div class="col-xs-12 col-md-6">Sign In</div>
</div>
</form>
<?php
} else {
## connect mysql server
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
## query database
# prepare data for insertion
$username = $_POST['username'];
$password = $_POST['password'];
$company_name = $_POST['company_name'];
$description = $_POST['description'];
$email = $_POST['email'];
# check if username and email exist else insert
$exists = 0;
$result = $mysqli->query("SELECT username from companies WHERE username = '{$username}' LIMIT 1");
if ($result->num_rows == 1) {
$exists = 1;
$result = $mysqli->query("SELECT email from companies WHERE email = '{$email}' LIMIT 1");
if ($result->num_rows == 1) $exists = 2;
} else {
$result = $mysqli->query("SELECT email from companies WHERE email = '{$email}' LIMIT 1");
if ($result->num_rows == 1) $exists = 3;
}
if ($exists == 1) echo "<p>Username already exists!</p>";
else if ($exists == 2) echo "<p>Username and Email already exists!</p>";
else if ($exists == 3) echo "<p>Email already exists!</p>";
else {
# insert data into mysql database
$sql = "INSERT INTO `companies` (`id`, `username`, `password`, `company_name`, `description`, `email`)
VALUES (NULL, '{$username}', '{$password}', '{$company_name}', '{$description}', '{$email}')";
if ($mysqli->query($sql)) {
//echo "New Record has id ".$mysqli->insert_id;
echo "<p>Registred successfully!</p>";
} else {
echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
exit();
}
}
}
?>
</div>
</div>
You don't have a name for your submit button, so this won't get posted.
<input type="submit" value="submit" name="submit"
class="btn btn-primary btn-block btn-lg" tabindex="7">
Give the name attribute and make it set.
Note: You must never rely on Submit button's attribute!
The (!isset($_POST['submit'])) conditional statement depends on the execution of your code.