Form Not Posting To SQL Database [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
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 5 years ago.
I have this form and I can’t get it to post to my sql database?
https://xcanberracars.com/assets/booking/bookride_bu240118.php
This is the code I believe I have everything right but obviously not.
error_reporting(E_ALL & ~(E_STRICT|E_NOTICE|E_WARNING));
$host="localhost";
$dbuser="xcanberr_accountmanager";
$dbpass="*********”;
$dbname="xcanberr_PromoCodes";
$link = mysqli_connect($host, $dbuser, $dbpass,$dbname);
if(!$link)
{
echo "ERROR| Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
$booking_id = "XBR".date("dmYHis");
$sql = "INSERT INTO booking_table (bookingID, first_name, last_name, email, phone, pickup_location, drop_location)
VALUES ('$bookingId', '$firstName', '$lastName', '$email', '$phone,'$pickupAddressLine1', '$dropAddressLine1')";
mysqli_query($conn, $sql);
I can get it to work on my local host pc but won’t work on the server

In your code their is 2 mistakes.
1- line no. 19 '$phone should be '$phone'
2- line no. 21 mysqli_query($conn, $sql); should be mysqli_query($link, $sql);
3- booking_id AND bookingId
use this code
error_reporting(E_ALL & ~(E_STRICT|E_NOTICE|E_WARNING));
$host="localhost";
$dbuser="xcanberr_accountmanager";
$dbpass="*********”;
$dbname="xcanberr_PromoCodes";
$link = mysqli_connect($host, $dbuser, $dbpass,$dbname);
if(!$link){
$bookingId = "XBR".date("dmYHis");
$firstName = mysqli_real_escape_string($link, $firstName);
$email = mysqli_real_escape_string($link, $email);
$phone = mysqli_real_escape_string($link, $phone);
$pickupAddressLine1 = mysqli_real_escape_string($link, $pickupAddressLine1);
$dropAddressLine1 = mysqli_real_escape_string($link, $dropAddressLine1);
$sql = "INSERT INTO booking_table (bookingID, first_name, last_name, email, phone, pickup_location, drop_location)
VALUES ('$bookingId', '$firstName', '$lastName', '$email', '$phone','$pickupAddressLine1', '$dropAddressLine1')";
mysqli_query($link, $sql);

Related

Parse error: syntax error, unexpected 'values' (T_STRING) in E:\xampp\htdocs\study\connect.php on line 13 [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
When I try to connect to my database, I get this error:
Parse error: syntax error, unexpected 'values' (T_STRING) in
E:\xampp\htdocs\study\connect.php on line 13
This is my php code:
<?php
$firstName = $_POST['fname'];
$middleName = $_POST['mname'];
$lastName = $_POST['lname'];
$password = $_POST['pwd'];
//Database Connection
$conn = new mysqli ('localhost','root','','task1');
if($conn->connect_error){
die('connection failed :' .$conn->connect_error);
}else{
$stmt = $conn->prepare("insert to registration(firstName, middleName, lastName, password")
values(?,?,?,?)
$stmt ->bind_param("ssss",$firstName,$middleName, $lastName, $password);
$stmt ->execute();
echo "Registration Successful..";
$stmt ->close();
$conn ->close();
}
?>
$stmt = $conn->prepare("insert to registration(firstName, middleName, lastName, password")
values(?,?,?,?)
I think you made a mistake.
Try this instead of your code, you missed out ");
$stmt = $conn->prepare("insert to registration(firstName, middleName, lastName, password")
values(?,?,?,?)");
Closing double quote issue, use it as
$stmt = $conn->prepare("insert to registration(firstName, middleName, lastName,
password)
values(?,?,?,?)");

Inserting data into multiple different data tables

I was trying to insert data into multiple data tables. It's only working for single data tables, I'm just wondering how I would be able to insert data into two data tables. I've been struggling with this issue for the past few hours and can't seem to get to the bottom of it. If anyone has any advice please let me know. :)
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost","ivodatat","","");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Inputs for security
$fname = mysqli_real_escape_string($link, $_REQUEST['fname']);
$sname = mysqli_real_escape_string($link, $_REQUEST['sname']);
$address = mysqli_real_escape_string($link, $_REQUEST['address']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);
$phone = mysqli_real_escape_string($link, $_REQUEST['phone']);
$mac = mysqli_real_escape_string($link, $_REQUEST['mac']);
$installer = mysqli_real_escape_string($link, $_REQUEST['installer']);
$status = mysqli_real_escape_string($link, $_REQUEST['status']);
// Insert Query
$sql1 = "INSERT INTO leadlist (fname, sname, address, email, phone, mac, installer, status) VALUES ('$fname', '$sname', '$address', '$email', '$phone', '$mac', '$installer', '$status')";
$sql2 = "INSERT INTO $installer (fname, sname, address, email, phone, mac, installer, status) VALUES ('$fname', '$sname', '$address', '$email', '$phone', '$mac', '$installer', '$status')";
if (mysqli_multi_query($link, $sql1, $sql2)){
mysqli_close($conn);
header("Location: installercontrol.php");
exit;
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close The Connection
mysqli_close($link);
?>
To use mysqli_multi_query you need to append the queries to each other as it only takes one query argument. From the manual:
Executes one or multiple queries which are concatenated by a semicolon.
Try this instead:
mysqli_multi_query($link, $sql1 . ';' . $sql2)
You should probably also update your error message:
echo "ERROR: Could not able to execute $sql1;$sql2. " . mysqli_error($link);

mysqli PHP not inserting into DB, no errors on Log [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 6 years ago.
My PHP Code:
<?php
//Connecting to sql db.
$mysqli = new mysqli("127.0.0.1", "admin", "pass", "enedpt_faculties");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
mysqli_set_charset($mysqli,"utf8");
$user = $_POST['user'];
$pass = $_POST['pass'];
$nome = $_POST['nome'];
$apelido = $_POST['apelido'];
$fac = $_POST['fac'];
$data = $_POST['data'];
$email = $_POST['email'];
mysqli_query($mysqli,"`enedpt_faculties`.`users` (`user`, `pass`, `name`, `sname`, `facid`, `nasc`, `mail`)
VALUES ('$user', '$pass', '$nome', '$apelido, '$fac', '$data', '$email')");
?>
For some reason the query is not inserting, and there are no errors on the error Log. Please Help.
You have to uae correct SQL.
An insert statement looks like this:
'INSERT INTO "table" (columnnames) VALUES (values)';
In your specific case you also have to concatenate the variables to the SQL-string:
'INSERT INTO `users` (`user`, `pass`, `name`, `sname`, `facid`, `nasc`, `mail`) VALUES ('.$user.', '.$pass.', '.$nome.', '.$apelido.', '.$fac.', '.$data.', '.$email.')'
You also can use back tics on the variables. Also you had some mistakes like missing quotes.

I am getting a Syntax Error in SQL

Having trouble submitting data to a database because of syntax error.
Database Structure
database: red_fungi
username: fungi_47
password: *******
Table Structure:
columns > type
id > int(11)
first_name > text
last_name > text
email > text
phone > text
website > text
description > text
As well as the php code:
<?php
$servername = "localhost";
$username = "fungi_47";
$password = "********";
$dbname = "red_fungi";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_POST['first_name']);
$last_name = mysqli_real_escape_string($link, $_POST['last_name']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$phone = mysqli_real_escape_string($link, $_POST['phone']);
$website = mysqli_real_escape_string($link, $_POST['website']);
$comment = mysqli_real_escape_string($link, $_POST['comment']);
$hosting = mysqli_real_escape_string($link, $_POST['hosting']);
$sql = "INSERT INTO contact (id, first_name, last_name, email, phone, website, description, hosting)
VALUES (NULL, $first_name, $last_name, $email, $phone, $website, $comment, $hosting)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
When submitting, I see that the post has been successful:
first_name=Bill&last_name=Nye&email=bill%40nye.com&phone=8888888888&website=billnyefungi.com&comment=help%20me%20make%20a%20fungi%20website&hosting=yes
but the post response shows the following error:
Error: INSERT INTO contact (id, first_name, last_name, email, phone, website, description, hosting)
VALUES (NULL, , , , , , , )You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near ' , , , , , )' at line 2
However I've checked the syntax and can't see anything wrong with it. Any ideas what's going wrong?
Your sql statement needs to look more like this:
$sql = "INSERT INTO `contact` (`id`, `first_name`, `last_name`, `email`, `phone`, `website`, `description`, `hosting`)
VALUES (NULL, '{$first_name}', '{$last_name}', '{$email}', '{$phone}', '{$website}', '{$comment}', '{$hosting}')";
The first thing I do when I have a problem like this is echo out the sql and see if there are obvious problems
and follow up on all the data validation & security points made by other users.
Your code is assuming that $_POST['XXX'] will be populated, and it isn't. Thats what all those ,,,,,,,, mean in the error.
Instead, first check if $_POST['XXX'] is created, and has a value prior to using it.
if ((isset($_POST['first_name'])) && (!empty( $_POST['first_name'])) ) {
//do query and rest of your script
} else { die('Need form input');}

How to insert a new row into table where values are variables? [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
I am trying to INSERT a new row into table using variables, like this-
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$password = $_POST['password'];
$gender = $_POST['gender'];
if($name && $surname && $email && $password){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = '_erica';
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
};
$sql = 'INSERT INTO login(id, name, surname, email, sex, password) VALUES (Null,$name,$surname,$email,$gender,$password)';
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}else{
die('All fields requiret!');
};
But it does not work, I have tried to add quotes around variables like this
$sql = 'INSERT INTO login(id, name, surname, email, sex, password) VALUES(Null,"$name","$surname","$email","$gender","$password")';
still nothing... what could I do?
Use this. Just change single quotes with double quotes
$sql = "INSERT INTO login(id, name, surname, email, sex, password) VALUES (Null,$name,$surname,$email,$gender,$password)";
Try this,
$sql = 'INSERT INTO login(id, name, surname, email, sex, password) VALUES(NULL,"'.$name.'","'.$surname.'","'.$email.'","'.$gender.'","'.$password.'")';
Or (use double quotes outside instead of single quotes)
$sql = "INSERT INTO login(id, name, surname, email, sex, password) VALUES(NULL,$name,$surname,$email,$gender,$password)";

Categories