-1

I do this multi_query and get an error:

$date = date('Y-m-d H:i:s');

$searchLimit = $_POST['limitSearch'];        

$inputDate = $_POST['dateSearch'];

$searchDate = date("Y-m-d H:i:s", strtotime($inputDate));

$topic = $_POST['topicSearch'];

foreach($_POST['check_list_search'] as $selected){   

$journal = $selected;

$sql = "SELECT tbl.* FROM (SELECT date, section, title, teaser, href, image FROM scrap WHERE (date > '$searchDate' AND journal = '$journal' AND (LOWER(teaser)LIKE LOWER('%{$topic}%') OR LOWER(title) LIKE LOWER('%{$topic}%'))) ORDER BY date DESC LIMIT $searchLimit) as tbl GROUP BY tbl.title";

$sql .= "INSERT INTO search_loggedin (date, journal, inputdate, searchlimit, topic) VALUES ('$date', '$journal', '$searchDate', '$searchLimit', '$topic')";

$dbc->multi_query($sql);

$res = $dbc->store_result();

echo $dbc->error;

Undeclared variable: search_loggedin

I just created the table search_loggedin but I did the exact same query on another older table and it worked.

Why do I have this error?

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
ben
  • 75
  • 7
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Masivuye Cokile May 05 '17 at 15:05

1 Answers1

1

Your two statements need to be separated by a semicolon:

$sql = "SELECT tbl.* FROM (SELECT date, section, title, teaser, href, image FROM scrap WHERE (date > '$searchDate' AND journal = '$journal' AND (LOWER(teaser)LIKE LOWER('%{$topic}%') OR LOWER(title) LIKE LOWER('%{$topic}%'))) ORDER BY date DESC LIMIT $searchLimit) as tbl GROUP BY tbl.title;";

Although TBH - you shouldn't be using multi query, it's better to do this one thing at a time!

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Okay I am going to check. Thanks a lot. I need to insert the choice of the user and display the relevant thing, all at once. Isn't multi_query the most suitable for that? – ben May 05 '17 at 15:43
  • 'All at once' isn't quite the case. You need a script to insert data and select some other data. This is already done as two separate statements, so split it down to two separate calls to `query`. It makes it clearer as to what statement is doing what and also any errors are more localised. – Nigel Ren May 05 '17 at 16:04