Bootstrap Form to Database - php

I created a bootstrap form and want to include the data from the form into my database. My problem is that it is not working and I have no idea why. In my opinion everything is correct. I am looking for hours now but I can't find the issue.
You can find my form here: http://schulkantine.ccsolution.at/registration.php
Please do not look at the style, I have to customize it later.
Here is my register.php! Database connection is working.
<?php
// Create connection credentials
$db_host = 'localhost';
$db_name = 'DBNAME';
$db_user = 'DBUSER';
$db_pass = 'DBPASSWORD';
// Create mysqli object
$connect = new mysqli ($db_host, $db_user, $db_pass, $db_name);
// Error Handler
if ($connect->connect_error) {
printf ("Connection failed: %s\n", $connect->connect_error);
exit();
}
?>
<?php
// Check if form is submitted
if (isset ($_POST['submit'])) {
$anrede = mysqli_real_escape_string ($connect, $_POST['anrede']);
$vorname = mysqli_real_escape_string ($connect, $_POST['vorname']);
$nachname = mysqli_real_escape_string ($connect, $_POST['nachname']);
$strasse = mysqli_real_escape_string ($connect, $_POST['strasse']);
$plz = mysqli_real_escape_string ($connect, $_POST['plz']);
$ort = mysqli_real_escape_string ($connect, $_POST['ort']);
$email = mysqli_real_escape_string ($connect, $_POST['email']);
$telefon = mysqli_real_escape_string ($connect, $_POST['telefon']);
// Validate Input
$query = mysql_query("INSERT INTO user (anrede, firstname_parent, lastname_parent, street_parent, plz_parent, city_parent, email, phonenumber_parent)
VALUES ('$anrede', '$vorname', '$nachname', '$strasse', '$plz', '$ort', '$email', '$telefon')") or die(mysql_error());
}
?>
Hope someone can help me and tell me what my error is!

Try changing
INSERT INTO user (anrede, firstname_parent, lastname_parent, street_parent, plz_parent, city_parent, email, phonenumber_parent) VALUES ('$anrede', '$vorname', '$nachname', '$strasse', '$plz', '$ort', '$email', '$telefon')"
to
INSERT INTO `user` (`anrede`, `firstname_parent`, `lastname_parent`, `street_parent`, `plz_parent`, `city_parent`, `email`, `phonenumber_parent`) VALUES ('$anrede', '$vorname', '$nachname', '$strasse', '$plz', '$ort', '$email', '$telefon')"

Replace this line in your registration.php file. You forget to add "name" attribute so your condition if (isset ($_POST['submit'])) not work.
<button type="submit" name="submit" value="submit" class="btn btn-custom pull-right">Send</button>

Related

php mysqli transactions are not storing second query

ive been struggling with this for hours now. i have a 2 step registraton form and transfered all input to session variables and they all work on the second step. however, when i try to insert data to the second table nothing gets stored and i cannot figure out why.
<?php
include ("encrypt.php");
$conn = mysqli_connect($servername, $dbuser, $dbpassword, $dbname);
$problem = '';
$firstName = $_SESSION['firstName'] ;
$lastName = $_SESSION['lastName'];
$email = $_SESSION['email'];
$username= $_SESSION['username'];
$password= $_SESSION['password'];
$pass = encrypt($password);
if(isset($_POST["mysubmit"]) && ($_POST["mysubmit"]=="Submit Form")){
$dOb = mysqli_real_escape_string ($conn, $_POST["eventDate"]);
$difficulty = mysqli_real_escape_string ($conn, $_POST ["difficultyCatagory"]);
$club = mysqli_real_escape_string ($conn,$_POST["clubSelect"]);
echo $dOb, $difficulty, $club, $firstName, $lastName, $email, $username,$password, $pass;
mysqli_autocommit($conn,FALSE);
mysqli_query($conn,"INSERT INTO userBMX (username,password) VALUES ('$username', '$pass')");
mysqli_query($conn,"INSERT INTO userDetailsBMX(userID, firstName, lastName, email, dateofBirth, Status, club)
VALUES (last_insert_id(),'$firstName','$lastName','$email','$dOb','$difficulty','$club')");
mysqli_commit($conn);
echo 'stored';
/*header ("Location: login.php");*/
}
else{
echo "ERROR: was not able to execute $conn. " . mysqli_error($conn);
}
?>

Asking for some Error

I've got error
when i fill the form and click the submit
the result was error "Data Gagal Di tambahkan"
Here the code
<?php
include 'koneksi/koneksi.php';
if ($_SERVER['REQUEST_METHOD'] == "POST"){
$nis = $_POST['nis'];
$nama = $_POST['nama'];
$kelas = $_POST['kelas'];
$nilai_ulangan_teori = $_POST['nilai_ulangan_teori'];
$nilai_ulangan_praktek = $_POST['nilai_ulangan_praktek'];
$sql = "INSERT INTO t_siswa VALUES ('$nis',
'$nama',
'$kelas',
'$nilai_ulangan_teori',
'$nilai_ulangan_praktek'
)";
$nilai ="($nilai_ulangan_teori + $nilai_ulangan_praktek)/2";
if (mysql_query($sql)){
header("location:index.php");
} else {
echo'<script type="text/javascript">alert("Data gagal ditambahkan");</script>';
}
}
here the connection
<?php
$host = "localhost";
$username = "root";
$password = "";
$db = "db_uas_pwd_2017";
mysql_connect($host, $username, $password) or die (mysql_error());
mysql_select_db($db);
$mysqli = mysql_connect($host, $username, $password, $db);
Your SQL code is wrong. You need to specify the column names in the SQL code. For example, the SQL should look something like this
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
And, mysql_ has been depreciated and now PHP recommends either using mysqli_ or PDO. For more references, visit the links below :
https://www.w3schools.com/php/php_mysql_insert.asp
https://www.w3schools.com/php/php_mysql_connect.asp
Answer from xXAlphaManXx, 15yr old

I cannot insert data into database

I have checked all table and column names are right and there are no mistakes in any input names but it is not inserting that data.
<?php
if(isset($_POST['Murad'])){
session_start();
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$userName=$_POST['username'];
$password=$_POST['pwd1'];
$userName = stripslashes($userName);
$password = stripslashes($password);
$email=$_POST['email'];
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "123";
$mysql_databse = "websiteusersa";
$bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysqli_select_db($bd,'websiteusersa');
$sql = "INSERT INTO websiteusersa
(fullname,lastname,userName,email,pass)
VALUES ( '$firstname', '$lastname','$userName', '$email','$password')";
$query = mysqli_query($bd, $sql);
header("location: main.php?/$firstname/");
}
?>
You can post a question here but not check for error messages?
$query = mysqli_query($bd, $sql) or die(mysqli_error($db));
^
Its a simple debugging process which others have to perform for you then. That will simply tell you where the error is.
Check your statement here:
$sql = "INSERT INTO websiteusersa
(fullname,lastname,userName,email,pass)
VALUES ( '$firstname', '$lastname','$userName', '$email','$password')";
maybe the column fullname could possibly mean firstname that's why your code doesn't work.

query not sending to database

Embarrassing question here as it's obviously something so simple that's going wrong.
(Also for anyone who claims 'duplicate', I've spent 3 hours on here looking up relevant questions and putting numerous answers into practice but to no avail, hence the need for the question.)
I'm in the process of making a user registration form using PHP and MySQL, everything submits fine, however, I don't see the values in my database? So obviously it isn't submitting fine.
THE QUESTION
Why isn't it submitting?
I've done a var_dump($_POST); and the result is :
array(4) { ["name"]=> string(14) "Foo Bar" ["email"]=> string(18) "foob#bar.com" ["password"]=> string(8) "foobar123" ["submit"]=> string(8) "Register" }
I've also done print_r($query); on the query to see what's being passed and the result is:
INSERT INTO user_info (name, email, password) VALUES ('Foo Bar', 'foo#bar.com', '$2y$10$36UQIGc6OwFrNs/TBfc6letRlrrdRGGoj.lh65puJElDJER08ozQe')
The table user_info already exists and my connection to the database is fine.
I've tried using backward commas for the column names, I've also tried not using commas for the values but nothing seems to be submitting.
Here's the code:
HTML -
<form method="post" name="register-form" action="database.php">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
Password: <input type="password" name="password">
<input type="submit" name="submit" value="Register" />
</form>
PHP -
<?php
require 'dbconnect.php';
function registerUser() {
$username = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$hash = password_hash($password, PASSWORD_BCRYPT);
$mysqli = mysqli_init();
$query = "INSERT INTO user_info (name, email, password) VALUES ('$username', '$email', '$hash')";
$mysqli->query($con, $query); //$con referenced in dbconnect to connect to db
var_dump($_POST);
print_r($query);
}
if(isset($_POST['submit']) === TRUE){
registerUser();
}
?>
dbconnect.php
<?php
$dbserver = 'localhost';
$dbusername = 'foo';
$dbpassword = 'bar';
$dbdatabase = 'usersdb';
$con = mysqli_connect($dbserver, $dbusername, $dbpassword, $dbdatabase);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
I'm starting to think this might be permission related but it's on my localhost using XAMPP so surely it can't be? Any help would be greatly appreciated! Thank you.
Inside your function registerUser there is no $con defined.
create a connection and your data will get inserted.
function registerUser($con) {
$username = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$hash = password_hash($password, PASSWORD_BCRYPT);
$mysqli = mysqli_init();
$query = "INSERT INTO user_info (name, email, password) VALUES ('$username', '$email', '$hash')";
$mysqli->query($con, $query); //$con referenced in dbconnect to connect to db
var_dump($_POST);
print_r($query);
}
The problem lies here:
$mysqli = mysqli_init();
$query = "INSERT INTO user_info (name, email, password) VALUES ('$username', '$email', '$hash')";
$mysqli->query($con, $query); //$con referenced in dbconnect to connect to db
You need to establish a connection using mysqli_real_connect after mysqli_init or use the constructor new mysqli() instead of those two. Also $mysqli->query uses the $mysqli object and takes only one parameter, your query. If you have specified the connection somewhere else, you need to use mysqli_query. See http://php.net/manual/de/mysqli.query.php
This is the solution :
in your dbconnect.php reaplace this line
$con = mysqli_connect($dbserver, $dbusername, $dbpassword, $dbdatabase);
with
$mysqli = new mysqli($dbserver, $dbusername, $dbpassword, $dbdatabase);
and reaplace the content of database.php with this :
require 'dbconnect.php';
$username = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$hash = password_hash($password, PASSWORD_BCRYPT);
if(isset($_POST['submit']) === TRUE){
// $mysqli = mysqli_init();
$query = "INSERT INTO user_info (name, email, password) VALUES ('$username', '$email', '$hash')";
$mysqli->query($query); //$con referenced in dbconnect to connect to db
var_dump($_POST);
print_r($query);
}
this should work

Insertion In php not working

I have this php script that inserts data from a form into the database.The code always returns an error. What might be the problem.
NB: the names of the fields in the form are correctly matched.
<?php
$db_hostname = 'localhost';
$db_database = 'townmanagement';
$db_username = 'root';
$db_password = '';
// Connect to server.
$db_server = mysql_connect($db_hostname, $db_username, $db_password)
or die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
// Get values from form
$fname= mysql_escape_string(trim ($_POST['fname']));
$lastname= mysql_escape_string(trim ($_POST['lname']));
$dpt=mysql_escape_string(trim($_POST['dpt']));
$user= mysql_escape_string(trim ($_POST['username']));
$psswd=mysql_escape_string(trim ($_POST['password']));
// Insert data into mysql
$sql="INSERT INTO staff_reg (fname, lname, dpt, username, password, registration_date)
VALUES ($fname, $lastname, $dpt, $user, SHA1($password), NOW())";
$result = mysql_query($sql);
if($result){
echo ("sUCCESSFUL");
}
else {
echo "error";;
}
?>
<?php
// close connection
mysql_close();
?>
You need to quote your parameters in the SQL statement
$sql="INSERT INTO staff_reg (fname, lname, dpt, username, password, registration_date)
VALUES ('$fname', '$lastname', '$dpt', '$user', SHA1('$password'), NOW())";
And if possible you should upgrade to mysqli or pdo.
You are missing quotes around your values:
$sql="INSERT INTO staff_reg (fname, lname, dpt, username, password, registration_date)
VALUES ('$fname', '$lastname', '$dpt', '$user', SHA1($password), NOW())";
For better troubleshooting, consider adding to your mysql_query statement to detect when and why the query fails:
$result = mysql_query($sql) or die( mysql_error() );
Finally, be aware that the mysql_* functions are deprecated. Please consider updating your code to use mysqli or PDO.
mysql_select_db($db_database,$db_server)
or die("Unable to select database: " . mysql_error());
//you have to select db using connection previously established
Try this: You should know, that I am not encouraging you to use mysql_ since it is deprecated, and you should learn and implement PDO in the future:
<?php
$db_hostname = 'localhost';
$db_database = 'townmanagement';
$db_username = 'root';
$db_password = '';
// Connect to server.
$db_server = mysql_connect($db_hostname, $db_username, $db_password)
or die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
// Get values from form
$fname= mysql_escape_string(trim ($_POST['fname']));
$lastname= mysql_escape_string(trim ($_POST['lname']));
$dpt=mysql_escape_string(trim($_POST['dpt']));
$user= mysql_escape_string(trim ($_POST['username']));
$psswd=mysql_escape_string(trim ($_POST['password']));
$psswd2 = SHA1($psswd);
// Insert data into mysql
$sql="INSERT INTO staff_reg (fname, lname, dpt, username, password, registration_date)
VALUES ('".$fname."', '".$lastname."', '".$dpt."', '".$user."', '".$psswd2."', "NOW()" )";
if(mysql_query($sql)); {
echo ("sUCCESSFUL");
}else {
echo "error";;
}
mysql_close();
?>

Categories