Cannot insert values into database from HTML Forms - php

I'm having trouble inserting values to the database from the HTML form. Here is my code:
http://pastebin.com/HhynqRnF
Can someone help me out? Thanks!

first you need to create mysql connection .and change your code into this code.
<?php
$con = mysqli_connect("localhost", "root", "", "my_db"); //create connection
if(isset($_POST['submit']))
{
$name = $_POST['fullname'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "INSERT INTO users (name, username, email, password) VALUES ('$name', '$username', '$email', '$password')";
$result = mysqli_query($con, $sql);
}
?>
<form action="" method="post">
<div class="field-wrap">
<label>
Name<span class="req">*</span>
</label>
<input name="fullname" type="text" required autocomplete="off" />
</div>
<div class="field-wrap">
<label>
Username<span class="req">*</span>
</label>
<input name="username" type="text" required autocomplete="off" />
</div>
<div class="field-wrap">
<label>
Email Address<span class="req">*</span>
</label>
<input name="email" type="text" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Set A Password<span class="req">*</span>
</label>
<input name="password" type="password" required autocomplete="off"/>
</div>
<button type="submit" class="button button-block"/>Create Account</button>
</form>

Try Using This
$sql = "INSERT INTO `users` (`name`, `username`, `email`, `password`) VALUES ('$name', '$username', '$email', '$password')";

Related

Using PHP Session variables in SQL queries [duplicate]

Im using the following code to insert users into a table called 'accounts'
session_start();
include("include/connect.php");
//Posted information from the form put into variables
$username = mysqli_escape_string($conn, $_POST["username"]);
$email = mysqli_escape_string($conn,$_POST["email"]);
$password = mysqli_escape_string($conn,$_POST["password_1"]);
//check if user already exists
$usernameCheck="SELECT * FROM accounts WHERE username='$username'";
//query the db
$usernameResult = mysqli_query($conn, $usernameCheck) or die(mysqli_error($conn));
//if no users exits then add data
if(mysqli_num_rows($usernameResult) == 0){
$AddUser = "INSERT INTO `accounts` (`username`, `email`, `password`) VALUES ('$username', '$email', '$password')";
$newUserSend = mysqli_query($conn, $AddUser) or die(mysqli_error($conn));
mysqli_close($conn);
$_SESSION["user_session"]= $username;
}
However every time it runs it inserts a row with the correct data and then a blank row below. For example
ID Name email password
1 test test#test.com test
2
This is the form where the data is posted from:
<form method="POST" class="signup-form" action="RegisterProcess.php">
<h2 class="form-title">Create account</h2>
<div class="form-group">
<input type="text" value="<?php echo $username; ?>" class="form-input" name="username" placeholder="Your Name" required/>
</div>
<div class="form-group">
<input type="email" value="<?php echo $email; ?>" class="form-input" name="email" placeholder="Your Email Address" required/>
</div>
<div class="form-group">
<input type="password" class="form-input" name="password_1" placeholder="Your Password" required/>
<span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
</div>
<div class="form-group">
<input type="password" class="form-input" name="password_2" placeholder="Confirm your password" required/>
<span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
</div>
<div class="form-group">
<input type="submit" class="form-submit" value="Sign up"/>
</div>
</form>

PHP inserts duplicate rows into database

Im using the following code to insert users into a table called 'accounts'
session_start();
include("include/connect.php");
//Posted information from the form put into variables
$username = mysqli_escape_string($conn, $_POST["username"]);
$email = mysqli_escape_string($conn,$_POST["email"]);
$password = mysqli_escape_string($conn,$_POST["password_1"]);
//check if user already exists
$usernameCheck="SELECT * FROM accounts WHERE username='$username'";
//query the db
$usernameResult = mysqli_query($conn, $usernameCheck) or die(mysqli_error($conn));
//if no users exits then add data
if(mysqli_num_rows($usernameResult) == 0){
$AddUser = "INSERT INTO `accounts` (`username`, `email`, `password`) VALUES ('$username', '$email', '$password')";
$newUserSend = mysqli_query($conn, $AddUser) or die(mysqli_error($conn));
mysqli_close($conn);
$_SESSION["user_session"]= $username;
}
However every time it runs it inserts a row with the correct data and then a blank row below. For example
ID Name email password
1 test test#test.com test
2
This is the form where the data is posted from:
<form method="POST" class="signup-form" action="RegisterProcess.php">
<h2 class="form-title">Create account</h2>
<div class="form-group">
<input type="text" value="<?php echo $username; ?>" class="form-input" name="username" placeholder="Your Name" required/>
</div>
<div class="form-group">
<input type="email" value="<?php echo $email; ?>" class="form-input" name="email" placeholder="Your Email Address" required/>
</div>
<div class="form-group">
<input type="password" class="form-input" name="password_1" placeholder="Your Password" required/>
<span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
</div>
<div class="form-group">
<input type="password" class="form-input" name="password_2" placeholder="Confirm your password" required/>
<span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
</div>
<div class="form-group">
<input type="submit" class="form-submit" value="Sign up"/>
</div>
</form>

WAMP object not found- URL not found on server?

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>

Custom email address with Bootstrap

I have a registration form, with "Username", "Email", "Password" fields.
I would like that only email with "#mychoice.it" (it's only an example) can be used.
hitoeveryone#mychoice.it -> ok
erika#mychoice.it -> ok
anthony#gmail.it -> NO!!
It is possible? I'm using bootstrap and PHP.
This is my form:
<form method="post" action="registration.php" >
<div class="form-group" id="form-login" >
<label for="exampleInputEmail1">Indirizzo Email</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="nome.cognomeX#studio.unibo.it" name="Input_Email" required>
</div>
<div class="form-group" id="form-login">
<label for="exampleInputUsername1">Username</label>
<input type="name" class="form-control" id="exampleInputUsername1" placeholder="username" name="Input_Username" required>
</div>
<div class="form-group" id="form-login">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="example_InputPassword1" name="Input_Password" placeholder="password" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
<button type="reset" class="btn btn-default" value="Reset">Reset</button>
and this is my PHP script for registration
<?php
session_start();
require 'connect.php';
if(isset($_POST['Input_Username'])) {
$username = $conn->real_escape_string($_POST['Input_Username']);
}
if(isset($_POST['Input_Email'])) {
$email = $conn->real_escape_string($_POST['Input_Email']);
}
if(isset($_POST['Input_Password'])) {
$password = $conn->real_escape_string($_POST['Input_Password']);
}
$sql = "INSERT INTO utente (Email, Username, Password) VALUES ('$email', '$username', '$password')";
$result = $conn->query($sql);
$conn->close();
header("location:prova.php");
?>
<?php
session_start();
require 'connect.php';
$allowed_domains = array("example.com","example2.com");
if(isset($_POST['Input_Username'])) {
$username = $conn->real_escape_string($_POST['Input_Username']);
}
if(isset($_POST['Input_Email'])) {
$email = $conn->real_escape_string($_POST['Input_Email']);
}
if(isset($_POST['Input_Password'])) {
$password = $conn->real_escape_string($_POST['Input_Password']);
}
$email = explode("#",$email);
if (in_array($email[1],$allowed_domains)) {
$sql = "INSERT INTO utente (Email, Username, Password) VALUES ('$email', '$username', '$password')";
$result = $conn->query($sql);
$conn->close();
header("location:prova.php");
}else{
DO SOMETHING ELSE HERE
}
?>

html/php form to mysql

I am new to coding and stackoverflow so I apologise in advance. I have looked through stackoverflow but cannot find the answer to my problem. I have a html form set up which then saves the email and password to mysql no problem. I wish to add name, DOB, Town, Country to these inputs which I have done so in the html below:
<form class="marginTop" method="post">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Name</span>
<input name="name" class="form-control" placeholder="Katie" type="text" value="<? echo addslashes($_POST['name']); ?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Email</span>
<!-- email panel -->
<input name="email" class="form-control" placeholder="Katie#katie.com" type="email" value="<? echo addslashes($_POST['email']); ?>" />
</div>
<div class="input-group">
<span class="input-group-addon">DOB</span>
<!-- DOB panel -->
<input name="DOB" class="form-control" placeholder="dd/mm/yyyy" type="DOB" value="<? echo addslashes($_POST['DOB']); ?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Town/City</span>
<!-- Place panel -->
<input name="Town" class="form-control" placeholder="New York" type="town" value="<? echo addslashes($_POST['town']); ?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Country</span>
<!-- country panel -->
<input name="country" class="form-control" placeholder="America" type="country" value="<? echo addslashes($_POST['country']); ?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Password</span>
<!-- password panel -->
<input name="password" class="form-control" placeholder="At least 8 characters and 1 cap letter" type="password" value="<? echo addslashes($_POST['password']); ?>" />
</div>
<input type="submit" name="submit" value="Sign Up" class="btn btn-success btn-lg marginTop"/>
</form>
The line in the php doc that encrupts the password and adds it along with the email to mysql is this. This works fine, what I am struggling with is adding Name, DOB, Town and Country.
$query = "INSERT INTO `users` (`email`, `password`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."', '".md5(md5($_POST['email']).$_POST['password'])."')";
I thought i could simply add these to this line of code but i get errors when I do. For example like this with adding name to it.
$query = "INSERT INTO `users` (`email`, `password`, `name`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."', '".md5(md5($_POST['email']).$_POST['password']).$_POST['name'])."')";
Any help would be very much appreciated, like I said I am new to this so be gentle on me.
Thanks in advance.
Do like below:
$query = "INSERT INTO `users` (`email`, `password`, `name`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."', '".md5(md5($_POST['email']).$_POST['password']))."','".$_POST['name']."')";
As #Epodax stated, instead of separating your values (password and name) you are concatenating them.
$query = "INSERT INTO `users` (`email`, `password`, `name`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."', '".md5(md5($_POST['email']).$_POST['password']).$_POST['name']."')";
Should be...
$query = "INSERT INTO `users` (`email`, `password`, `name`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."', '".md5(md5($_POST['email']).$_POST['password']). "', '" . $_POST['name']."')";
try this:
$query = "INSERT INTO `users` (`email`, `password`, `name`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."', '".md5(md5($_POST['email']).$_POST['password']))."','".$_POST['name']."')";

Categories