MYSQL Insert syntax Error - php

I have two codes to put data into database but it is generating error, check out the code below.
$email = "example#hotmail.com"; //email
$pass = "helloworld"; //password
$fname = "Example"; //first name
$lname = "Man"; //last name
$birth = "2012-2-1"; //birthday
$gender = "male"; //gender
$site_prefix = "my_"; //table prefix
THIS CODE DOESNT WORK AND OUTPUT AN ERROR
$sql = "
INSERT INTO `{$site_prefix}login` (`email`,`pass`)
VALUES ('$email','$pass');
INSERT INTO `{$site_prefix}users` (`fname`,`lname`,`birthday`,`gender`)
VALUES ('$fname','$lname','$birth','$gender')";
mysql_query($sql,$con) or die(mysql_error());
ERROR
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 'INSERT INTO my_users (fname,lname,birthday,gender) VALUES ('Example','Ma' at line 2
THIS CODE WORK NORMALLY
$sql = "INSERT INTO `{$site_prefix}login` (`email`,`pass`) VALUES ('$email','$pass');";
$sql1 = "INSERT INTO `{$site_prefix}users` (`fname`,`lname`,`birthday`,`gender`) VALUES ('$fname','$lname','$birth','$gender')";
mysql_query($sql,$con) or die(mysql_error());
mysql_query($sql1,$con) or die(mysql_error());

mysql_query cannot process multiple statements in one query.
From the docs:
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier
Use mysqli (with mysqli_multi_query) if you need this functionality.

Related

insert data from a table and insert into another table in different database

I have 2 database that link together. I need to retrieve data from that table and insert those column into a table in different database based on their Unique id number.
<?php
$handle = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_query("USE shop",$handle);
$query = "SELECT ModelCode,Class FROM shopfloor_pro WHERE CommNo = '0985560712'";
$result = mysql_query($query);
while ($data = mysql_fetch_object($result)){
$variable1 = $data->ModelCode;
$variable2 = $data->Class;
mysql_query("USE vt",$handle);
$sql = "INSERT INTO track SET
t_model_code = '$variable1',
t_class = '$variable2' WHERE t_comm_no = '0985560712'";
if (!mysql_query($sql)) {
echo '<p>Error adding data into database: ' . mysql_error() . '</p>';
}
mysql_query("USE paintshop",$handle);
}
?>
this is the data that i want to retrieve
this is where i want to put the data
When i run the code it shows
"Error adding data into database: 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 'WHERE t_comm_no = '0985560712'' at line 3"
You can most likely do this in a single query - but as pointed out the mysql api has been deprecated a long time ago and totally removed from PHP 7+.
To do the query in a single operation you might try like this:
insert into `vt`.`track` (`t_model_code`,`t_class` )
select `ModelCode`,`Class` from `shop`.`shopfloor_pro` where `CommNo`='0985560712'

SQL syntax which sending me an Error

I have a Mysql Database named user. Here is a picture:
I want to change the Username of the user "dodlo.rg" programmatically.
Actually, I have the PHP-Version 7.1. And this is a part of my PHPCode:
EDITED CODE:
$newName= $_POST["changeT"];
$userId = $_POST["userId"];
$db = mysqli_connect("trolö", "trolö", "trolö123", "trolö")
$sql = "UPDATE user SET username = '$newName' WHERE user_id = '$userId'";
$query = mysqli_query($db, $sql);
$response["successU"] = true;
But I get the Error: "You gave an Error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT * FROM user' at line 1"
Thanks in advance.
The problem lies in 2 parts.
Firstly, since this column is a varchar field it needs to be inside quotes else it produces an sql error.
Secondly the SELECT statement just after is not valid, but i guess it was a copy/paste error.
Therefore your working code should be:
$newName= $_POST["changeT"];
$db = mysqli_connect("trolö", "trolö", "trolö123", "trolö")
$sql = "UPDATE user SET username = '".addslashes($newName)."' WHERE username = 'dodlo.rg'";
$query = mysqli_query($db, $sql);
$response["successU"] = true;
Also, please consider using your primary keys on your where statement rather a varchar field, as it'll improve speed when more complex queries. (eg. where user_id = 35 instead of where username = 'dodlo.rg' ).
Lastly, but quite important this code might be vulnerable to sql injections. You need to use prepared statements.
You have to convert this query into two parts
$sql1 = "UPDATE user SET username = $newName WHERE username = 'dodlo.rg'";
$sql2 = "SELECT * FROM user";

SQL syntax error. check corresponds to MYSQL server

I try to do a form which can insert data into database. After I insert a dummy data the is come out.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
This error are make me in trouble. My database are not inserted any record
<?php
$db = "assignment";
$table = "column";
$conn = mysqli_connect("localhost","root","");
mysqli_select_db($conn,$db);
$Title = $_POST['title'];
$Author = $_POST['author'];
$Country = $_POST['country'];
$Date = $_POST['date'];
$Abstract = $_POST['abstract'];
$Problem = $_POST['rproblem'];
$Aim = $_POST['raim'];
$Objectives = $_POST['robjective'];
$Type = $_POST['rstudies'];
if(isset($_POST['rmethod'])){
$method = implode(",",$_POST['rmethod']);
}else{
$method = "";
}
$sql = "INSERT INTO '$table' (title,author,country,date,abstract,rproblem,raim,robjective,rstudies,rmethod)
VALUES ('$Title','$Author,'$Country','$Date','$Abstract','$Problem','$Aim','$Objectives','$Type','$method')";
mysqli_query($conn,$sql);
if (!mysqli_query($conn,$sql)){
die('Error: ' . mysqli_error($conn));
}else{
echo "Data Added";
}
mysqli_close($conn);
?>
You've set your $table variable inside single quotes while using a reserved word, column for your table name $table = "column";
Use backticks around it, like so:
INSERT INTO `$table`
either do that or give your table another name.
Read the manual about table and column identifiers
You also have a quote missing here '$Author, so do '$Author',
Also, you can remove mysqli_query($conn,$sql); since you're already using
if (!mysqli_query($conn,$sql))
Footnotes:
Your present code is open to SQL injection. I strongly suggest that you use prepared statements, or PDO with prepared statements.
Try this
$sql = "INSERT INTO $table (title,author,country,date,abstract,rproblem,raim,robjective,rstudies,rmethod)
VALUES ('$Title','$Author','$Country','$Date','$Abstract','$Problem','$Aim','$Objectives','$Type','$method')";
The table name or column name must enclose them in back-ticks (`) and not in single quotes or double quotes. Otherwise don't wrap them.Simply try like above.And if you are using reserved keywords as table name or column name then you must enclose them in back-ticks.And its better not to use any reserve keyword.So if you can change the name then it will be the best choice.You are using two reserve keywords in your query. Your table name and date column. Both are keywords
You can check my answer here for more
Follow other answer you also missing ' on $author
$sql = "INSERT INTO `$table` (title,author,country,date,abstract,rproblem,raim,robjective,rstudies,rmethod)
VALUES ('$Title','$Author','$Country','$Date','$Abstract','$Problem','$Aim','$Objectives','$Type','$method')";
Also better use to replace
mysqli_query($conn,$sql);
if (!mysqli_query($conn,$sql)){
to
$result = mysqli_query($conn,$sql);
if (!$result){
else your query will execute two time.

PHP insert query gives syntax error but still writes into database

A query executes and writes into a database table and the field data is fetched and displayed in a WHILE loop so basically it works but I get a php error :
Error Inserting!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 \'1\' At Line 1
With line 1 being
<?php
I have tried playing around with commas and colons but I cannot get rid of the error. This is the query.
$Link = mysql_connect($Host, $User, $Password);
$user = $_SESSION['UserName'];
$query = mysql_query("INSERT INTO films VALUES ('0', '".($user)."','".($formValue["subject"])."',NOW(),'".($usercomments)."','".($formValue["rating"])."','action')");
if(mysql_query ($query, $Link)){
$message = "Thank you for your comments";
header("Location: films.php?message=$message");
}else{
$message = "Error Inserting!" . mysql_error();
header("Location: films.php?message=$message");
$query = "INSERT INTO films VALUES ('0', '$user','$formValue[subject]',NOW(),'$usercomments','$formValue[rating]','action')";
This may simplify the code and solve your error.

Renaming table name

I am trying to rename a table's name from a specific database. I have tried with both of the query given below, but it shows the same error message. I can't understand my mistakes.
The query
1st one :
<?php
$id = $_POST['id'];
$department = $_POST['department'];
$dept_id = $_POST['dept_id'];
$olddept_id = $_SESSION['olddept_id'];
if(isset($_POST['submit']))
{
$order = "UPDATE department SET department='$_POST[department]', dept_id='$_POST[dept_id]' WHERE id='$_POST[id]'";
mysql_query($order) or die (mysql_error());
mysql_query("RENAME TABLE $olddept_id TO $dept_id;") or die (mysql_error());
}
and
2nd one :
<?php
$id = $_POST['id'];
$department = $_POST['department'];
$dept_id = $_POST['dept_id'];
$olddept_id = $_SESSION['olddept_id'];
if(isset($_POST['submit']))
{
$order = "UPDATE department SET department='$_POST[department]', dept_id='$_POST[dept_id]' WHERE id='$_POST[id]'";
mysql_query($order) or die (mysql_error());
mysql_query("ALTER TABLE $olddept_id RENAME $dept_id;") or die (mysql_error());
}
The error message is :
"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 'TO CSEE' at line 1"
The table names which i want to edit, are also stored in a table, named "department". This is done successfully, but the table doesn't renaming.
-thank you
Show the exact SQL that's generated by those queries. I'm guessing you're using a reserved word for the original table name, which means you'd have to escape it with backticks:
RENAME reservedword TO CSEE

Categories