Entering data onto php form, not populating MySQL table - php

Have a PHP form for a registration system:
<div class="col-md-6 login-right">
<h2> Register Here </h2>
<form action="registration.php" method="post">
<div class="form-group">
<label>Username</label>
<input type="text" name="user" class="form-control" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary"> Register</button>
</form>
</div>
And a registration.php created:
<?php
session_start();
$con = mysqli_connect('localhost','root', 'test');
mysqli_select_db($con, 'userregistration');
$name = $_POST['user'];
$pass = $_POST['password'];
$s = " select * from usertable where name = '$name'";
$result = mysqli_query($con, $s);
$num = mysqli_num_rows($result);
if($num == 1){
echo " Username Already Taken";
}else{
$reg = " insert into usertable(name , password) values ('$name' , $pass')";
mysqli_query($con, $reg);
echo" Registration Successful";
}
?>
Also have a MySQL database created with Database: userregistration »Table: usertable. And the MySQL not sure, quite new to this isn't being populated with the inputted data from the php. When the data is inputted into the php form it requests the registration.php page which works successfully but doesn't populate the table with the data inputted.

You should be actively checking that the connection is successful first, and then also checking that the query was successfully executed too in order to debug this further.
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (mysqli_query($conn, $reg)) {
echo "New record created successfully";
} else {
echo "Error: " . $reg . "" . mysqli_error($conn);
}
You should then also close the connection

Related

Login php script unable to call infromation from db

Good day.
So below i have a php script that is supposed to query my db and look for user details. the db is set up and the data is available in it. the issue here seems that once i click the submit button with my user entered details, it fails on the first if statement, to see if the email exists. i am not sure why.
But here is the submit form.
<form action = "submit2.php" method="Post" >
<div class="row form-group">
<div class="col-md-12">
<!-- <label for="email">Email</label> -->
<input type="text" id="email" name="email" class="form-control" placeholder="Your user name">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<!-- <label for="subject">Subject</label> -->
<input type="text" id="password" name="password" class="form-control" placeholder="Your Password">
</div>
</div>
<div class="form-group">
<input type="submit" value="Login" class="btn btn-primary">
</div></form>
and here is the submit2.php that is supposed to manipulate the data from the form and query the db.
<?php
session_start();
require_once('connect.php');
if(isset($_POST) & !empty($_POST)){
$useremail = mysqli_real_escape_string($connection,$_POST['email']);
$userpassword = mysqli_real_escape_string($connection, $_POST['password']);
if (empty($useremail) || empty($userpassword)){
header("Location: customerportal.php?login=empty");
exit();
}
else{
$sql = "SELECT * FROM 'USERS' where EMAIL ='$useremail';";
$emailresult = mysqli_query($connection, $sql);
$emailresultcheck = mysqli_num_rows($emailresult);
//check if email exists
if($emailresultcheck == 0){
header("Location: customerportal.php?login=invalidEmail");
}
else {
if($row = mysqli_fetch_assoc($emailresult)){
//dehash the password
$hashedPWDCheck = password_verify($userpassword,$row['ENCRYPTEDPWD']);
if($hashedPWDCheck == false){
header("Location: customerportal.php?login=passwordincorrect");
exit();
}
elseif($hashedPWDCheck == true){
$_SESSION['email'] = $email;
// header("Location: Landingpage.php");
echo "Success";
}
}
else{
header("Location: customerportal.php?login=invalid");
exit();
}
}
}
}
?>
The submit always fails else statement and returns the invalidEmail header location and i am not sure why. the Connection file is below.what am i missing?
<?php
$connection = mysqli_connect("localhost", "root", "");
if(!$connection){
echo "Failed to connect database" . die(mysqli_error($connection));;
}
$dbselect = mysqli_select_db($connection, "dhctest");
if(!$dbselect){
echo "Failed to Select database" . die(mysqli_error($connection));
}
?>
Change this
$sql = "SELECT * FROM 'USERS' where EMAIL = '$useremail';";
to this
$sql = "select * from users where email = $useremail";
Okay, so solved the issue, by running a var_dump() on everyone of my variables until i came across the error that was being outputted by my sql code.
On the line
$sql = "SELECT * FROM 'USERS' where EMAIL = '$useremail';";
I had to remove the '' and replace with ``.
And that seems to have solved the issue.
Thank you for everyone who assisted.

writing in a mysql table using POST & php

I'm trying to register the e-mail entered in my form by users in my SQL table. but I'm not receiving any errors and the data are not saved either!
<?php
echo "I was here !!!";
if(!empty($_POST['mail']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mailing";
echo "I was here !!!";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'INSERT INTO contact VALUES ("","'.$_POST['mail'].'")';
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
and my html code:
<div class="form">
<p>Rester notifié par toutes les nouveautés !</p>
<form method="post" action="index.php" class="mainform">
<div class="field"><input type="text" class="field" name="mail" /></div>
<div class="submit"><input class="submit" type="button" value="Envoyer" /></div>
</form>
</div>
can you tell me what's the problem ?
change your button type .because if you want to submit the data by form then button type should be submit like that
<input class="submit" type="submit" value="Envoyer" />
Check if there's a value for $_POST['mail']. Your condition didn't handle empty value for $_POST['mail']. If there is a value. Change your query
INSERT INTO contact(email) VALUES ("'.$_POST['mail'].'")
Try this. Since you only need to add an email. Hope it helps.

how to return values from mysql table with ajax and php

I am new to programing. I have one form to create Groups. It has two text fields Code Id and Code description. After submitting it showed me that the Code Id which i entered is already exist and if not it add one record in MySQL table. What I want that when I leave the Id text field at the same time with onchange event and Ajax to search table and alert if the Id already exit and at the same time fill name text box with description of that Code Id. How to do that? My code is
HTML file
...
</style>
<body>
<H1>Create Grup</h1>
<br>
<form action="creategrup.php" method="post">
<p>
<label for="codigo">Grup Id:</label>
<input type="text" required="required" autofocus="autofocus"
maxlength="4" name="codigo" id="codigo">
</p>
<p>
<label for="nombre">Grupo description:</label>
<input type="text" required="required" name="nombre" id="nombre"">
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>
And PHP is
<?php
include 'connectdb.php';
$nombre=$_POST['nombre'];
$codigo=$_POST['codigo'];
$sql = "select codigogrupo,nombregrupo from grupo where
codigogrupo='$codigo'";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) >0) {
echo "<p><h1><b>Grup Id $codigo allready exist....</h1></b></p><br>";
echo "<a href='creategrup.html'>Go Back</a>";
}
else {
mysqli_query($conn, "insert into grupo(codigogrupo,nombregrupo)
values('$codigo','$nombre')");
if(mysqli_affected_rows($conn)>0){
echo "<p><h1><b>Grup $nombre added</h1></b></p>";
echo "<a href='creategrup.html'>Go Back</a>";
} else {
echo "Grup not added<br>";
echo mysqli_error ($conn);
}
}
?>
Connectdb.php
<?php
$servername = "server name";
$username = "user name";
$password = "password";
$dbname = "database";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// echo "Connected successfully";
?>
Please help me.
You are getting already exist message so that may be confirm that there is another data with same id. For further assistance please knock me

issues with login and register page php

I have thoroughly researched my topic before coming here and can't seem to figure out my problem.
I have an HTML page:
<form role="form" action="register.php" method="POST">
<div class="form-group">
<label>First Name:</label>
<input type="text" name="first_name">
</div>
<div class="form-group">
<label>Last Name:</label>
<input type="text" name="last_name">
</div>
<div class="form-group">
<label>Student ID:</label>
<input type="number" name="student_id">
</div>
<div class="form-group">
<label>Email address:</label>
<input type="email" name="email">
</div>
<button type="submit" name="register" value="register">Register</button>
</form>
<form role="form" action="login.php" method="POST">
<div class="form-group">
<label>Email address:</label>
<input type="email" name="email">
</div>
<button type="submit" name="login" value="login">Login</button>
</form>
This functions and communicates perfectly well with my login page written in php, it checks if the submitted email address already exists in a MySQL database. It will then point the user to a profile page and the code exits itself.
My issue is with my register page, I use the same MySQL SELECT functions that I do on my login page, to check and see if the submitted student ID or email already exists in the database and if so, will return back to the form for the user to try again:
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$student_id = $_POST['student_id'];
$email = $_POST['email'];
if (isset($_POST['register'])) {
register($conn, $first_name, $last_name, $student_id, $email);
}
function register($conn, $first_name, $last_name, $student_id, $email) {
$Ssql = "SELECT student_id FROM AidenLocke where student_id = '$student_id'";
$Sresult = mysqli_query($conn, $sql);
if (mysqli_num_rows($Sresult) > 0) {
header('Location: form.html');
} else {
$sql = "INSERT INTO AidenLocke (first_name, last_name, email, student_id)
VALUES ('$first_name', '$last_name', '$email', '$student_id')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br />" . $conn->error;
}
header('Location: profile.php');
}
}
(I have removed my database information for security reasons but there is no connection problem)
My main issue is with the else section of the second if statement, my code does not check if the student id already exists, and regardless of what information I enter into the form, makes a new entry in to the database.
I am quite confused and hoping someone can give me a valid answer, thanks!
You seem to have a typo in your variable when you query the database:
$Ssql = "SELECT student_id FROM AidenLocke where student_id = '$student_id'";
^^^^
$Sresult = mysqli_query($conn, $sql);
^^^
That is, you're using $sql instead of $Ssql

updating a specific row from a table

I have a table that is being displayed on the user screen. When the user clicks on edit, I want him to be able to click the parameters of that specific row and to update a single parameter (e.g., name); then, the other parameters (e.g., email, password, and address) should remain the same.
name email password address action
user user#gmail.com user u.address edit
The code used for the edit link is:
echo"<td class='center'><a class='btn btn-info' href=\"admin_edit_user.php?id=".$row['id']."\"><i class='glyphicon glyphicon-edit icon-white'></i>Edit</a></td>";
Code on admin_edit_user.php page:
<form class="form-horizontal" role="form" action="admin_update_user.php" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="col-lg-3 control-label">Name</label>
<div class="col-lg-8">
<input class="form-control" name="name" value="" type="text">
</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="email" value="" type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Password</label>
<div class="col-lg-8">
<input class="form-control" name="password" value="" type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Address</label>
<div class="col-lg-8">
<input class="form-control" name="address" value="" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="submit">
<input class="btn btn-primary" value="Save Changes" type="submit" name="submit">
</div>
</div>
</form>
After this form the user gets redirected to admin_update_user.php page
<?php
$con=mysqli_connect("localhost","root","","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_GET['id'];
$query="SELECT * FROM user";
$result= mysqli_query($con, $query) or die(mysqli_error());
while ($row= mysqli_fetch_array($result))
{
$name_data=$row['name'];
$name_email=$row['email'];
$name_password=$row['password'];
$name_address=$row['address'];
}
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
$address=$_POST['address'];
if(empty($name))
{
//if the value is empty its going to set it equal to the database value
$name=$name_data;
}
else
$name=$name;
if(empty($email))
{
$email=$name_email;
}
else
$email=$email;
if(empty($password))
{
$password=$name_password;
}
else
$password=$password;
if(empty($address))
{
$address=$name_address;
}
else
$address=$address;
}
//0: demo.name (value given for name)
//demo#gmail.com (value given for email)
//demo (value given forpassword)
//demo
//demo.address (value given for address)
$sql = "UPDATE user SET name='".$name."',email='".$email."',password='".$password."',address='".$address."' WHERE id ='".$id."'";
echo mysqli_errno($con) . ": " . mysqli_error($con) . "\n";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header("Location: admin_user_list.php");
exit;
mysqli_close($con);
?>
I am getting an error in update query. I would be highly obliged if someone could help.
P.S # FortMauris here is the edited part that you wished to see.
$id = $_GET['id'];
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$query="SELECT * FROM user";
$result= mysqli_query($con, $query) or die(mysqli_error());
//get the value from database
while ($row= mysqli_fetch_array($result))
{
$name_data=$row['name'];
$name_email=$row['email'];
$name_password=$row['password'];
$name_address=$row['address'];
}
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
$address=$_POST['address'];
if(empty($name))
{
//if the value is empty its going to set it equal to the database value
$name=$name_data;
}
if(empty($email))
{
$email=$name_email;
}
if ($password == '')
{
$password=$name_password;
}
if(empty($address))
{
$address=$name_address;
}
}
echo mysqli_errno($con) . ": " . mysqli_error($con) . "\n";
$sql = "UPDATE user SET name='".$name."',email='".$email."',password='".$password."',address='".$address."' WHERE id ='".$id."'";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
//header("Location: admin_user_list.php");
exit;
mysqli_close($con);
On both pages add this line at the top session_start();
Than on the admin_edit_user Add this line $_SESSION['id']=$_GET['id'];
Than on admin_update_user.php add this line $id=$_SESSION['id'];
Than on the same file this file admin_update_user.php you have this line $id=$_GET['id']; remove it.
$sql = "UPDATE user SET name='".$name."',email='".mysqli_real_escape_string($con,$email)."',password='".$password."',address='".mysqli_real_escape_string($con,$address)."' WHERE id ='".$id."'";
I feel you have reserved keyword in query try to change your query to and you are mixing mysql and mysqli.
remove this line echo mysql_errno($con) . ": " . mysql_error($con) . "\n";
$sql = "UPDATE `user` SET `name`='".$name."',`email`='".$email."',`password`='".$password."',`address`='".$address."' WHERE `id` =".$id;
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
header("Location: admin_user_list.php");
exit;
if it dosn't work then print your query and try to run it manually in phpmyadmin or mysql;
$sql = "UPDATE user SET name='".$name."',email='".$email."',password='".$password."',address='".$address."' WHERE id ='".$id."'";
Instead of this, try using this:
$sql = "UPDATE user SET name = '$name', email = '$email', password = '$password', address = '$address' WHERE id = $id";
It is much cleaner and will solve lots of query issues.
EDIT:
The issue is probably with 1 of your variables having ' or ".
when you put it in, it becomes something like this:
$sam = "Sam'";
$sql = " UPDATE user SET name = 'sam'' ";
It detects an additional inverted comma and therefore returns an error.

Categories