(isset($_POST['submitted'])) doesn't seem to work - php

I can't figure out what I'm doing wrong here. I've been watching different videos on this problem, and been searching for answers for a long time.
When I hit the submit button on the web page, the fields get cleared and nothing else happens. There is probably a simple mistake, but I can't find out what I'm doing wrong
<a1>
<h1>Registrer ny bruker</h1>
<hr>
<form method="post" action="" >
<input type="text" name="surName" placeholder="Etternavn" required>
<br><br>
<input type="text" name="givenName" placeholder="Fornavn" required>
<br><br>
<select name="gender">
<option value="male">male</option>
<option value="female">female</option>
</select>
<br><br>
<input type="number" name="age" placeholder="Alder" required>
<br><br>
<input type="text" name="mail" placeholder="e-post" required>
<br><br>
<input type="text" name="userName" placeholder="Brukernavn" required>
<br><br>
<input type="text" name="password" placeholder="Passord" required>
<br><br>
<input type="submit" name="submitted" value="Registrer">
</form>
</a1>
<?php
if(isset($_POST['submitted'])){
$mysqli = new mysqli('my database username and password');
$email = $mysqli->real_escape_string($_POST['mail']);
$surName = $mysqli->real_escape_string($_POST['surName']);
$givenName = $mysqli->real_escape_string($_POST['givenName']);
$username = $mysqli->real_escape_string($_POST['userName']);
$password = md5($_POST['password']);
$gender = $mysqli->real_escape_string($_POST['gender']);
$age = $mysqli->real_escape_string($_POST['age']);
$sql = "INSERT INTO blog_user (first_name, last_name, age, gender, user_password, user_name, mail)
VALUES ('$givenName','$surName','$age','$gender','$password','$username','$email')
";
$insert = $mysqli->query($sql);
if ($insert) {
echo "Succes!";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
}
?>
Edit : I made a small typo when I first added this, so the first answers did not fix my problem. I did not give them a down vote, and I'm sorry I made a typo.

<?php
if(isset($_POST['submitted'])){
$mysqli = new mysqli('my database username and password');
$email = $mysqli->real_escape_string($_POST['mail']);
$surName = $mysqli->real_escape_string($_POST['surName']);
$givenName = $mysqli->real_escape_string($_POST['givenName']);
$username = $mysqli->real_escape_string($_POST['userName']);
$password = md5($_POST['password']);
$gender = $mysqli->real_escape_string($_POST['gender']);
$age = $mysqli->real_escape_string($_POST['age']);
$sql = "INSERT INTO blog_user (first_name, last_name, age, gender, user_password, user_name, mail)
VALUES ('$givenName','$surName','$age','$gender','$password','$username','$email')
";
$insert = $mysqli->query($sql);
if ($insert) {
echo "Succes!";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
}
?>
you have missed if statement.

Your code is "clearing the fields" because your form is always presented first with default values. The default values are not given with the input fields, so it's therefore "cleared" everytime you execute your php-file.
<form method="post" action="" >
<input type="text" name="surName" placeholder="Etternavn" required>
<br><br>
<input type="text" name="givenName" placeholder="Fornavn" required>
<br><br>
<select name="gender">
<option value="male">male</option>
<option value="female">female</option>
</select>
<br><br>
<input type="number" name="age" placeholder="Alder" required>
<br><br>
<input type="text" name="mail" placeholder="e-post" required>
<br><br>
<input type="text" name="userName" placeholder="Brukernavn" required>
<br><br>
<input type="text" name="password" placeholder="Passord" required>
<br><br>
<input type="submit" name="submitted" value="Registrer">
</form>
For this reason you should use the form-request-part before you show the form
if(isset($_POST['submitted'])){
$mysqli = new mysqli('my database username and password');
$email = $mysqli->real_escape_string($_POST['mail']);
$surName = $mysqli->real_escape_string($_POST['surName']);
$givenName = $mysqli->real_escape_string($_POST['givenName']);
$username = $mysqli->real_escape_string($_POST['userName']);
$password = md5($_POST['password']);
$gender = $mysqli->real_escape_string($_POST['gender']);
$age = $mysqli->real_escape_string($_POST['age']);
$sql = "INSERT INTO blog_user (first_name, last_name, age, gender, user_password, user_name, mail)
VALUES ('$givenName','$surName','$age','$gender','$password','$username','$email')
";
$insert = $mysqli->query($sql);
if ($insert) {
echo "Succes!";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
}
and show default values something like this...
<form method="post" action="" >
<input type="text" name="surName" placeholder="Etternavn"
required value="<?php echo $surName;?>">
<br><br>
<input type="text" name="givenName" placeholder="Fornavn"
required value="<?php echo $givenName;?>">
etc...
NOTE Database-query should be using prepared statements to avoid sql
injections.

Try this instead...
<?php
if(isset($_POST['submitted'])){

Related

I can't get my php data base to work correctly

So I'm trying to link my html to my php database but whenever I open up my browser to 'localhost/cs/staff/sign_up.php'only the message 'Firstname should not be empty' comes up.
How do I fix this?
----My 'sign_up.html' code----
<body>
<form method="post" action="../sign_up.php" style="border:1px solid #ccc">
<div class="container">
<h1>Sign Up</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<label for="Firstname"><b>Firstname</b></label>
<input type="text" placeholder="Enter Firstname" name="firstname" required>
<label for="Lastname"><b>Email</b></label>
<input type="text" placeholder="Enter Lastname" name="lastname" required>
<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>
<label for="psw-repeat"><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="psw-repeat" required>
<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">
<button type="button" class="cancelbtn">Cancel</button>
<button type="submit" class="signupbtn">Sign Up</button>
</div>
</div>
</form>
</body>
---My 'sign_up.php' code---
<?php
$f_name = filter_input(INPUT_POST, 'firstname');
$l_name = filter_input(INPUT_POST, 'lastname');
$email = filter_input(INPUT_POST, 'email');
$password = filter_input(INPUT_POST, 'psw');
if (!empty($f_name)){
if (!empty($l_name)){
if (!empty($email)){
if (!empty($password)){
$DB_SERVER = "localhost";
$DB_USERNAME = "root";
$DB_PASSWORD = "";
$DB_NAME = "project";
// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_errno() .') '
. mysqli_connect_error());
}
else{
$sql = "INSERT INTO account (firstname, lastname, email, password)
values ('$firstrname', '$lastname','$email', '$password')";
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
}
else{
echo "Error: ". $sql ."
". $conn->error;
}
$conn->close();
}
}
else{
echo "Password should not be empty";
die();
}
}
else{
echo "Email should not be empty";
die();
}
}
else{
echo "Lastname should not be empty";
die();
}
}
else{
echo "Firstname should not be empty";
die();
}
?>
I'm still new at coding so sorry if the error is something really simple and noobish.
You are saving the $_POST values of the variable firstname on $f_name and lastname on $l_name
This is your code.
$f_name = filter_input(INPUT_POST, 'firstname');
$l_name = filter_input(INPUT_POST, 'lastname');
You have to update your sql, to match the variable names.
$sql = "INSERT INTO account (firstname, lastname, email, password)
values ('$f_name', '$l_name','$email', '$password')";

Form submit is not entering data into database

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="duration" placeholder="Enter duration">
<input type="text" name="budget" placeholder="Enter Budget">
<input type="text" name="keyskills" placeholder="Enter Skills">
<input type="text" name="jobdescription" placeholder="Enter Job Description">
<input type="text" name="edate" placeholder="Click to enter expiry date">
<input type="text" name="cdexmin" placeholder="Enter Minimum Experience">
<input type="text" name="cdexmax" placeholder="Enter Maximum Experience">
<input type="submit">
</form>
<?php
if(isset($_POST['submit'])) {
try {
// Establish server connection and select database
$username = $_SESSION['username'];
$stmt = $db->prepare("SELECT * FROM employer INNER JOIN company ON employer.cid = company.cid WHERE employer.username='$username' ");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$cid=$row['cid'];
$eid = $row['eid'];
$duration = $_POST['duration'];
$budget = $_POST['budget'];
$keyskills = $_POST['keyskills'];
$jobdescription = $_POST['jobdescription'];
$edate = $_POST['edate'];
$cdexmin = $_POST['cdexmin'];
$cdexmax = $_POST['cdexmax'];
$stmt = $db->prepare("INSERT INTO job(cid,eid,duration,budget,keyskills,jdesc,edate,cdexmin,cdexmax) values('$cid','$eid','$duration','$budget','$keyskills','$jobdescription','$edate','$cdexmin','$cdexmax') ");
$stmt->execute();
echo "JOB POSTED SUCCESSFULLY";
} catch(PDOException $e) {
echo "Error occurs:". $e->getMessage();
}
}
?>
Here is my code that I just created for a sample form that is trying to insert the values into database. My problem is that the values are not entering the database.
Why is it not working? Is there an syntax error I can't find?
Page parsing is easily done and it's not showing any errors but values are not entering the database.
You are using $_POST['submit'] but there is no any input with this name.
Add name to your input type submit as follow
<input type="submit" name="submit">
Change
<input type="submit">
to
<input type="submit" name="submit">
You have change in code. Add NAME attribute for POST form.
<input type="submit" name="submit">

Php form does not post all fields to MySql

I checked the answers under ( PHP Form not posting all fields 2 ) and I do have names in all form fields : .... However, It is only posting the id, the date, and the name... I am so confused.
Here is my Sign-up -html- :
<legend>Registration Form</legend><p></p>
<label> Name </label>
<input id="intext" type="text" name="name" /><p></p>
<label> Email </label>
<input id="intext" type="text" name="email" /><p></p>
<label> Zip_Code </label>
<input id="intext" type="text" name="zipcode" /><p></p>
<label> UserName </label>
<input id="intext" type="text" name="user" /><p></p>
<label> Password </label>
<input type="password" name="pass" /><p></p>
<label> Confirm Password </label>
<input type="password" name="cpass" /><p> </p>
<div class="center">Comments / Inquiry </div>
<div class="center">
<textarea id="textarea" name="comments" rows="10" cols="40"></textarea>
Here is my php function:
function NewUser() { $name = $_POST['name'];
$Name = $_POST['name'];
$Email = $_POST['email'];
$Zip_Code = $_POST['zipcode'];
$UserName = $_POST['user'];
$Password = $_POST['pass'];
$Comments = $_POST['comments'];
$query = "INSERT INTO WebsiteUsers (Name, Email, Zip_Code, UserName, Password, Comments)
VALUES ('$name','$email','$zipcode','$user','$pass','$comments')";
$data = mysql_query ($query)or die(mysql_error());
if($data) { echo "Thank you for Registering with us.";
}
}
function SignUp() { if(!empty($_POST['user'])) //checking the 'user' name which is from Sign-Up.html, is it empty or have some text
{
$query = mysql_query("SELECT * FROM WebsiteUsers WHERE UserName = '$_POST[user]' AND Password = '$_POST[pass]'") or die(mysql_error());
if(!$row = mysql_fetch_array($query) or die(mysql_error())) { newuser();
}
In your query are usign variables in lowercase but in variable declarations are with uppercase.
Can you try this code?
$query = "INSERT INTO WebsiteUsers (Name, Email, Zip_Code, UserName, Password, Comments)
VALUES ('$Name','$Email','$Zipcode','$User','$Pass','$Comments')";
please try this.
I am assuming that you do not have issue implementing $conn, the connection to database, selecting db
sign-up.php
<form action="script.php" method="POST">
<legend>Registration Form</legend>
<label> Name </label>
<input id="intext" type="text" name="name" />
<label> Email </label>
<input id="intext" type="text" name="email" />
<label> Zip_Code </label>
<input id="intext" type="text" name="zipcode" />
<label> UserName </label>
<input id="intext" type="text" name="user" />
<label> Password </label>
<input type="password" name="pass" />
<label> Confirm Password </label>
<input type="password" name="cpass" />
<div class="center"><p>Comments / Inquiry </p>
<textarea id="textarea" name="comments" rows="10" cols="40"></textarea>
</div>
</form>
script.php
<?php
function newUser($conn)
{
$name = $_POST['name'];
$email = $_POST['email'];
$zipcode = $_POST['zipcode'];
$username = $_POST['user'];
$password = $_POST['pass'];
$password2 = $_POST['cpass'];
$comments = $_POST['comments'];
if($password== $password2)
{
$query = "INSERT INTO WebsiteUsers VALUES ('".$name."','".$email."','".$zipcode."','".$username."','".$password."','".$comments."')";
if(mysql_query($query,$conn))
echo 'signup successful';
else
echo 'error inserting new user';
}
else
echo 'Password missmatched';
}
function signUp($conn)
{
if(!empty($_POST['user']))
{
$username = $_POST['user'];
$password = $_POST['pass'];
$query = "SELECT * FROM WebsiteUsers WHERE UserName = '".$username."' AND Password = '".$password."';";
$result = mysql_query($query,$conn);
if(mysql_num_rows($result)<1)
newUser($conn);
}
else
echo 'form not submitted';
}
// now calling the signUp()
$conn= mysql_connect("","","") or die("Error connecting database"); // host, user, pass to connect db
mysql_select_db(""); // select database
signUp($conn);
?>
This is just cleanup of your code. Hope this will help to solve your problem. My implementation would be completely different than this one.
And one more thing, please use mysqli_* or PDO as mysql_* is depreciated

PHP Registration Form not saving

I have a HTML Sign Up form that allows new users to be registered to the site.:
<form action="register.php" method="POST" class="register-form">
<h1>Create Account</h1>
<label>
<span>First Name :</span>
<input id="firstname" type="text" name="firstname" placeholder="Your First Name" autocomplete="off" required/>
</label>
<label>
<span>Surname :</span>
<input id="surname" type="text" name="surname" placeholder="Your Surname" autocomplete="off" required/>
</label>
<label>
<span>Username :</span>
<input id="username" type="text" name="username" placeholder="Your Chosen Username" autocomplete="off" required/>
</label>
<label>
<span>Email :</span>
<input id="email" type="email" name="email" placeholder="Your Email Address" autocomplete="off" required/>
</label>
<label>
<span>Password :</span>
<input id="password" type="password" name="password" placeholder="Your Chosen Password" autocomplete="off" required/>
</label>
<hr>
<input name="action" type="hidden" value="signup" />
<input type="submit" class="btn register btn-success btn-lg" name="submit" value="Register">
</form>
Which goes to register.php:
<?php
$connection = mysql_connect('localhost', 'root', 'password');
if (!$connection) {
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('gmaps1');
if (!$select_db) {
die("Database Selection Failed" . mysql_error());
}
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])) {
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$query = "INSERT INTO `users` (firstname, surname, username, password, email) VALUES ('$firstname', '$surname', '$username', '$password', '$email')";
$result = mysql_query($query);
if ($result) {
header("Location: thank_you.html");
} else {
echo 'User Not Created';
}
}
?>
But when I click the Register button it doesn't save the data and returns "User Not Created". Would it be better using MySQLi rather than MySQL or is there a better way for this to work??
Error checking solved my problem - "field 'active' doesn't have a default value"
There was an inactive field in the table 'Users'. I got rid of that and it works fine. It must have been added in by mistake.
You don't get a "Database Connection Failed" so you sucessfully connected with the database
Its better to use MySQLi or PDO (my choice)
At least use mysql_real_escape and maybe a trim() but thats just a starting point when it comes to security
check wether all your database fields are named exactly the way you are adressing them inside your Insert-Statement
Do a echo $query, open phpMyAdmin (for example) and copy-paste the output inside the SQL field and send -> you may get a MySQL error you can analyse
It's better to store passwords hashed. Try inserting MD5($password) (there are way better options!) and on login do compare:
if(MD5($inputPassword) == $passwordhashFromDatabase){}

Clear html text fields

I have an HTML form for submitting and retrieves data from MySQL database with two buttons, "Save/Submit" and "New/Reset"
It fetch data correctly from MySQL database but when I click on New/Reset button for new contact entry it couldn't clear forms text fields. My HTML and PHP codes are as under:
<?php
//Database Connection file.
include'connect.php';
$sql = mysql_query("SELECT * FROM contact_list WHERE id='1'");
While($result = mysql_fetch_assoc($sql)){
$fname = $result['fname'];
$lname = $result['lname'];
$email = $result['email'];
$contact = $result['contact'];
}
if(isset($_POST['fname'])&&isset($_POST['lname'])&&isset($_POST['email'])&&
isset($_POST['contact'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$contact = $_POST['contact'];
if($sql = mysql_query("INSERT INTO contact_list VALUES ('', '$fname', '$lname',
'$email', '$contact')")){
echo'Contact Save Successfully.';
}else{
echo'Contact not save.';
}
}
?>
<html>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>">
//Clean all fields of forms for new entry.
<input type="reset" value="New">
//Save or submit form data into mysql database
<input type="submit" value="Save">
</form>
</html>
You can do this easily by using jQuery
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$("#btnReset").click(function(){
$("#fname").val("");
$("#lname").val("");
$("#email").val("");
$("#contact").val("");
});
});
</script>
</head>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>" id="fname">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>" id="lname">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>" id="email">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>" id="contact">
//Clean all fields of forms for new entry.
<input type="reset" value="New" id="btnReset">
//Save or submit form data into mysql database
<input type="submit" value="Save" id="btnSave">
</form>
</html>

Categories