1
$query=mysqli_query($conn,"INSERT INTO bus_info(bus_id,route_num,school_name) values('$BusNum','$RouteNum','$SchoolName'); INSERT INTO bus_loc(bus_id,lat,lon) values ((SELECT bus_id from bus_info where bus_info.bus_id='$BusNum'),'$latitude','$longitude')"); 

PHP

$BusNum = $_POST["BusNum"];
$SchoolName = $_POST["SchoolName"];
$RouteNum = $_POST["RouteNum"];
$latitude = $_POST["lat"];
$longitude = $_POST["lng"];

Database is connected i.e. returned true.enter code here

Fails with : Error sending data:

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 bus_loc(bus_id,lat,lon) values ((SELECT bus_id from bus_info where b at line 1

3 Answers 3

2

From: http://php.net/manual/de/mysqli.query.php#87203

mysqli::query() can only execute one SQL statement.

Use mysqli::multi_query() when you want to run multiple SQL statements within one query.

How to use mysqli_multi_query: http://php.net/manual/de/mysqli.multi-query.php

0
1

For better understanding split query into two parts and use them like :-

$query =   mysqli_query($conn,"INSERT INTO bus_info (bus_id,route_num,school_name) values('$BusNum','$RouteNum','$SchoolName')"); 

$query2 =  mysqli_query($conn,"INSERT INTO bus_loc (bus_id,lat,lon) values ((Select bus_id from `bus_info` where bus_id = '$BusNum'),'$latitude','$longitude')");
1
  • Thats a better and cleaner way! Thanks mate!
    – Tanuj
    Oct 5, 2015 at 10:05
1

This query is missing a where clause condition

SELECT bus_id from bus_info where b

change it to like:

SELECT bus_id from bus_info where b = 'something'

but you should not execute two queries like this but execute this first save the result in a variable and then execute the next one like

 $query =   SELECT bus_id from bus_info where b = 'something'
 $saved = $mysqli_query($yourconnection, $query);
 $row = mysqli_fetch_assoc();
 $fetched = row['columnnamehere'];

and then

INSERT INTO bus_loc(bus_id,lat,lon) values ('$fetched');
2
  • I added the 'WHERE' clause where bus_info.bus_id='$BusNum'. Trying your variable method.
    – Tanuj
    Oct 5, 2015 at 9:58
  • The "where b" reported in the error is just the MySQL server truncating the string at that point for the purposes of reporting an error; if you check the PHP core in the question, you can see that the query has a proper where clause.
    – daiscog
    Oct 5, 2015 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.