Im trying to create a instant messaging service using php, im able to get the website up and running to register users to MySQL database but for the life of me i cant understand why it wont add the users
I have already made the table for the users in phpMyAdmin, also remade them just in case i messed it up but nothing works
This is my main webpage to register new users
<!doctype html>
<html>
<head>
<style>
*{margin:0px; padding:0px;}
#main{ width:200px; margin:24px auto; }
</style>
</head>
<body>
<?Php
require_once("connection.php") ;
if(isset($_POST['Register'])){
$first_name = $_POST['first_name'] ;
$last_name = $_POST['last_name'] ;
$user_name = $_POST['user_name'] ;
$password = $_POST['password'] ;
if ($first_name !="" and $last_name !="" and $user_name !="" and $password !="" ){
$q="INSERT INTO `user` ('id','first_name','last_name','user_name', 'password')
VALUES('', '".$first_name."', '".$last_name."', '".$user_name."', '".$password."')
" ;
if(mysqli_query($con, $q )){
header("location:login.php") ;
}else{
echo $q ;
}
}else{
echo "please fill in all the boxes" ;
}
}
?>
<div id="main">
<h2 align="center">Registration</h2>
<form method="post">
First Name:<br>
<input type="text" name="first_name" placeholder="First Name" />
<br><br>
Last Name:<br>
<input type="text" name="last_name" placeholder="Last Name" /><br><br>
User Name:<br>
<input type="text" name="user_name" placeholder="User Name" /><br><br>
Password:<br>
<input type="password" name="password" placeholder="Password" /><br><br>
<input type="submit" name"register" value="Register" />
</form>
</div>
</body>
</html>
It is referencing connection.php which is:
<?php
$con = mysqli_connect("localhost","pmauser","root","chat application") ;
?>
Once all the text has been entered and they click register, the form clears but no message pops up, after that i go to phpMyAdmin and check for users and it comes up empty, what should be happening is that it takes all the information in the textboxes and adds them to the data base.
4
Answers
Change
name"register"
toname"Register"
because your check is like this
OR
make it as (if you don’t want to change your html)
The way you are passing parameters would lead to SQL Injection. Please read about it here.
I did notice you assign a value to the input box of register, since the input box is a submit type, value is only for display purposes and not submitted in the form. I’ve included a hidden input box to do this instead.
Refactoring your code and amending it to prevent SQL injection attacks, you get the following:
HTML Document
Connection PHP file.
I suspect that USER info data is not getting stored in your database because AUTOCOMMIT is switched off. To verify this, run:
SELECT @@autocommit;
before your insert. If the result is 0, AUTOCOMMIT is indeed switched off. If it is off, your INSERT will not be saved unless you issue a COMMIT after your INSERT. Alteratively, switch on AUTOCOMMIT (for session or entire database) i.e.
SET autocommit = 1;
The following works perfect for me. I changed somethings but it works :).