MySQL and PHP Basic Form Echo - php

I am a beginner PHP coder. I want it to be if when they register for my php code, it echos "You have been registered", instead of just showing a blank page. This is my code:
<?php
require('config.php');
if(isset($_POST['submit'])){
//Preform the verification of the nation
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($email1 == $email2) {
if($pass1 == $pass2) {
//All good. Carry on.
$name = mysql_escape_string($_POST['name']);
$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string($_POST['email1']);
$email2 = mysql_escape_string($_POST['email2']);
$pass1 = mysql_escape_string($_POST['pass1']);
$pass2 = mysql_escape_string($_POST['pass2']);
$pass1 = md5($pass1);
$sql = mysql_query("SELECT * FROM `users` WHERE `uname` = '$uname'");
if(mysql_num_rows($sql) > 0) {
echo "Sorry, that user already exists!";
exit();
}
mysql_query("INSERT INTO `users` (`id`, `name`, `lname`, `uname`, `email`,
`pass`) VALUES (NULL, '$name', '$lname', '$uname', '$email1',
'$pass1')");
}else{
echo "Sorry, your passwords do not match<br><br>";
exit();
}
}else{
echo "Sorry, your emails do not match.<br><br>";
}
}else{
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /><br />
Last Name: <input type="text" name="lname" /><br />
Username: <input type="text" name="uname" /><br />
Email: <input type="text" name="email1" /><br />
Confirm Email: <input type="text" name="email2" /><br />
Password: <input type="password" name="pass1" /><br />
Confirm Password: <input type="password" name="pass2" /><br />
<input type="submit" value="Register" name="submit" />
</form>
EOT;
echo $form;
}
?>
As you can see, there is no echo for if everything works. Please help me add an echo if they're registrations gets registered!!

Just check the return of the insert query.
$result = mysql_query("INSERT ...");
if ($result) {
echo "Created!";
} else {
echo "Uh oh! Something went wrong!";
}

In your case - You could just type:
echo "Congratulations, You've been submitted";
or whatever you want to say just under the insert statement

before the last
}else{
in your code put
echo "You have signed up";
and that's it :)

Related

PHP login script T_SL error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I'm making a register page on 000webhost and I save the code, open the site up and I get this error : Parse error: syntax error, unexpected T_SL on line 40.
<?php
require('config.php');
if (isset($_POST['submit']))
{
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($email1 == $email2 && $pass1 == $pass2)
{
//All good
$name = mysql_escape_string($_POST['name']);
$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string($_POST['email1']);
$email2 = mysql_escape_string($_POST['email2']);
$pass1 = mysql_escape_string($_POST['pass1']);
$pass2 = mysql_escape_string($_POST['pass2']);
$pass1 = md5($pass1);
//Check if username is taken
$check = mysql_query("SELECT * FROM users WHERE uname = '$uname'")or die(mysql_error());
if (mysql_num_rows($check)>=1) echo "Username already taken";
//Put everyting in DB
else{
mysql_query("INSERT INTO `users` (`id`, `name`, `lname`, `uname`, `email`, `pass`) VALUES (NULL, '$name', '$lname', '$uname', '$email1', '$pass1')") or die(mysql_error());
echo "Registration Successful";
}
}
else{
echo "Sorry, your email's or your passwords do not match. <br />";
}
}
else{
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /><br />
Last Name: <input type="text" name="lname" /><br />
Username: <input type="text" name="uname" /><br />
Email: <input type="text" name="email1" /><br />
Confirm Email: <input type="text" name="email2" /><br />
Password: <input type="password" name="pass1" /><br />
Confirm Password: <input type="password" name="pass2" /><br />
<input type="submit" value="Register" name="submit" /> </form>
EOT;
echo $form;
}
?>
I have spent a bit attempting to solve the issue but I cant find the problem.
Exactly on line 40 $form = <<<EOT you cannot have anything after the heredoc name, in your case you have spaces after "EOT" $form = <<<EOT[space]

Im trying to create a sign up page. Getting blank screen

Im trying to create a sign up page. I followed a guide but its erroring and im presented with a blank screen when i run it. I think i may of misunderstood some code. any help would be greatly appreciated.
<?php
if (isset($_POST['register'])){
if (empty($_POST['email']) &&
(empty($_POST['username']) &&
(empty($_POST['password']) &&
(empty($_POST['re-enter']) &&
(empty($_POST['title']) &&
(empty($_POST['first name']) &&
(empty($_POST['second name']) &&
(empty($_POST['address']) &&
(empty($_POST['postcode']) &&
(empty($_POST['contactnumber'])){
$email=$_POST['email'];
$username=$_POST['username'];
$password=$_POST[['password'];
$reenter=$_POST['re-enter'];
$title=$_POST['title'];
$firstname=$_POST['firstname'];
$secondname=$_POST['secondname'];
$address=$_POST['address'];
$postcode=$_POST['postcode'];
$contactnum=$_POST['contactnumber'];
if ($password == $reenter) {
$conn = mysqli_connect('127.0.0.1', 'i7266***', 'Winter****', 'i72*****');
$emailquery = "SELECT * FROM UserTable WHERE email = '$email'";
$r = mysqli_query($conn, $emailquery);
$count = mysqli_num_rows($r);
if ($count == 1) {
echo "Email already exists";
} else {
$query = "INSERT INTO UserTable VALUES ('$username', '$password', '$email', '$title', '$firstname', '$secondname', '$address', '$postcode', '$contactnum')";
$run = mysqli_query($conn, $query);
echo "Customer account has been created";
}
} else {
echo "Passwords did not match";
}
} else {
echo "please enter your details to register";
}
}else {
echo "Please enter details to register";
}
?>
<form method="POST" action="">
Email:
<input type="email" name="email"><br>
Username:
<input type="text" name="username"><br>
Password:
<input type="password" name="password"><br>
Re-enter password:
<input type="password" name="re-enter"><br>
Title:
<input type="text" name="title"><br>
First name:
<input type="text" name="firstname"><br>
Second name:
<input type="text" name="secondname"><br>
Address
<input type="text" name="address"><br>
Postcode:
<input type="text" name="postcode"><br>
Contact number
<input type="number" name="contactnumber"><br>
<br>
<input type="submit" name="register" value="Register">
You are not closing the '( )' in the second if in all the evaluations and you have one more of this '[' in $password=$_POST[['password'];
Try this:
<?php
if (isset($_POST['register'])){
if ((empty($_POST['email'])) &&
(empty($_POST['username'])) &&
(empty($_POST['password'])) &&
(empty($_POST['re-enter'])) &&
(empty($_POST['title'])) &&
(empty($_POST['first name'])) &&
(empty($_POST['second name'])) &&
(empty($_POST['address'])) &&
(empty($_POST['postcode'])) &&
(empty($_POST['contactnumber']))){
$email=$_POST['email'];
$username=$_POST['username'];
$password=$_POST['password'];
$reenter=$_POST['re-enter'];
$title=$_POST['title'];
$firstname=$_POST['firstname'];
$secondname=$_POST['secondname'];
$address=$_POST['address'];
$postcode=$_POST['postcode'];
$contactnum=$_POST['contactnumber'];
if ($password == $reenter) {
$conn = mysqli_connect('127.0.0.1', 'i7266***', 'Winter****', 'i72*****');
$emailquery = "SELECT * FROM UserTable WHERE email = '$email'";
$r = mysqli_query($conn, $emailquery);
$count = mysqli_num_rows($r);
if ($count == 1) {
echo "Email already exists";
} else {
$query = "INSERT INTO UserTable VALUES ('$username', '$password', '$email', '$title', '$firstname', '$secondname', '$address', '$postcode', '$contactnum')";
$run = mysqli_query($conn, $query);
echo "Customer account has been created";
}
} else {
echo "Passwords did not match";
}
} else {
echo "please enter your details to register";
}
}else {
echo "Please enter details to register";
}
?>
<form method="POST" action="">
Email:
<input type="email" name="email"><br>
Username:
<input type="text" name="username"><br>
Password:
<input type="password" name="password"><br>
Re-enter password:
<input type="password" name="re-enter"><br>
Title:
<input type="text" name="title"><br>
First name:
<input type="text" name="firstname"><br>
Second name:
<input type="text" name="secondname"><br>
Address
<input type="text" name="address"><br>
Postcode:
<input type="text" name="postcode"><br>
Contact number
<input type="number" name="contactnumber"><br>
<br>
<input type="submit" name="register" value="Register">
Hope works for you.
The syntax are fine now but the logic is wrong, as #Niet the Dark Absol says: You are processing the form only if ALL of them ARE empty.

php form submission to mysql database

I have a registration form. In the database, the username and email are unique index. When the form submits and username or email are already present in the database, the values are not inserted. I want to notify the user that the values were not inserted. How can i do this?
HTML
<form action="register.php" method="post" id="reg" onsubmit='return validate();'>
Company Name:
<input type="text" class="inputs" name="name" id="name" /><br />
Email:
<input type="text" class="inputs" name="email" id="txtEmail" /><br />
User name:
<input type="text" class="inputs" name="uname" id="uname"/><br />
Password:
<input type="password" class="inputs" name="pass" id="pass1"/><br />
Conferm Password:
<input type="password" class="inputs" name="cpass" id="pass2"/><br /><br />
<input type="submit" value="Register" class="button" />
</form>
register.php:
include ("db.php");
if (isset($_POST['register'])) {
echo $name = ($_POST["name"]);
echo $email = ($_POST["email"]);
echo $uname = ($_POST["uname"]);
echo $password = ($_POST["pass"]);
mysqli_query($con,"INSERT INTO company_profile(user_name, password, company_name, email, phone, country, activation_string) VALUES ('$uname','$password','$name','$email','','','')");
}
*Sweet And Short *
First check that username or email is exist or not using select query if resulting is 0 (it means not exists), Insert query will run ahead
<?php
if($_POST['register']){
$uname = $_POST['uname'];
$email = $_POST['email'];
$name= $_POST['name'];
$pass= $_POST['pass'];
$result = mysqli_query($con, 'SELECT * from TABLE_NAME where email_id = "'.$email.'" or username = "'.$uname.'" ');
if(mysqli_num_rows($result) > 0){
echo "Username or email already exists.";
}else{
$query = mysqli_query($con , 'INSERT INTO TABLE_NAME (`email_id`, `username`,`name`,`pass`) VALUES("'.$email.'", "'.$email.'", "'.$uname.'","'.$name.'", "'.$pass.'")');
if($query){
echo "data are inserted successfully.";
}else{
echo "failed to insert data.";
}
}
}
?>
The query method would return true or false, depending on if the row has been inserted or not.
Try the following Code
include ("db.php");
if (isset($_POST['register']))
{
echo $name = ($_POST["name"]);
echo $email = ($_POST["email"]);
echo $uname = ($_POST["uname"]);
echo $password = ($_POST["pass"]);
$var = mysqli_query('SELECT * from company_profile where email_id = "'.$email.'" or username = "'.$uname.'" ');
$num = mysqli_num_rows($var);
if($num==0)
{
$result = INSERT INTO company_profile(user_name, password, company_name, email, phone, country, activation_string) VALUES ('$uname','$password','$name','$email','','','');
$res = mysqli_query($result);
if($res)
{
echo "Records Inserted Successfully!!";
}
else
{
echo "Records Inserted Failed!!";
}
}
else
{
echo "User with the Details Already exists!!"
}
}

PHP for member sign up issues on submit

I've been putting together my first attempt at anything PHP...and it doesnt work...
Basically when I click 'Submit' it just changed the window to display the php code, and submits nothing to mysql.
This is what I have.
<?php
require('config.php');
if(isset($_POST['submit'])){
//Perform the verification
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($email1 == $email2){
if($pass1 == $pass2){
//all good carry on
$name = mysql_escape_string($_POST['name']);
$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string($email1);
$email2 = mysql_escape_string($email2);
$pass1 = mysql_escape_string($pass1);
$pass2 = mysql_escape_string($pass2);
mysql_query("INSERT INTO `users` (`id`, `name`, `lname`, `uname`, `email`, `pass`) VALUES (NULL, '$name', '$lname', '$uname', '$email1', '$pass1')");
}else{
echo "Sorry, your passwords do not match.<br />";
exit();
}
}else{
echo "Sorry, your email addresses do not match.<br /><br />";
}else{
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /><br />
Last Name: <input type="text" name="lname" /><br />
Username: <input type="text" name="uname" /><br />
Email: <input type="text" name="email1" /><br />
Confirm Email: <input type="text" name="email2" /><br />
Password: <input type="text" name="pass1" /><br />
Confirm Password: <input type="text" name="pass2" /><br />
<input type="submit" value="Register" name="submit" />
</form>
EOT;
}
?>
I'll add that the only way I could see the form was to copy the 'form' into a separate html file.
Any help wold be great and if you need more info just let me know.
Thanks
If you can see php code in your browser, your web server isn't configured properly and so this has nothing to do with php itself.
Do not pass the id field as null, just leave it out. Assuming of course the id field is set to primary index and autoincrement, which it should be.
turns out it was simply because i had mismatched my mysql_connect details.
Thanksfor the hints guys! wouldnt have figured it out otherwise.

Simple PHP + MySQL Form Not Working

Alright, so recently I watched a tutorial and coded along with it in Notepad++. I am attempting a simple MYSQL login/register form, but when I login- it gives me the "Wrong U/P" error echo I wrote. It saves everything in the database as the md5 and stuff. Here is my codes.
register.php
<?php
require('config.php');
if(isset($_POST['submit'])){
//Preform the verification of the nation
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($email1 == $email2) {
if($pass1 == $pass2) {
//All good. Carry on.
$name = mysql_escape_string($_POST['name']);
$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string($_POST['email1']);
$email2 = mysql_escape_string($_POST['email2']);
$pass1 = mysql_escape_string($_POST['pass1']);
$pass2 = mysql_escape_string($_POST['pass2']);
$pass1 = md5($pass1);
$sql = mysql_query("SELECT * FROM `users` WHERE `uname` = '$uname'");
if(mysql_num_rows($sql) > 0) {
echo "Sorry, that user already exists!";
exit();
}
mysql_query("INSERT INTO `users` (`id`, `name`, `lname`, `uname`, `email`, `pass`) VALUES (NULL, '$name', '$lname', '$uname', '$email1', '$pass1')");
}else{
echo "Sorry, your passwords do not match<br><br>";
exit();
}
}else{
echo "Sorry, your emails do not match.<br><br>";
}
}else{
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /><br />
Last Name: <input type="text" name="lname" /><br />
Username: <input type="text" name="uname" /><br />
Email: <input type="text" name="email1" /><br />
Confirm Email: <input type="text" name="email2" /><br />
Password: <input type="password" name="pass1" /><br />
Confirm Password: <input type="password" name="pass2" /><br />
<input type="submit" value="Register" name="submit" />
</form>
EOT;
echo $form;
}
?>
login.php
<?php
require('config.php');
if(isset($_POST['submit'])){
$uname = mysql_real_escape_string($_POST['uname']);
$pass = mysql_real_escape_string($_POST['pass']);
$pass = md5($pass);
$sql = mysql_query("SELECET * FROM `users` where `uname` = '$uname' and `pass` = '$pass'");
if(mysql_num_rows($sql) > 0){
echo "You are now logged in.";
exit();
}else{
echo "Wrong U/P combination";
}
}else{
$form = <<<EOT
<form action="login.php" method="POST">
Username: <input tye="text" name="uname" /><br>
Password: <input type="password" name="pass" /><br>
<input type="submit" name="submit" value="Login" />
</form>
EOT;
echo "$form";
}
?>
and config.php
<?php
mysql_connect("localhost", "X", "X");
mysql_select_db("X");
?>
The config.php code is correct, but I am not giving away X.
As you can see, this code echos out an error for login.php if it's incorrect. It gives me that error even if it is correct. I used MD5 hash passes, so please help!
Firstly, you're using the ` tag in there - this should be ' .
You need to either interpolate or concatenate your variables; i.e; instead of
mysql_query("INSERT INTO `users` (`id`, `name`, `lname`, `uname`, `email`, `pass`) VALUES (NULL, '$name', '$lname', '$uname', '$email1', '$pass1')");
use;
mysql_query("INSERT INTO 'users' ('id', 'name', 'lname', 'uname', 'email', 'pass') VALUES (NULL, '{$name}', '{$lname}', '{$uname}', '{$email1}', '{$pass1}')");
Anyway, aside from some good practice, have a look at this line;
$sql = mysql_query("SELECET * FROM `users` where `uname` = '$uname' and `pass` = '$pass'");
Just a small typo ruining everything for you. Change SELECET to SELECT , and you should be good to go.
Best of luck!
Eoghan
you don't need the following lines:
$email2 = mysql_escape_string($_POST['email2']);
and
`$pass2 = mysql_escape_string($_POST['pass2']);`
2. run SELECET * FROM users in order to see that the user/pwd really made it to the DB
3. add echo "$uname $pass <br>"; to the login form to make sure that it passed correctly
The other two answers are correct, but you have a more fundamental issue with this: you are using the old, deprecated mysql_* functions. Those functions are an old, procedural interface to MySQL and don't support the modern features of that RDBMS. I suggest using mysqli or PDO for an OOP approach to database access.
If you are going to stick to this ancient code, you should at least use mysql_real_escape_string() instead of mysql_escape_string().

Categories