How to fix MySQL and PHP in WAMP not inserting data? - php

I’m using WAMP as my php server, I’m using PHP-AdminPage for my databse (usin MySql), Everything is working, my database is connected . except for when I want to display the data, rows are created but they’re EMPTY! even tho I didn’t leave them empty. I don’t understand, I’m so confused.
PHP Code:
<html>
<body>
<?php
$conn = new mysqli('localhost','root','');
//check connection
if ($conn-> connect_error) {
die("Connection failed: ".$conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn,"catcafe");
//insert query
$sql="INSERT INTO customer(FName,LName,Email,PhNum,Time) VALUES('$_POST[firstname]',
'$_POST[lastname]','$_POST[Email]','$_POST[Mobile]','$_Post[Time]')";
if($conn->query($sql) === true) {
echo "New record created";
}
else{
echo "Error, check values".$sql."<br>".$conn->error;
}
mysqli_close($conn);
?>
</body>
</html>
//insert query
$sql="INSERT INTO customer(FName,LName,Email,PhNum,Time) VALUES('$_POST[firstname]',
'$_POST[lastname]','$_POST[Email]','$_POST[Mobile]','$_Post[Time]')";
MySQL keeps telling me that the error is here! but I dont know what it is, the columns have the exact same name as the ones in the database.
MY FORM /HTML CODE:
<form action="InsertReservation.php">
<label for="fname">First Name </label><br>
<input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:600px; font-size: 30px;">
<br>
<label for="lname">Last Name </label><br>
<input type="text" id="lname" name="lastname" placeholder="Your last name.."style="width:600px; font-size: 30px;">
<br>
<label for="Email">Email </label><br>
<input type="text" id="Email" name="Email" placeholder="Your Email is.."style="width:600px; font-size: 30px;">
<br>
<label for="Mobile">Mobile Number </label><br>
<input type="text" id="Mobile" name="Mobile" placeholder="Your Mobile Number is.." style="width:600px; font-size: 30px;">
<br>
<label for="Time">Time </label><br>
<input type="time" id="Time" name="Time" min="8:00" required style="width:600px; font-size: 30px;">
<br>
<input type="submit" value="Submit" onclick="alert ('Thank you. Your Reservation Is Confirmed!')">
<button type="reset" value="Reset" onclick="alert ('Reset is complete')"> Reset </button> <br>
</form>
MySQL Code
SELECT * FROM `customer` ORDER BY `customer`.`FName` ASC
I want it to insert the data, but it keeps inserting empty data.

Validate your input data all the time.
Use prepared statments for user input
Time is a mysql reserved word so use backticks `` around it.
$conn = new mysqli('localhost', 'root', '');
//check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn, "catcafe");
//insert query
$sql = "INSERT INTO customer(FName,LName,Email,PhNum,`Time`) VALUES(?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssss", $_POST['firstname'], $_POST['lastname'], $_POST['Email'], $_POST['Mobile'], $_POST['Time']);
if ($stmt->execute()) {
echo "New record created";
} else {
echo "Error, check values " . $stmt->error;
}
mysqli_close($conn);
?>

Hello Please use this code on a single file it's working fine.
Thanks
$conn = new mysqli('localhost', 'root', '');
//check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn, "catcafe");
if($conn){
//insert query
if(isset($_POST['submit']) && $_POST['submit'] == 'Submit'){
$sql = "INSERT INTO customer(FName,LName,Email,PhNum,Time)
VALUES('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['Email']."','".$_POST['Mobile']."','".$_POST['Time']."')";
if($conn->query($sql) === true) {
echo "New record created";
}
else{
echo "Error, check values".$sql."<br>".$conn->error;
}
}
mysqli_close($conn);
}
<form method="post">
<label for="fname">First Name </label><br>
<input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:600px; font-size: 30px;">
<br>
<label for="lname">Last Name </label><br>
<input type="text" id="lname" name="lastname" placeholder="Your last name.."style="width:600px; font-size: 30px;">
<br>
<label for="Email">Email </label><br>
<input type="text" id="Email" name="Email" placeholder="Your Email is.."style="width:600px; font-size: 30px;">
<br>
<label for="Mobile">Mobile Number </label><br>
<input type="text" id="Mobile" name="Mobile" placeholder="Your Mobile Number is.." style="width:600px; font-size: 30px;">
<br>
<label for="Time">Time </label><br>
<input type="time" id="Time" name="Time" min="8:00" style="width:600px; font-size: 30px;">
<br>
<input type="submit" name="submit" value="Submit" onclick="alert ('Thank you. Your Reservation Is Confirmed!')">
<button type="reset" value="Reset" onclick="alert ('Reset is complete')"> Reset </button> <br>
</form>

Related

Why can't I enter the data into the database even though the codes are correct and no error appear?

There is no change and no response in the form or in the database. I believe
the code is okay as no error appears and I included the connect database pdo. The connection is okay, and I changed the position of code to be above the form and under form but still no response. I connected correctly.
I separated the php page but it did not help.
Where is the problem in this code ?
Summary of the problem:
1-no response or error message
2- data not inserted into database
connection database:
<?php
$dsn ='mysql:host=localhost;dbname=person';
$user ='root';
$pass = "";
$option = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8",);
try{
$pdo = new PDO($dsn, $user , $pass, $option);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);}
catch(PDOException $e){
die("ERROR: Could not connect. " . $e->getMessage()); }
and form of html:
<div class="container">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=" POST">
<label for="fname"> الاسم </label><br>
<input type=" text" id="fname" name="username" placeholder=" الاسم"><br>
<label for="email"> البريد الالكتروني </label><br>
<input type="email" id="email" name="email" placeholder="البريد الالكتروني "><br>
<label for="valley"> اسم الحي </label><br>
<input type="text" id="valley" name="valley" placeholder="اسم الحي "><br>
<label for="phone"> رقم الجوال</label><br>
<input type="text" id="phone" name="mobile" placeholder="رقم الجوال "><br>
<label for="subject">الطلب</label><br>
<textarea id="subject" name="subj" placeholder="اكتب الطلب " style="height:200px">
</textarea><br>
<input type="submit" name="submit" value="ارسل">
</form>
</div>
<br>
php and query insertion to database
<?php
include('connect_pdo.php');
if (isset($_post['submit'])) {
$name = $_POST['username'];
$email = $_POST['email'];
$valley = $_POST['valley'];
$phone = $_POST['mobile'];
$subject = $_POST['subj'];
$stmt = $pdo->prepare("INSERT INTO emails (username,email,valley,mobile,subj)
VALUES('$name','$email','$valley','$phone' ,'$subject')");
if ($stmt->execute()) {
echo "تم الارسال بنجاح";}
else {
echo "تم الارسال" } }

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){}

storing data to mysql database using php

my code wont insert into the student table I dont know whats wrong could you guys possibly help? I tried to print and error message in each line to find out where the error was but I got no errors so I'm confused
<?PHP
if(isset($_POST['submit'])) {
$link = mysql_connect('localhost','root','');
if (!$link) {
die('Could not connect :' . mysql_error());
}
$Selected= mysql_select_db("elearningg", $link);
if (!$Selected) {
die("Could not connect: " . mysql_error());
}
$sql = "INSERT INTO student (FirstName, LastName, UserName,Password, confirmP ,phoneNum,Email) VALUES ('$_POST[FN]','$_POST[LN]','$_POST[userName]','$_POST[password]','$_POST[confirmPass]','$_POST[number]','$_POST[email]') ";
mysql_query($sql,$link);
mysql_close($link);
}
?>
and here's the html form:
<form method="post" action="Register.php">
<div class="contact-to">
<P> <input name="FN" type="text" class="text" value="First name" >
<input name="LN" type="text" class="text" value="Last name" style="margin-left: 10px">
<input name="userName" type="text" class="text" value="username" style="margin-left: 10px">
</div>
<div class="contact-to">
<input name="password" type="text" class="text" value="Password" style="margin-left: 10px">
<input name="confirmPass" type="text" class="text" value="Confirm password" style="margin-left: 10px">
<input name="number" type="text" class="text" value="Phone Number" >
<input name="email" type="text" class="text" value="email" >
</div>
<div> <input value="submit" type="submit" class="submit"> </div>
</form>
If you're trying to validate $_POST['submit'] after user clicks the button, you should include attribute name inside your input submit tag like this:
<input value="submit" type="submit" name="submit" class="submit">
And then you can check $_POST:
if(isset($_POST['submit'])) {
// your code
}
PHP will always return false in your condition without a attribute name...
Also, consider using mysqli with prepared statements, or PDO with prepared statements, they're much safer.
Change your sql insert query code to this:
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
function debug($data){
echo '<pre>';
print_r($data);
echo '</pre>';
}
function getConnected($host,$user,$pass,$db) {
$mysqli = new mysqli($host, $user, $pass, $db);
if($mysqli->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $mysqli;
}
if(isset($_POST['submit'])) {
debug('Connecting database...');
$link = getConnected('localhost','root','', 'stackoverflow');
debug('Database connected...');
$sql = "
INSERT INTO `student` (`FirstName`, `LastName`, `UserName`, `Password`, `confirmP`, `phoneNum`, `Email`)
VALUES (
'{$_POST['FN']}',
'{$_POST['LN']}',
'{$_POST['userName']}',
'{$_POST['password']}',
'{$_POST['confirmPass']}',
'{$_POST['number']}',
'{$_POST['email']}'
)";
if (!mysqli_query($link, $sql)) {
debug("Errormessage: ". mysqli_error($link)."\n");
}else{
debug('Query executed successfully...');
}
mysqli_close($link);
}
?>
Also change your form submit button code to this:
<div> <input name="submit" value="submit" type="submit" class="submit"> </div>

Adding a record from html form to oracle database

I have a HTML form that I want to add a record to an Oracle database when somebody hits submit. The table is hooking up somewhat, the problem is that when somebody submits their information they come up as NULL values in the database table.
HTML:
<form name="myForm" action="/Add_File.php" onsubmit="return validateForm()" method="post"><!--Form-->
<fieldset>
<label class ="label1" for "name">First Name: </label>
<input type="text" name="fname"><br />
<label class ="label1" for "name">Surname: </label><input type="text" name="sname"><br/>
<label for="email">E-mail Address: </label><input type="text" name="email"><br />
<label for "address">Address: </label> <input type="text" name="address"><br />
<label class="label" for "Password">Select a Password: </label> <input type="password" name="pass"><br />
<label class="label" for "Password">Retype-Password:</label> <input type="password" name="pass2"><br />
</fieldset>
<fieldset><input class="button" type="submit" onclick="message()" value="Submit"/>
<input class="button" type="reset" value="reset form" onclick="myFunction()"/>
</fieldset>
</form>
PHP code:
$dbuser = "scott";
$dbpassword = "tiger";
$db = "orabis";
$conn = oci_connect($dbuser,$dbpassword,$db);
if (!$conn){
echo "Connection error";
exit;
}
$fname=$_POST['First_Name'];
$sname=$_POST['Surname'];
$email=$_POST['Email_Address'];
$address=$_POST['Address'];
$selpass=$_POST['Select_A_Password'];
$confirm=$_POST['Retype_Password'];
$sql = "INSERT INTO Become_A_Member_110385461(First_Name,Surname,Email_Address,Address,Select_A_Password,Retype_Password)
VALUES ('".$fname."','".$sname."', '".$email."', '".$address."','".$selpass."', '".$confirm."')";
$stmt = oci_parse($conn, $sql);
if (!$stmt) {
echo "Error in preparing the statement";
exit;
}
oci_execute($stmt, OCI_DEFAULT);
print "Record Inserted";
oci_commit($conn);
oci_close($conn);
change like this
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$email=$_POST['email'];
$address=$_POST['address'];
$selpass=$_POST['pass'];
$confirm=$_POST['pass2'];

PHP won't post form to database

Trying to post a simple form to my database but can't get it to work. I have PHP and MySQL activated through XAMPP. The database "E-mail list" is set up with the table "Players".
PHP code:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$query = 'INSERT INTO Players (
name,
email,
phone,
other
)
VALUES ('.$name.', "'.$email.'", "'.$phone.'","'.$other.'")';
if ($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}}
?>
And the form:
<form id="myForm" method="post">>
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
</form>
<p><strong id="error"></strong></p>
<br><br>
<input type="button" id="save" name="save" value="Submit Form" />
<p id="response"></p>
I did some changes in your codes both PHP and HTML Parts.,
For PHP :
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$query = "INSERT INTO Players (`name`,`email`,`phone`,`other`) VALUES ('".$name."','".$email."','".$phone."','".$other."')";
if($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}
}
?>
For HTML :
<form id="myForm" method="post" action="">
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" /><br />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" /><br />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
<p><strong id="error"></strong></p>
<br><br>
<input type="submit" id="save" name="save" value="Submit Form" />
<p id="response"></p>
</form>
I think this may help you to resolve your problem.
Missing double quotes for name value in your SQL.
VALUES ("'.$name.'", "'.$email.'", "'.$phone.'","'.$other.'")';
Use Firefox/firebug to see the parameters and result, and add an echo($query); so you can see it in firebug.
'E-mail list' doesn't seem like convenient database name, though it should be okay.
Anyway, your goal should be to display all possible error that may occur.
So, you have to always check for the errors and report them in more usable form than just 'Cannot save data.'
Always check your connect
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
if ($mysqli->connect_error) {
trigger_error($mysqli->connect_error);
}
same for the query
if (!$mysqli->query($query)) {
trigger_error($mysqli->error." ".$query);
}
If you see no error messages - check the logic of your code: if you ever run the code, if you run the code you wrote, if PHP works, typos etc.

Categories