PHP form not submitting correctly - php

I have a form that displays user data based on a query. The form is meant to allow users to update their user account information. i.e., First name, Last name, Email address, etc.
account.php - form
<form class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
<label class="col-lg-3 control-label">First name:</label>
<div class="col-lg-8">
<input class="form-control" name="fName" type="text" placeholder="<?php echo $fName ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Last name:</label>
<div class="col-lg-8">
<input class="form-control" name="lName" type="text" placeholder="<?php echo $lName ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email:</label>
<div class="col-lg-8">
<input class="form-control" name="emailAddress" type="text" placeholder="<?php echo $emailAddress ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Username:</label>
<div class="col-md-8">
<input class="form-control" name="username" type="text" placeholder="<?php echo $username ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Password:</label>
<div class="col-md-8">
<input class="form-control" name="password" type="password">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Confirm password:</label>
<div class="col-md-8">
<input class="form-control" name="confirmPassword" type="password">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Update" name="updateaccount" type="button">
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
updateaccount.php
<?php
$msg = "";
if(isset($_POST["updateaccount"]))
{
$fName = $_POST["fName"];
$lName = $_POST["lName"];
$username = $_POST["username"];
$emailAddress = $_POST["emailAddress"];
$password = $_POST["password"];
$fName = mysqli_real_escape_string($db, $fName);
$lName = mysqli_real_escape_string($db, $lName);
$username = mysqli_real_escape_string($db, $username);
$emailAddress = mysqli_real_escape_string($db, $emailAddress);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$sql="SELECT emailAddress FROM users WHERE emailAddress='$emailAddress'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result) == 1)
{
$msg = "Sorry...This email already exists";
}
else
{
$query = mysqli_query($db, "INSERT INTO users (fName, lName, username, emailAddress, password)VALUES ('$fName', '$lName', '$username', '$emailAddress, '$password')");
if($query)
{
$msg = "Your account has been updated";
}
}
}
?>
The above code is called on the account.php page:
include ("getuseraccount.php");
Did I miss something?

You are using input type button instead of submit for the Update button. Use 'submit' type instead of 'button' type.
<input class="btn btn-primary" value="Update" name="updateaccount" type="button">
try
<input class="btn btn-primary" value="Update" name="updateaccount" type="submit">

Unless you're using Ajax to submit the form (which you made no mention of, you need to add the file that you are submitting the form information to.
<form class="form-horizontal" role="form" method="post" action="updateaccount.php">

Related

How to make my update query work using my code below

I am setting up an update profile page using php and a bit of laravel. I have an if check that checks if the form is submitted and I have an update query.
The update query only works outside of the if statement and I have no idea why. If I put in inside of the if statement it doesnt do anything. I have checked if all the names are the same as in my sql database and they are all correct.
<form class="form" id="registrationForm">
<div class="form-group">
<div class="col-xs-6">
<label for="first_name">
<h4>Voornaam</h4></label>
<input type="text" class="form-control" name="voornaam" id="first_name" placeholder="<?= $user->voornaam; ?>" title="enter your first name if any.">
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<label for="last_name">
<h4>Achternaam</h4></label>
<input type="text" class="form-control" name="achternaam" id="last_name" placeholder="<?= $user->achternaam; ?>" title="enter your last name if any.">
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<label for="phone">
<h4>Gebruikersnaam</h4></label>
<input type="text" class="form-control" name="gebruikersnaam" id="username" placeholder="<?= $user->gebruikersnaam; ?>" title="enter your phone number if any.">
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<label for="mobile">
<h4>Telefoon nummer</h4></label>
<input type="text" class="form-control" name="telefoonnummer" id="mobile" placeholder="<?= $user->telefoonnummer; ?>" title="enter your mobile number if any.">
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<label for="email">
<h4>Email</h4></label>
<input type="email" class="form-control" name="email" id="email" placeholder="<?= $user->email; ?>" title="enter your email.">
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<br>
<button class="btn btn-lg btn-success" type="submit" name="submit"><i class="glyphicon glyphicon-ok-sign"></i> Sla op</button>
<button class="btn btn-lg" type="reset" name="resetww"><i class="glyphicon glyphicon-repeat"></i> Verander wachtwoord</button>
</div>
</div>
</form>
<?php
if (isset($_POST['submit'])) {
$voornaam = $_POST['voornaam'];
$achternaam = $_POST['achternaam'];
$gebruikersnaam = $_POST['gebruikersnaam'];
$telefoonnummer = $_POST['telefoonnummer'];
$email = $_POST['email'];
App\User::where('klant_id', 1)->update(['gebruikersnaam' => $gebruikersnaam, 'voornaam' => $voornaam, 'achternaam' => $achternaam, 'email' => $email ,'telefoonnummer' => $telefoonnummer]);
}
?>
I want my database to be updated
Put the tag in your form, the PHP is trying to find de POST but you are not sending it.
method="POST"
First of all mention form method as below and use csrf token
#csrf
Why u can't use controller to complete this work
First of all use controller, Then make a method to update information
public function update_user(Request $request){
if ($request->isMethod('post')) {
$data = $request->all();
User = new User();
$voornaam = $_POST['voornaam'];
$achternaam = $data['achternaam'];
$gebruikersnaam = $data['gebruikersnaam'];
$telefoonnummer = $data['telefoonnummer'];
$email = $data['email'];enter code here
}
}

isset function not working correctly

I am trying to insert records into a table but I am facing a problem on the isset function. I am unable to process the code.
In the following code the else statement is executed instead of the isset function.
<?php
if (isset($_POST['submit'])) {
include_once 'dbh.inc.php';
$firstname = mysqli_real_escape_string($conn, $_POST['firstname']);
$surname = mysqli_real_escape_string($conn, $_POST['surname']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$cpassword = mysqli_real_escape_string($conn, $_POST['cpassword']);
$bdate = mysqli_real_escape_string($conn, $_POST['bdate']);
$gender = mysqli_real_escape_string($conn, $_POST['gender']);
if(empty($firstname) || empty($surname) || empty($email) || empty($username) || empty($password) || empty($cpassword) || empty($bdate) || empty($gender)) {
header("Location: ../index.php?index=empty");
exit();
} else {
$sql = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
header("Location: ../index.php?index=username already taken");
exit();
} else {
// Hashing the password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
//Insert users into the database
$sql = "INSERT INTO users (id, firstname, surname, email, username, password, bdate, gender) VALUES ('', '$firstname', '$surname', '$email', '$username', '$password', '$bdate', '$gender');";
mysqli_query($conn, $sql);
header("Location: ../profile.php");
exit();
}
}
} else {
header("Location: ../index.php?index=error");
exit();
}
Here is the front-end containing the "sign up" part:
<div class="container and">
<div class="row">
<div class="col-lg-6 pull-right">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Create a new account.</h3>
</div>
<div class="panel-body">
<form id="loginform" action="includes/signup.inc.php" method="POST">
<div class="row">
<div class="form-group has-error col-md-6">
<input type="text" class="form-control" name="firstname" placeholder="First Name">
</div>
<div class="form-group has-error col-md-6">
<input type="text" class="form-control" name="surname" placeholder="Surname">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-12">
<input type="text" class="form-control" name="email" placeholder="Email Address">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-12">
<input type="text" class="form-control" name="username" placeholder="Username">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-12 padding-top-10">
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-12">
<input type="password" class="form-control" name="cpassword" placeholder="Confirm Password">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-12">
<h4> Birthday</h4>
<input type="date" class="form-control" name="bdate" value="birthdate">
</div>
</div>
<div class="row padding-top-10">
<div class="form-group has-error col-md-6 padding-top-10">
<div class="pull-right">
Male: <input type="radio" name="gender" value="male" />
</div>
</div>
<div class=" form-group has-error col-md-6 padding-top-10">
Female: <input type="radio" name="gender" value="female">
</div>
</div>
<div class="padding-top-10">
<button class="btn btn-success" name="submit">Create Account</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
You missed the button type attribute which will be type="submit" If you didn't include this type attribute, the form will not submit data.
Your code:
<button class="btn btn-success" name="submit">Create Account</button>
It should be:
<button type="submit" class="btn btn-success" name="submit">Create Account</button>
If, this is not your problem, let me know! Thanks.

Storing the values into database using php forms

Final Edit:
Thank you everyone for the help. I have been trying to write all the code related to connection in index.php rather than submit.php. It is resolved now.
Edit:
I have updated the code based on your feedback.
I am able to get the values to the database now but the thing is it is showing only empty results. here is the updated code.
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" name="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="subject" name="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
PHP Code:
<?php
if (isset($_POST)) {
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
die("Database connection failed: " . mysqli_error($conn));
}
else
echo "connected successfully";
//Escaping string, not 100% safe, also consider validating rules and sanitization
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$subject = mysqli_real_escape_string($conn, $_POST['subject']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')");
}
?>
Here is the snapshot of the database
I have a form made using HTML. I want to store the results when i submit the form in the database. The connection was successful but the data is not being stored in the database.
Basically what submit.php does is just sent the text "Successfully submited the form".
Here's my code:
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
PHP code:
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
die("Database connection failed: " . mysqli_error($conn));
}
else
echo "connected successfully";
if (isset($_POST['submit'])) {
//Escaping string, not 100% safe, also consider validating rules and sanitization
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$subject = mysqli_real_escape_string($conn, $_POST['subject']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message');");
if ($result) {
$message="successfully sent the query!!";
}
else
{$message="try again!!";}
}
?>
None of your input fields have a name="" attribute, including the button. So none of these fields will be sent in the $_POST array.
Add a name="" attribute like this to all the fields you want sent to PHP
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" name="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
</div>
</div>
</div>
<div class="col-lg-1"></div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="subject" name="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1"></div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" name="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1"></div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" class="btn name="submit" btn-primary">Submit</button>
</div>
</div>
</form>
Also in your code in submit.php change this so you see an actual error message if one occurs.
if ($result) {
$message="successfully sent the query!!";
} else {
$message="Insert failed : " . mysqli_error($conn);
}
echo $message;
Although this does assume you are actually showing the $message value somewhere in your code that you have not shown us.
You have to add name attribute to your button element so that if (isset($_POST['submit'])) will be true.
Please change
<button type="submit" class="btn btn-primary">Submit</button>
to
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
or
<input type="submit" name="submit" value="Submit" class="btn btn-primary" />
First of all you must need to provide name attribute for each input tags and button tags for a better approach :
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" name ="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" name ="email" id ="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" name ="subject" id ="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" name ="message" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" name ="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Php Code for Insert data in DB :
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('".$name."', '".$email."', '".$subject."', '".$message."')");
Try this code :-
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
die("Database connection failed: " . mysqli_error($conn));
}
else
echo "connected successfully";
//Escaping string, not 100% safe, also consider validating rules and sanitization
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$subject = mysqli_real_escape_string($conn, $_POST['subject']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')");
echo "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')";//die;
if ($result) {
$message="successfully sent the query!!";
}
else
{$message="try again!!";}
}
?>
<form action="index.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" name ="name" id ="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" name="subject" class="form-control" id ="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</div>
</div>
</form>

Undefined index when passing variable to another page from a form

I'm trying to pass a value from one page to the other but I can't for the life of me understand why I keep getting the undefined index error message.
This is the PHP code to the first page:
<?php
include 'database_connection.php';
$id = $_GET['id'];
$sql="select engineer.id, engineer.team_id, engineer.first_name, engineer.active, engineer.last_name, engineer.role, engineer.region, engineer.phone, to_date, team.team_name, team.manager_name, team.description, team.type, engineer.email from engineer inner join team on engineer.team_id=team.id where active=0 and engineer.team_id > 0 and engineer.id = '".$id."'";
$results = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($results)) {
$id=$row['id'];
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$role=$row['role'];
$email=$row['email'];
$phone=$row['phone'];
$region=$row['region'];
$type=$row['type'];
?>
<form class="form-horizontal col-sm-12" role="form" method="post" action="../admin/update.php">
<fieldset disabled>
<div class="form-group">
<label for="sso" class="col-sm-2 control-label">SSO ID</label>
<div class="col-sm-10">
<input type="text" id="id" name="id" class="form-control" value="<?php echo $id; ?>">
</div>
</div>
</fieldset>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="first_name" placeholder="First Name" value="<?php echo $first_name; ?>">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">Surname</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="last_name" placeholder="Last Name" value="<?php echo $last_name; ?>">
</div>
</div>
<div class="form-group">
<label for="team" class="col-sm-2 control-label">Team</label>
<div class="col-sm-10">
<select style="width:auto;" class="btn btn-default dropdown-toggle form-control" type="button" name="team_name" value="" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<?php
//connect to the database
include 'database_connection.php';
//query the database
$sql_team = "SELECT DISTINCT id, team_name FROM team";
//uses $sql_team variable to make a specific query
$query = mysqli_query($connection, $sql_team);
?>
<option value="<?php echo $team_name; ?>"><?php echo $team_name; ?></option>
<?php
//initilises a while loop to retrieve all the rows
while ($row = mysqli_fetch_array($query) )
{
//echos all the distinct catDesc rows into a list
echo "<option value='" . $row['id'] . "' >".htmlspecialchars($row["team_name"])."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="role" class="col-sm-2 control-label">Role</label>
<div class="col-sm-10">
<input type="role" class="form-control" id="role" name="role" placeholder="Role" value="<?php echo $role; ?>">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo $email; ?>">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">Phone Number</label>
<div class="col-sm-10">
<input type="phone" class="form-control" id="phone" name="phone" placeholder="Business number" value="<?php echo $phone; ?>">
</div>
</div>
<div class="form-group">
<label for="region" class="col-sm-2 control-label">Region</label>
<div class="col-sm-10">
<input type="region" class="form-control" id="region" name="region" placeholder="Region e.g. South" value="<?php echo $region; ?>">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" value="Save" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
<?php
//closes and stops the loop
}
?>
This is the code to the second page, where the error message appears:
<?php
include 'database_connection.php';
$id = mysqli_real_escape_string($connection, $_GET['id']);
$first_name = mysqli_real_escape_string($connection, $_POST['first_name']);
$last_name = mysqli_real_escape_string($connection, $_POST['last_name']);
$team_name = mysqli_real_escape_string($connection, $_POST['team_name']);
$role = mysqli_real_escape_string($connection, $_POST['role']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$phone = mysqli_real_escape_string($connection, $_POST['phone']);
$region = mysqli_real_escape_string($connection, $_POST['region']);
if ($role == ''){
$role = NULL;
}
if ($email == ''){
$email = NULL;
}
if ($phone == ''){
$phone = NULL;
}
if ($region == ''){
$region = NULL;
}
$sql = "UPDATE people SET id='$id', first_name='$first_name', last_name='$last_name', team_id='$team_name', role='$role', email='$email', phone=$phone', region='$region' where id='$id')";
?>
So the error appears when I try to retrieve the ID onto the second page from the first page. What am I missing?
if you want to pass a variable as get method you can pass it inside action attribute of form
eg: action="xyz.php?id="
on line 3 you have assigned a $_GET[] variable you cannot assign there since those values only sets after submitting form so if you want to remove error please script it like this
if(isset($GET['id']){
$id = $_GET['id'];
}
use an
<input id="id" type="hidden" value=$id>
in your form and read it as
$_POST['id']
in the second page

PHP code isn't submitting into mysql database

I'm trying to create a login for a website. So far whenever I post to my database doesn't show the submitted information, the only things which are posted is a hashed password.
<form method="post" action="submit.php">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password">
</div>
<div class="form-group">
<label for="pwd2">Re-Password</label>
<input type="password" class="form-control" id="pwd2">
</div>
<div class="form-group">
<input type="submit" class="form-control" id="submit">
</div>
</form>
To submit into this php block
<?php
$servername = "localhost";
$username = "root";
$password = "Password";
$dbname = "DBNAME";
$email = NULL;
$user = NULL;
$pass1 = NULL;
if (isset($_POST['email'])){
$email = $_POST['email'];
}
if (isset($_POST['username'])){
$user = $_POST['username'];
}
if (isset($_POST['password'])){
$pass1 = $_POST['password'];
}
$hash = password_hash($pass1, PASSWORD_BCRYPT);
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Users (email, username, password )
VALUES ('$email', '$user', '$hash')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Your form fields lack name attributes. Without them no values are sent your your script. This is easily testable by doing var_export($_POST).
<form method="post" action="submit.php">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="email" id="email">
</div>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" name="username" id="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<div class="form-group">
<label for="pwd2">Re-Password</label>
<input type="password" class="form-control" name="pwd2" id="pwd2">
</div>
<div class="form-group">
<input type="submit" class="form-control" id="submit">
</div>
</form>
FYI, you are wide open to SQL injections

Categories