-1

I have 4 pages, across which I need to share the same restaurant_ID. I need to be able to insert the ID into all 4 tables in the database. I am using a INSERT INTO SELECT statement to do so. But I am receiving the following error:

POSSIBLE Syntax Error (check preceding valid syntax error) unexpected: identifier 'SELECT'

POSSIBLE Syntax Error (check preceding valid syntax error) unexpected: identifier 'Resturant_ID'

POSSIBLE Syntax Error (check preceding valid syntax error) unexpected: identifier 'Rest_Dets'

This is the sql I am using:

(Original):

  INSERT INTO Product (Resturant_ID)
  SELECT Resturant_ID Rest_Dets;

(Edited):

  INSERT INTO Product (Resturant_ID)
  SELECT Resturant_ID FROM Rest_Dets;

I have also tried

  $rest_id = mysqli_real_escape_string($dbc, $_SESSION['Resturant_ID']);
  INSERT INTO Product (Resturant_ID)
  SELECT Resturant_ID FROM Rest_Dets  WHERE Resturant_ID = $rest_id ;

I have looked all over the internet and it doesn't seem like I should have a problem, the webpage is also connected successfully.

Rest_Details(The table i want to get the Restaurant_ID from)

     CREATE TABLE `Rest_Details` (
    `Resturant_ID` bigint(255) NOT NULL AUTO_INCREMENT,
    `Resturant_name` varchar(100) NOT NULL,
    `Resturant_des` varchar(200) NOT NULL,
    `Res_Address_Line_1` varchar(200) NOT NULL,
    `Res_Address_Line_2` varchar(200) DEFAULT NULL,
    `City_name` varchar(100) NOT NULL,
    `Resturant_Postcode` varchar(8) DEFAULT NULL,
    `Cat_ID` tinyint(11) NOT NULL,
    `Avg_Del` tinyint(11) NOT NULL,
    `Est_Del` tinyint(11) NOT NULL,
    `Email1` varchar(200) NOT NULL,
    `Email2` varchar(200) DEFAULT NULL,
    `Min_ord` tinyint(11) NOT NULL,
     PRIMARY KEY (`Resturant_ID`),
     UNIQUE KEY `Resturant_name` (`Resturant_name`),
     UNIQUE KEY `Resturant_ID` (`Resturant_ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

Products

   CREATE TABLE `Product` (
  `Product_Id` bigint(255) NOT NULL AUTO_INCREMENT,
  `Resturant_ID` bigint(255) NOT NULL,
  `Product_Name` varchar(100) NOT NULL,
  `Product_Desc` text NOT NULL,
  `Product_Price` decimal(65,0) NOT NULL,
  `Add_On_ID` int(11) NOT NULL,
   PRIMARY KEY (`Product_Id`),
   UNIQUE KEY `Product_Id` (`Product_Id`),
   UNIQUE KEY `Resturant_ID` (`Resturant_ID`)
   ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8
jerneva
  • 473
  • 1
  • 8
  • 25
  • Did you read the manual on it? https://dev.mysql.com/doc/refman/5.5/en/insert-select.html – Funk Forty Niner Apr 05 '16 at 18:09
  • 2
    `SELECT Resturant_ID FROM Rest_Dets` – juergen d Apr 05 '16 at 18:09
  • @juergend i am trying to both select and insert at the same time. – jerneva Apr 05 '16 at 18:15
  • Did you try the code I provided? You missed the `FROM` – juergen d Apr 05 '16 at 18:16
  • @Fred-ii- yes i have, the manual shows the query should work fine – jerneva Apr 05 '16 at 18:16
  • Taken from the manual's example `INSERT INTO tbl_temp2 (fld_id) SELECT tbl_temp1.fld_order_id FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;` you missed some stuff here. Therefore, did not using as per the manual's syntax. Edit: and should update your question with what you're now using. – Funk Forty Niner Apr 05 '16 at 18:17
  • @juergend oh sorry, i miss understood. Yes I've changed it and same error messages – jerneva Apr 05 '16 at 18:18
  • From what table do you want to select? And I doubt you get the exact same error. – juergen d Apr 05 '16 at 18:19
  • You tagged as php but no code to support the question and if you're using it correctly in a script. – Funk Forty Niner Apr 05 '16 at 18:20
  • 1
    Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Apr 05 '16 at 18:25
  • @juergend i am trying to select from Rest_Dets. It sounds odd but it is the same error. That what is confusing me even more – jerneva Apr 05 '16 at 18:27
  • @Fred-ii- i already have that exact code at the top of my webpage. – jerneva Apr 05 '16 at 18:28
  • if you're running this exactly as you posted `$rest_id = mysqli_real_escape_string($dbc, $_SESSION['Resturant_ID']); INSERT INTO Product (Resturant_ID) SELECT Resturant_ID FROM Rest_Dets WHERE Resturant_ID = $rest_id ;` in your script (if you're using a script), will fail. If you're using that code in phpmyadmin, then that too will fail you. I for one don't understand because I don't know how you're using that exactly, sorry. – Funk Forty Niner Apr 05 '16 at 18:30
  • You need to put the query in quotes and execute it, don't just put it in your code. – juergen d Apr 05 '16 at 18:31
  • Here give this a whirl `$rest_id = mysqli_real_escape_string($dbc, $_SESSION['Resturant_ID']); $query = ($dbc, "INSERT INTO Product (Resturant_ID) SELECT Resturant_ID FROM Rest_Dets WHERE Resturant_ID = $rest_id");` and to be executed from PHP and not in phpmyadmin assuming a `mysqli_` successful connection and the session array contains an integer value and not a string. – Funk Forty Niner Apr 05 '16 at 18:35
  • Can you show us the schema of your tables, it's not like on one column you've got VARCHAR and another column you've got INT or something? – Martin Apr 05 '16 at 18:37
  • ping me if anything I said made sense. I closed this tab. – Funk Forty Niner Apr 05 '16 at 18:45
  • can you post the *actual* code you use to query the database? It occurs to me that you are using PHP functions in the first line of your code and then raw SQL in the second line and perhaps you don't realise you need to wap your SQL inside a PHP function such as `mysqli_query()` etc? – Martin Apr 05 '16 at 19:21
  • @Martin hi martin. sorry i am very new to this so maybe i am making very silly mistakes, the code above is my code. i would usually start a query $sql = (""); but i am not sure if thats what i have to do here – jerneva Apr 05 '16 at 19:31
  • @jerneva I think I've found your issue, please read my edited answer. – Martin Apr 05 '16 at 19:48

1 Answers1

0

As referenced by Fred-ii- you can read https://dev.mysql.com/doc/refman/5.5/en/insert-select.html which will give you exactly the answer you are looking for:

INSERT INTO Product (Resturant_ID) SELECT Resturant_ID FROM Rest_Dets WHERE <condition>;

Additional

WHERE Resturant_ID = $rest_id ;

Your string (if it is a string) should be encased in single quotes:

 WHERE Resturant_ID = '$rest_id' ;

And properly concatenated, regardless of if it is a string or a number:

 WHERE Resturant_ID = '".$rest_id."' ;

As an aside you can achieve exactly what you want by simply doing:

INSERT INTO Product(Resturant_ID) VALUES ('".$rest_id."')

(You've also spelt Restaurant wrong. )


Solution

The solution from your comments I think is this:

Your current PHP code is something like:

<?php
...
$rest_id = mysqli_real_escape_string($dbc, $_SESSION['Resturant_ID']);
  INSERT INTO Product (Resturant_ID)
  SELECT Resturant_ID FROM Rest_Dets  WHERE Resturant_ID = $rest_id ;

You are typing SQL commands directly into PHP script, which is giving you the errors you state in your question, so what you should actually be doing is running the SQL queries through an interface manager (in this case PHP functions that setup and execute MySQL commands to the server).

Please read http://php.net/manual/en/book.mysqli.php which will give you a good guide on using MySQLi functions in PHP script to communicate with your database.

Your reference to mysqli_real_escape_from_newyork_string is the sort of functions I'm on about, you should be able to clearly see from the below code what (I think) you've missed:

<?php
/** Error reporting: **/
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

/** Set up the mysqli connection: **/
    $dbc = mysqli_connect("localhost", "my_user", "my_password", "my_db");

/** Next clean your input **/ 
    $rest_id = mysqli_real_escape_string($dbc, $_SESSION['Resturant_ID']);

/** Next run the query **/
    mysqli_query($dbc, "INSERT INTO Product (Resturant_ID)
  SELECT Resturant_ID FROM Rest_Dets WHERE Resturant_ID = ".$rest_id."") 
    or die(__LINE__.": yourSQL Error: ".mysqli_error($dbc));

/** It is good practise for beginniners to add the or die to the end of the code to 
display MySQLi errors. NOT FOR PUBLIC/LIVE WEBSITES **/

/** Finally a success statment! **/
    print "If you can read this then your query worked ok!!";

Copy and paste the above code into your page and think about the logic and adjust fitting it around your current code but this I think is the key you're missing. Please read up the following articles:

NOTES:

  • Remember if your $rest_id is a string then it needs to be encased in single quotes inside the mysqli_query()
  • localhost may not be local (on the same machine the PHP is running on) and may be on another server.
  • ALWAYS clean any input as thoroughly as possible. Never trust any user input, ever.
  • NEVER use MySQL_ functions, they are no longer supported. Use MySQLi (as here) or PDO.
Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • my apologies i should have also stated that I've tired ... WHERE Resturant_ID = $rest_id. And defined the $rest_id before hand – jerneva Apr 05 '16 at 18:23
  • I have a feeling that the OP isn't telling us the whole story as they did originally tag as php and the error may very well lie in there. – Funk Forty Niner Apr 05 '16 at 18:23
  • I'd take another look at the guy's edit. Underneath that *"I have also tried..."*. The only php I see there is the first line, then followed by code that's usually executed in phpmyadmin's SQL section, not PHP. – Funk Forty Niner Apr 05 '16 at 18:29
  • Thank you. I have tired this, but it seems to me that the issue is the INSERT INTO Product, as this is underlined and a new error has appeared. I am puling out my hair – jerneva Apr 05 '16 at 18:55
  • @jerneva [show us your mysql table schema](http://stackoverflow.com/questions/1498777/how-do-i-show-the-schema-of-a-table-in-a-mysql-database) – Martin Apr 05 '16 at 19:14
  • Hi. I will show you now. the error messages are now gone, just not printing the message – jerneva Apr 05 '16 at 20:11
  • @jerneva ok, has the data appeared in your `Products` table? – Martin Apr 05 '16 at 20:12
  • @jerneva I had made a tiny update I had misplaced a `"` mark in the code so that may cause you an error if you didn't fix it yourself. I have edited and updated my answer code, above. – Martin Apr 05 '16 at 20:13
  • hi. Yeah i saw that and fixed it. i have added my tables as you asked – jerneva Apr 05 '16 at 20:18
  • @jerneva I also added in error reporting at the top of the code, too. Thanks for the table schema, your table appears fine, however you have made the Restaurant_ID a unique Key so that means if it's trying to write a duplicate key then MySQL will fail silently. – Martin Apr 05 '16 at 20:20
  • @jerneva re my comment above, this post may be worth your time: http://stackoverflow.com/questions/8449540/php-detect-mysql-update-insertion-failure-due-to-violated-unique-constraint – Martin Apr 05 '16 at 20:22
  • since I've undated myphpadmin if i don't make the primary key a unique key the tables do not insert nor update, before hand i didn't not have to do this. It is very strange thats the reason it is like that. but i will drop the unique key and see what happens – jerneva Apr 05 '16 at 20:26
  • 1
    @jerneva well it should be unique, yes, but if you happen to be trying to add `restaurant_ID = 12` twice then this will be a reason that it fails but doesn't specifically tell PHP that it failed. If you repeat the script do the PHP error reports tell you anything? – Martin Apr 05 '16 at 20:28
  • I mean on your `Products` table you have a `resturant_ID`unique key which is not the primary key, but which means that the product table can only hold 1 row per restaurant Id. I think this index should not be unique. @jerneva – Martin Apr 05 '16 at 20:30
  • @jerneva in my original comment I explained it badly. – Martin Apr 05 '16 at 20:35
  • thank you for all your help and explanations and the links. It is much appreciated. I am still unable to retrieve the restaurant_ID from the rest_dets page and insert into the product db i will continue to read – jerneva Apr 05 '16 at 21:06
  • @jerneva did you set `session_start()` at the top of the page? Use the error reporting on PHP to help you track down at which point in the process the value goes missing. – Martin Apr 05 '16 at 21:12
  • yeah i have session_start() , require ('call database here') i already had error handling on my page. When i run the file it is blank apart from the database connected echo. no error messages – jerneva Apr 05 '16 at 21:17
  • where is the session value for `rest_id` set? @jerneva – Martin Apr 05 '16 at 21:22
  • i just changed the rest id to $rest_id = mysqli_real_escape_string($dbc, $_POST['Resturant_ID']); changed the error handler slightly and i can now see the success message, finally. but i have not checked the db yet to see if its worked – jerneva Apr 05 '16 at 21:38
  • unfortunately not. I have no idea where i have gone wrong to be honest. All i need is for the correct ID to be used on multiple pages, and insert into maybe 3 or 4 tables from the original table. Who knew it would be this complicated. – jerneva Apr 05 '16 at 22:04
  • as I've said, you need to track the value through each time you use it, define it, print it out, follow the value and then establish where and when it goes missing, also keep checking the error logs on both PHP and MySQL. – Martin Apr 05 '16 at 22:06
  • @jerneva use something like http://pastebin.com/ and copy and paste your actual PHP code into the there and I can take a look at each page for you and give you feedback. – Martin Apr 05 '16 at 22:07
  • hah, I like to find out why sh!t goes wrong. And I just finished a piece of my own work so I'm feeling positive :) – Martin Apr 05 '16 at 22:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108336/discussion-between-martin-and-jerneva). – Martin Apr 05 '16 at 22:35