I've successfully been able to input data into my MySQL database but having trouble inserting the data when submitting the form. At the moment, blank data will insert whenever i load the register.php
page. I read that I need to possibly put this code in a separate document containing ONLY the php code. I tried that and added that page to the form action. It redirected to the proccess.php
page but didn't actually insert the data. here is my php:
<?php
$servername = "localhost";
$username = "root";
$password = "@Passw0rd";
$dbname = "accounts";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$name = mysqli_real_escape_string($link, $_REQUEST['Ausername']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);
$userPassword = mysqli_real_escape_string($link, $_REQUEST['Apassword']);
$sql = "INSERT INTO users (username, email, password)
VALUES ('$name', '$email', '$userPassword')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . mysqli_error($conn);
}
mysqli_close($conn);
?>
and my form:
<form class="form-inline" action="register.php" method="post">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" class="form-control" id="Ausername" placeholder="Username" name="Ausername" required>
</div>
<br> <br>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input type="text" class="form-control" id="email" placeholder="Email" name="email" required>
</div>
<br> <br>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input type="text" class="form-control" id="Apassword" placeholder="Password" name="Apassword" required>
</div>
<br> <br> <br>
<button type="submit" class="btn btn-primary" id="button" name="submit" value="submit">
<i class="fa fa-user-plus"></i> Sign Up
</button>
<br> <br>
</form>
use $_POST
instead of $_REQUEST
and try
$name = mysqli_real_escape_string($link, $_POST['Ausername']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$userPassword = mysqli_real_escape_string($link, $_POST['Apassword']);
your form action "POST", you should retrieve data using $_POST.
My condition of IF statement , all values are mandatory , you can edit that of your own .
<?php
$servername = "localhost";
$username = "root";
$password = "@Passw0rd";
$dbname = "accounts";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST["submit"]) && !empty($_POST["Ausername"] && !empty($_POST["email"] && !empty($_POST["Apassword"])) {
$name = mysqli_real_escape_string($link, $_POST['Ausername']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$userPassword = mysqli_real_escape_string($link, $_POST['Apassword']);
}else {
// redirect to form page
}
$sql = "INSERT INTO users (username, email, password)
VALUES ('$name', '$email', '$userPassword')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . mysqli_error($conn);
}
mysqli_close($conn);
?>
please first before keeping data from a form in variable first check if post is submitted and chek the codes below
<?php
if(isset($_POST['submit']){
$name = mysqli_real_escape_string($link, $_POST['Ausername']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$userPassword=mysqli_real_escape_string($link,$_POST['Apassword']);
$query="INSERT INTO users(username, email, password)
VALUES('$name','$email','$userPassword')";
if($query){
?>
<script>
alert("record added");
window.location="index.php";//where index.php is a page with your html codes
</script>
}
else
die(mysqli_error($link));
<?php
}
?>
or you can user unset($_POST);
after inserting data from data it it help please notify us thanks but rember to use $_POST instead of $_REQUEST
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.