error with php file - insert query [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I'm having problems with insert details to mysql server.
This is the code (a simple one):
<?php
//Input posted data.
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$Date = $_POST["Date"];
$Mail = $_POST["Mail"];
$Pass = $_POST["Pass"];
// Create connection
$conn = mysqli_connect('localhost','root',"");
//Check if the connection was opened, if not prompt the error to the page.
if ($conn)
{
die('Could not connect: ' . mysql_error());
}
//Select the data base.
mysqli_select_db("club",$conn);
//Set the character set to utf-8 to allow hebrew.
mysqli_query("SET NAMES 'utf8'");
//SQL query - user Details
$sql = "INSERT INTO 'customers' (Fname, Lname, Mail, Date, Pass)
VALUES('$Fname','$Lname','$Mail','$Date','$Pass');
//Run SQL query
$results = mysqli_query($query) or die (mysql_error());
//Close the SQL connection.
mysqli_close($conn);
?>
I'm getting this error:
Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\Contact.php on line 36 <--- The last line
Can really use your help.
thanks in advance,
Jason.

$results=mysqli_query($sql);
you have given $query instead of $sql

Related

Data does not show up in the database when sent through php [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 4 years ago.
I'm new to php and dealing with databases. I have accomplished sending data from one arduino sensor to the database using PHP and XAMPP. My problem is sending data from multiple sensors.
The PHP code in file "write_data_w2"
<?php
$dbusername = "w123";
$server = "localhost";
$dbconnect = mysqli_connect($server, $dbusername);
$dbselect = mysqli_select_db($dbconnect,"weather1");
$sql = "INSERT INTO weather1.weather (temperature, humidity, rain) VALUES ('".$_GET["temperature"].",".$_GET["humidity"].",".$_GET["rain"]."')";
mysqli_query($dbconnect, $sql);
?>
I'm not using a password for the user "w123".
I wanted to check everything and tried inserting some made up data through browser with
"http://localhost/write_data_w2.php?temperature=32&humidity=45&rain=N"
and nothing happens, no warnings, no errors, no data. The database stays empty.
The database is named "weather1" consists of 1 table named "weather" and 5 columns named: "id", "time", "temperature", "humidity", "rain".
Solved
As a user suggested I added the line:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
which displayed some errors that I then solved.
I also had to modify "$sql" a bit:
$sql = "INSERT INTO weather1.weather (temperature, humidity, rain) VALUES ('".$_GET['temperature']."', '".$_GET['humidity']."', '".$_GET['rain']."')";
Just a suggestion
You should avoid the user of var or $GET/POST value directly in sql you are at risk for sql injection anyway you should check for error adding a $mysqli_error meggage ..
$dbusername = "w123";
$server = "localhost";
$dbconnect = mysqli_connect($server, $dbusername);
$dbselect = mysqli_select_db($dbconnect,"weather1");
$sql = "INSERT INTO weather1.weather (temperature, humidity, rain) VALUES ('".$_GET["temperature"].",".$_GET["humidity"].",".$_GET["rain"]."')";
mysqli_query($dbconnect, $sql);
// for check the erro try add
if (!$mysqli_query(dbconnect, $sql)) {
printf("Errormessage: %s\n", $mysqli_error);
}
?>

Parse error: syntax error, unexpected '$query' (T_VARIABLE) in c [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Can I mix MySQL APIs in PHP?
(4 answers)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 4 years ago.
I am trying to upload information to a database. The page I created is a registration page where users can type in their email username and password. The below code is the database connection and upload code I have written. But I keep getting the above error. Can someone tell me what I am missing, please?
<?php
$db_host=
$db_username=
$db_pass=
$db_name=
$connectToServer =mysqli_query($host,$db_username,$db_pass) or die("server problem");
$selectDb =mysqli_select_db($connectToServer,$db_name) or die("database not found");
if(isset($_POST['submit'])) {
$username=$_POST['username'];
$email=$_POST['eml'];
$password =$_POST['password'];
if(!empty($username)&&!empty($email)&&!empty($password)) {
$username = striplashes($username);
$email=striplashes($email);
$password=striplashes($password);
$username = mysql_real_escape_string($connectToServer,$username);
$selectTable = "SELECT * FROM user_info WHERE username='$username'"
$query = mysqli_query($connectToServer,$selectTable);
$insert = "INSERT INTO user_info (username, email, password) VALUES ($username, $eml, $password)"
$mquery = mysqli_query($connectToServer,,$insert);
if ($mquery) {
session_start();
$_SESSION['login_user'] =$username ;
header("Location ; profile.php");
}
}
else {
echo <script>('please enter details')</script>;
header("Location: register.html");
}
}
?>
You are missing a semi-colon on line 22:
$selectTable = "SELECT * FROM user_info WHERE username='$username'"; // <- here
Same for line 24.
You have an extra comma on line 25...
And you are missing double-quotes on line 34...

How to use Var Dump to update SQL record using $_POST [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I want to send input data from a form on publish.php to updateCopy.php that will then update the "postCopy" column on my SQL database.
Here's my code so far:
publish.php
<form action="\.\.\updateCopy.php" method="post" id="newCopy">
<input type="text" name="postCopy">
<input type="submit">
</form>
updateCopy.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "main";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE posts SET postCopy= var_dump ($_POST["postCopy"]); WHERE id=123456789";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
When I attempt to run this process I get the following error:
Parse error: syntax error, unexpected '"', expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
Is anyone able to tell me how I can effectivley use var_dump ($_POST["postCopy"]); to include the updated postCopy info and then update my SQL db?
Don't know why are you trying to use var_dump to execute the SQL statement, that makes no sense, and then a ; too that will terminate the sql, if you talk about the error
$sql ="UPDATE posts SET postCopy= var_dump ($_POST["postCopy"]); WHERE id=123456789";
change it to
$sql ="UPDATE posts SET postCopy= '".$_POST['postCopy']."' WHERE id=123456789";
and the error will go away.
Note : This is not the optimal way and an open invite to sql injection, you should use prepared statements and parameterized queries either use PDO or MYSQLI

I am struggling to link my PHPMyAdmin MySQL database to [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am trying to link my MySQL database to my PHP code. My friend has the same code as me and is able to connect their database to their PHP code. I end up getting a error code and do not know where I am going wrong.
This is the code that I am using
<?php
$username="root";
$password="password";
$database="account";
$connect = mysql_connect('localhost', $username, $password);
//$db-select = mysql_select_db($database,$connect) or die("Unable to select database");
$user = $_GET['user'];
$pass = $_GET['pass'];
if(!$connect) {
die('eror');
}
$db = my_sql_select_db("account", $connect)
mysql_query($db, "INSERT INTO 'account', 'tbl_account' (Username, Password) VALUES ('$user', '$pass')";);
mysql_close($connect);
?>
This is the outcome:
Parse error: syntax error, unexpected 'mysql_query' (T_STRING) in C:\Apache24\htdocs\PHP\index.php on line 11
add semicolon at the end of first line and remove the extra semicolon in second line
$db = my_sql_select_db("account", $connect);
mysql_query($db,"INSERT INTO 'account','tbl_account' (Username,Password) VALUES ('$user','$pass')");

PHP Parse error: syntax error, unexpected '$_GET' (T_VARIABLE) [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
First off, yes I have done research and have seen tons of posts like this one. I see the post this is supposed to be a duplicate of but it was not helpful. I am very new with this and do not know how to apply their results to mine.
I'm getting this result when running:
Parse error: syntax error, unexpected '$_GET' (T_VARIABLE) in /storage/ssd4/269/2113269/public_html/updateuser.php on line 12
Here is my script:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE Users ". "SET Status = '"$_GET["status"]"' ".
"WHERE Username = '"$_GET["username"]"'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Thank you a bunch for taking a look. I might be missing a semi-colon somewhere but I've looked over the code for a while. Please let me know!
You have to concatenate string using .
$sql = "UPDATE Users ". "SET Status = '".$_GET["status"]."' ".
"WHERE Username = '".$_GET["username"]."'";
You need to concatenate string and variable using dot(.) properly like this
$sql = "UPDATE Users
SET Status = '".$_GET["status"]."'
WHERE Username = '".$_GET["username"]."'";
This is because you end and start the statement with " before and after the the GET statement declaration; but haven't put the concatenation . in between the " and GET.
"SELETCT tb FROM db WHERE field = '".GET ['something']."'";
It's also a good habit to wrap the two GET in a IF statement and run the full code if bot Get has some value. Reduce the unnecessar SQL and PHP execution.

Categories