V
V
Vladimir Golub2015-05-21 12:53:56
PHP
Vladimir Golub, 2015-05-21 12:53:56

How to combine INSERT with SELECT?

How to fix this script? I don't understand how to combine them?

if($_POST['message'] != ""){

    $id_client = $_SESSION['id_client'];
    $text = $_POST['message'];
    $type = "2";
    $data = date("Y-m-d H:i:s");
    $status = "0";

    $query = "INSERT INTO messages (id_dialog, text, type, data, status) SELECT
 id_dialog, $text, $type, $data, $status
  FROM dialogs WHERE id_client = '$id_client'";

    $result = db_connection($query);

    exit;
}

The script goes to the server.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Mokhov, 2015-05-21
@RazerVG

before executing the query, execute
and try to duplicate the query in any MySQL client, it will immediately become clear where the error is

X
xtalen, 2015-05-21
@xom9lk

There is no logic in the line SELECT id_dialog, $text, $type, $data, $status. You are trying to get a column with a name in a variable, for example $data. Each time it will be different values. It's easy to guess that you don't have a column for every millisecond of time))
It may be more correct to SELECT id_dialog, text, type, data, status - but no one will tell you what table you have, what this script should do, etc.

U
urmi tilek uulu, 2015-05-28
@urma

Write better two separate requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question