fires more then one query in mysql_query - mysql

$updateStock = "UPDATE opening SET qtyUsed = 1000 WHERE openingId = 1 ; UPDATE purchase SET qtyUsed = qtyUsed + 25 WHERE purchaseId = 1";
$updateAllStock = mysql_query($updateStock);
This Gives Error : Data Not InsertedYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE purchase SET qtyUsed = qtyUsed + ' at line 3

mysql_query can only execute one query at a time. You need to execute mysql_query twice.
Instead of the mysql extension use mysqli. With mysqli you can execute multiple queries. http://www.php.net/manual/fr/mysqli.multi-query.php

You can use mysqli_query()
It allows multiple statements. Be careful.

You are sending two queries, so you have to query the databsae twice. There is the support for multiple queries in mysqli, but I strongly advise against using this feature, because you raise the probability of SQL injection attacks.
$updateStock = "UPDATE opening SET qtyUsed = 1000 WHERE openingId = 1";
$updatePurchase = "UPDATE purchase SET qtyUsed = qtyUsed + 25 WHERE purchaseId = 1";
$updateAllStock = mysql_query($updateStock);
$updateAllPurchase = mysql_query($updatePurchase);

Related

How do you pass and recive an output parameter from vb.net to a mysql

Dim Def_Command_MySQL1 As MySql.Data.MySqlClient.MySqlCommand
Def_Command_MySQL1.Parameters.Clear()
Def_Command_MySQL1.CommandText = "SET #OutID = 10;"
or
Def_Command_MySQL1.CommandText = "UPDATE ID_Max1 SET IdMax = IdMax + 1 , #OutID = IdMax + 1 where TypeID>1"
Def_Command_MySQL1.Parameters.Add(New SqlParameter("#OutID", SqlDbType.Int)).Value = 41
Def_Command_MySQL1.Parameters("#OutID").Direction = ParameterDirection.Output
Def_Command_MySQL1.ExecuteNonQuery()
IDMax_Update = Def_Command_MySQL1.Parameters("#OutID").Value
Err.Description "You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near '41 = 10' at line 1" String
or
Error = You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near '1=IdMax + 1 where TypeID>1' at line 1
First you set AllowUserVariables on True(Default is False in your connection string see https://dev.mysql.com/doc/connector-net/en/connector-net-6-10-connection-options.html
second get rid of the parameter code, because you replace mysql user defined variables #OutID with an actual value, which results in your error message.
With
SET #OutID = 10;"
You set in mysql a user defined variable #OutID to the numer 10.
But in general, following code add two parameters to a insert query
Def_Command_MySQL1.CommandText = "INSERT INTO myTable (myColumn1, myColumn2) VALUES (#C1, #C2)"
Def_Command_MySQL1.Parameters.AddWithValue("#C1", 1)
Def_Command_MySQL1.Parameters.AddWithValue("#C2", 2)
Def_Command_MySQL1.ExecuteNonQuery()

SQL Syntax Error in PHP code - adding together two values

I keep getting this syntax error in my MySQL code within a PHP file. I'm simply trying to increment/add to the value already in the table with this time variable. If anyone could help me out I'd greatly appreciate it.
PHP:
$sql = "UPDATE Aircraft
SET MaintenanceFlightTime = (MaintenanceFlightTime + $MaintenanceDuration),
WHERE AircraftID = $AircraftID";
Error:
UPDATE Aircraft SET MaintenanceFlightTime = (MaintenanceFlightTime + 00:10:00), WHERE AircraftID = 8
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':10:00), WHERE AircraftID = 8' at line 2
You cannot just add a string like '01:01:01' to a time column but you can use ADDTIME()
$MaintenanceDuration = '00:10:00';
$sql = "UPDATE Aircraft
SET MaintenanceFlightTime = ADDTIME(MaintenanceFlightTime, '$MaintenanceDuration')
WHERE AircraftID = $AircraftID";

JDBC update database

i have a form and onClick listener i want to update some data on my database exception says that i have a syntax error but when i execute query at mysql console it works here is my code all variables are checked
String temp = itemList.getModel().getElementAt(itemList.getSelectedIndex()).toString();
PreparedStatement pt = supplies.con.prepareStatement("update prods set pquant = pquant + ? where pname = ?");
pt.setInt(1, Integer.parseInt(empsalary.getText()));
pt.setString(2, temp);
supplies.pst.executeQuery(temp);
Error
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 'DVD Verdatim' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
at buy$1.actionPerformed(buy.java:62)
As the compiler mentioned, there is indeed a Syntax error in your SQL query. Try:
String temp = itemList.getModel().getElementAt(itemList.getSelectedIndex()).toString();
PreparedStatement pt = supplies.con.prepareStatement("update prods set pquant = #0 + 21" + "'variableForPquant'" "where pname = #1" + "'variableForPname'");
pt.setInt(1, Integer.parseInt(empsalary.getText()));
pt.setString(2, temp);
supplies.pst.executeQuery(temp);
You need to use quotes when you are using variables in your SQL query. I believe it is either
"'variableName'" or '"variableName'"
I found my error i declare a variable pt and i execute the query from the statement that is empty pst i change to pt.executeUpdate(); and its ok now

Is it possible to insert value in table based on parameter value

String sql = ("insert into registration(pic) values(?) where email='"+Email+"' ");
i get error :error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where email='yyy#ymail.com'' at line 1
You have to use UPDATE query to pass it like
String sql = "UPDATE registration SET pic = ? WHERE email = '" + Email + "'";
Syntax for UPDATE query is
UPDATE table_name SET column_name = value;
Insert query format should be,
"insert into tablename (columnname) values(coulmnvalue)"
OR
"update registration set pic='' where email='"+Email+"'";
Yes. that is impossible.
Either you want:
insert into registration(pic) values(?)
Which will give you a new row;
Or you want an UPDATE:
UPDATE registration SET pic = ?
WHERE email = <EMAILYouWant>
Which will update an existing row where email = the record with the email you want to update the pic column.

MYSQL update clause comparing the WHERE from 2 tables

$produpd = "UPDATE tblnavpc SET tblnavpc.ChildName = tblnav.NavName " .
"FROM tblnav WHERE tblnavpc.CID = tblnav.NavID";
This is the error I get"
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 'FROM tblnav
WHERE tblnavpc.CID = tblnav.NavID' at
line 1
I know keys aren't named greatly but I just trying to fix this one problem, I didn't name the tables.
You don't have a FROM clause in an update:
UPDATE tblnavpc
INNER JOIN tblnav ON tblnavpc.CID = tblnav.NavID
SET tblnavpc.ChildName = tblnav.NavName
"From" cannot be used with update statements.
Should be
$produpd = "UPDATE tblnavpc SET tblnavpc.ChildName = tblnav.NavName " .
"WHERE tblnavpc.CID = tblnav.NavID";