PHP Form not adding values to MySQL Database [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
I want to start by saying i tried atleast 10 different solutions on this site, but none worked in my case so i was wondering if you guys could help me figure out what's wrong.
Form:
<form align="center" action=includes/signup.php method="post">
<input type="text" id="fname" name="fname" placeholder="First Name"><br>
<input type="text" id="lname" name="lname" placeholder="Last Name"><br>
<input type="text" id="uname" name="uname" placeholder="Username"><br>
<input type="text" id="email" name="email" placeholder="Email"><br>
<input type="text" id="phone" name="phone" placeholder="Phone Number"><br>
<button type="submit" value="submit" name="submit" name="save"> Add To Database </button>
</form>
signup.php
<?php
include_once 'dbh.php';
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$uname = $_POST['uname'];
$email = $_POST['email'];
$phone = $_POST['phone_no'];
$sql = "INSERT INTO `users` (f_name, l_name, username, email, phone_no)
VALUES ('$fname', '$lname', '$uname', '$email', '$phone');";
if (!mysqli_query($conn, $sql)) {
echo 'Not Inserted';
} else {
echo 'inserted';
}
header("refresh:3; url=../index.php?add=success");
My problem is that everytime i press the button, i get "Not Inserted", but when i changed the values from "$fname" to "David", David got inserted into the database. Which means the data from the form is not being received by this page.
I am using VS Code incase that makes a difference.

Related

Why is this form posting to the wrong path [duplicate]

This question already has answers here:
Submit HTML form on self page
(5 answers)
Closed 2 years ago.
I have a page for people to submit their information /collect.php but when submitting it keeps going to the root. Is it possible to submit the form and make it go to the same collect?
Here is the code I have so far:
<?php if (!empty($_POST)):
$firstname = htmlentities($_POST['firstname']);
$lastname = htmlentities($_POST['lastname']);
$email = htmlentities($_POST['email']);
$phone = htmlentities($_POST['phone']);
echo $firstname."<br/>";
echo $lastname."<br/>";
echo $email."<br/>";
echo $phone."<br/>";
else: ?>
<form action="." method="post">
<input type="text" name="firstname" placeholder="First name"><br/>
<input type="text" name="lastname" placeholder="Last name"><br/>
<input type="email" name="eamil" placeholder="Email address"><br/>
<input type="phone" name="number" placeholder="Phone number"><br/>
<input type="submit">
</form>
<?php endif; ?>
You're almost there, to submit to the same page you can use the $_SERVER["PHP_SELF"] which is a super global variable that returns the filename of the currently executing script.
You'll also want to be careful with your input names, email is misspelled and the form for the phone number is named number while the post is labelled phone. Also, the correct the correct HTML5 for a phone number is tel.
<?php if (!empty($_POST)):
$firstname = htmlentities($_POST['firstname']);
$lastname = htmlentities($_POST['lastname']);
$email = htmlentities($_POST['email']);
$phone = htmlentities($_POST['phone']);
echo $firstname."<br/>";
echo $lastname."<br/>";
echo $email."<br/>";
echo $phone."<br/>";
else: ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="text" name="firstname" placeholder="First name"><br/>
<input type="text" name="lastname" placeholder="Last name"><br/>
<input type="email" name="email" placeholder="Email address"><br/>
<input type="tel" pattern="([0-9]{3}) [0-9]{3}-[0-9]{4}" name="phone" placeholder="(123) 456-7890" required><br/>
<input type="submit">
</form>
<?php endif; ?>

Inserting data with PHP from a form to MySQL database

I have created a HTML form which will need to insert the data that was entered into the form straight into a table in mySQL.
newuser.php file
<?php
//including the connection page
include('./DB_Connect.php');
//get an instance
$db = new Connection();
//connect to database
$db->connect();
//fetch username and password
$usertype = $_POST['userType'];
$firstname = $_POST['firstName'];
$lastname = $_POST['lastName'];
$username = $_POST['userName'];
$password = $_POST['password'];
$address1 = $_POST['add1'];
$address2 = $_POST['add2'];
//write the sql statement
$query = "INSERT INTO USERS (usertype, fname, lname, username, password, add1, add2)
VALUES ('$usertype', '$firstname', '$lastname', '$username', '$password', '$address1', '$address2')";
mysql_query($query,$db);
if (($query) == TRUE) {
echo "New record created successfully";
header("Location: login.php", true);
exit();
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
header("Location: register.php", true);
exit();
}
//close once finished to free up resources
$db->close();
?>
With the following html form:
<form action="newuser.php" method="POST" class="form" id="registerForm">
<label> Type of user: </label> <br>
<input type="radio" name="userType" id="itemSeeker">Item Seeker </input>
<input type="radio" name="userType" id="itemDonor">Item Donor </input>
<input type="radio" name="userType" id="peopleSeeker">People Seeker </input>
<input type="radio" name="userType" id="peopleDonor">People Donor </input>
<br>
<br>
<div class="form-inline">
<div class="form-group">
<input type="text" name="firstName" placeholder="First Name: " align="center" class="form-control" ></input>
</div>
<div class="form-group">
<input type="text" name="lastName" placeholder="Last Name: " align="center" class="form-control" ></input>
</div>
</div>
<br>
<input type="text" name="email" placeholder="Email Address: "align="center" class="form-control" ></input>
<br>
<div class="form-inline">
<div class="form-group">
<input type="text" name="userName" placeholder="Username: " align="center" class="form-control" ></input>
</div>
<div class="form-group">
<input type="password" name="password" placeholder="Password: " align="center" class="form-control" ></input>
</div>
</div>
<br>
<!-- <label> Address 1: </label>-->
<input type="text" name="add1" placeholder="Address 1: " align="center" class="form-control" ></input>
<!-- <label> Address 2: </label>-->
<input type="text" name="add2" placeholder="Address 2: " align="center" class="form-control" ></input>
<br>
<button class="btn btn-primary" name="submitReg" type="submit">Submit</button><br>
<br>
<a href="login.php" >Already have an account?</a>
</form>
The USERS table
The above two blocks of code and the code that I'm working with.
My problem here is, when the form is submitted, the data isn't actually being entered into the table. Note that the first ever submission actually did work. All submissions after the first one don't seem to be entering anything into the database.
I'm not quite sure what's wrong with my code for it to not work. It does go to the 'login.php' page which means there aren't any faults and the query submitted correctly.
Could someone please tell me what I'm doing wrong, thank you.
right now you have alot more trouble than your insert problem. your code is totaly insecure. (mysql injections).
Dont use mysql_* functions use pdo instead with prepared statements!
you output spaces before you send a header you cant send header if you have output. your redirect wouldnt work. dont relay on a clientside redirect use may have it disabled so output a link where you want the user to go.
anotherthing your radio buttons have no value check html syntax
var_dump($_POST) and check if you submit everything. also check for isset or empty befor assign variables. do some sort of validation.
have a look at some php frameworks they provide much more flexibility and error checking
dont reinvent the wheel by writing everthing by your own in a 10 or more year behind procedural way

Why is my form data being posted to my database? [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I have a HTML form which should post the data into an SQL database, No errors occur on submitting but the database does not receive the data.
<?php
include 'init.php';
//check if submit button is clicked
if(isset($_POST['submit'])) {
$User_Username = $_POST['User_Username'];
$User_First_Name = $_POST['User_First_Name'];
$User_Surname = $_POST['User_Surname'];
$User_Email = $_POST['User_Email'];
$User_Password = $_POST['User_Password'];
// prepare sql and bind parameters
$stmt = $pdo->prepare("INSERT INTO users (User_Username, User_First_Name, User_Surname,User_Password,User_Email)
VALUES (:User_Username, :User_First_Name, :User_Surname, :User_Password, :User_Email");
$stmt->bindParam(':User_Username', $User_Username);
$stmt->bindParam(':User_First_Name', $User_First_Name);
$stmt->bindParam(':User_Surname', $User_Surname);
$stmt->bindParam(':User_Email', $User_Email);
$stmt->bindParam(':User_Password', $User_Password);
$stmt->execute();
}
?>
<br>
<form action="" method="post">
<label>Username :</label><br>
<input type="text" name="User_Username" id="User_Username" required="required" placeholder=""/><br /><br />
<label>First Name :</label><br>
<input type="text" name="User_First_Name" id="User_First_Name" required="required" placeholder=""/><br /><br />
<label>Surname :</label><br>
<input type="text" name="User_Surname" id="User_Surname" required="required" placeholder=""/><br /><br />
<label>Email :</label><br>
<input type="email" name="User_Email" id="User_Email" required="required" placeholder=""/><br/><br />
<label>Password :</label><br>
<input type="Password" name="User_Password" id="User_Password" required="required" placeholder=""/><br /><br />
<br>
<input type="submit" value=" submit " name="submit"/><br />
</form>
</body>
</html>
You didn't close VALUES (....
You should have:
$pdo->prepare("INSERT INTO users (User_Username, A_Lot_more)
VALUES (:User_Username, :A_Lot_More)");
// ^ This one was missing
And you should NEVER save passwords as plain-text. Use something like password_hash() -- http://php.net/password_hash

Looking for a succes message after completing a modal form that is sent to a mysql database using php

<!--This is the html form code-->
<div class="modal-body">
<form id="modal-form" accept-charset="UTF-8" method="POST" action="signUp.php" data-remote="true" >
<p><label for="firstName"><small>First Name:</small></label><br />
<input type="text" id="firstName" name="firstName" required="required" /></p><br />
<p><label for ="lastName"><small>Last Name:</small></label><br />
<input type="text" id="lastName" name="lastName" required="required" /></p><br />
<p><label for="email"><small>Email: </small></label><br />
<input type="email" name="email" required="required"/></p><br />
<input id="modal-form-submit" type="submit" name="submit" class="btn btn-success"/>
</form>
`
<?php
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
require("includes/config.php");
require("includes/connect.php");
if(isset($_POST['submit'])){
$query="INSERT INTO mb_emaillist (ID, firstName, lastName, email) VALUES (null, '$firstName', '$lastName', '$email')";
$result=mysqli_query($db, $query);
if(!$result)
die("SELECT error: " .mysqli_error($db));
if($result){
print"Thank you $firstName $lastName for signing up with MyBrunch!";
}
}
mysqli_close($db);
?>`
I am looking to get a success message delivered in a modal after my form was successfully sent to my database. Right now the information is sent but, nothing happens.
Wow, just pick a PHP Framework! Don't work like this even for a small project.
Among so many options, I recommend Laravel...
Pick the 4.2 release, it's the simplest!
BTW: Your print call at line 16, lack of parenthesis.

PHP Registration Form not saving

I have a HTML Sign Up form that allows new users to be registered to the site.:
<form action="register.php" method="POST" class="register-form">
<h1>Create Account</h1>
<label>
<span>First Name :</span>
<input id="firstname" type="text" name="firstname" placeholder="Your First Name" autocomplete="off" required/>
</label>
<label>
<span>Surname :</span>
<input id="surname" type="text" name="surname" placeholder="Your Surname" autocomplete="off" required/>
</label>
<label>
<span>Username :</span>
<input id="username" type="text" name="username" placeholder="Your Chosen Username" autocomplete="off" required/>
</label>
<label>
<span>Email :</span>
<input id="email" type="email" name="email" placeholder="Your Email Address" autocomplete="off" required/>
</label>
<label>
<span>Password :</span>
<input id="password" type="password" name="password" placeholder="Your Chosen Password" autocomplete="off" required/>
</label>
<hr>
<input name="action" type="hidden" value="signup" />
<input type="submit" class="btn register btn-success btn-lg" name="submit" value="Register">
</form>
Which goes to register.php:
<?php
$connection = mysql_connect('localhost', 'root', 'password');
if (!$connection) {
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('gmaps1');
if (!$select_db) {
die("Database Selection Failed" . mysql_error());
}
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])) {
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$query = "INSERT INTO `users` (firstname, surname, username, password, email) VALUES ('$firstname', '$surname', '$username', '$password', '$email')";
$result = mysql_query($query);
if ($result) {
header("Location: thank_you.html");
} else {
echo 'User Not Created';
}
}
?>
But when I click the Register button it doesn't save the data and returns "User Not Created". Would it be better using MySQLi rather than MySQL or is there a better way for this to work??
Error checking solved my problem - "field 'active' doesn't have a default value"
There was an inactive field in the table 'Users'. I got rid of that and it works fine. It must have been added in by mistake.
You don't get a "Database Connection Failed" so you sucessfully connected with the database
Its better to use MySQLi or PDO (my choice)
At least use mysql_real_escape and maybe a trim() but thats just a starting point when it comes to security
check wether all your database fields are named exactly the way you are adressing them inside your Insert-Statement
Do a echo $query, open phpMyAdmin (for example) and copy-paste the output inside the SQL field and send -> you may get a MySQL error you can analyse
It's better to store passwords hashed. Try inserting MD5($password) (there are way better options!) and on login do compare:
if(MD5($inputPassword) == $passwordhashFromDatabase){}

Categories