Aidah Husnun Fauziah Aidah Husnun Fauziah - 2 months ago 7
PHP Question

My Query Update error on php

i have query update on my php file. but when i run this file, my query error.but when i already on trial in a database query is not an error

<?php
session_start();

$conn = pg_connect("host=localhost port=5432 dbname=alc user=postgres password=postgres");

$training_id= "SELECT m.training_mst_id FROM tbl_user u, training_mst m WHERE u.user_id = m.trainer_id AND m.start_traning >= now() AND m.end_traning <= now() AND m.trainer_id = ".$_SESSION['user_id'];
$result = pg_query($conn,$training_id);
$query = "UPDATE training_mst set status='TRUE' where training_mst_id= ".$training_id;
$result2 = pg_query($conn,$query);

?>


my error :


Warning: pg_query(): Query failed: ERROR: syntax error at or near "SELECT" LINE 1: ...ning_mst set status='TRUE' where training_mst_id= SELECT m.t... ^ in D:\xampp\htdocs\FeedbackALC\sistemuser\open_feedback.php on line 9

Answer

As @Geral Schneider pointed out, the variable you are using as a value in the second sql statement is the original sql query rather than the result. You need to retrieve the value you need from the initial recordset - the following is not tested but might give an indication as to how to proceed.

<?php
    session_start();

    $conn = pg_connect("host=localhost port=5432 dbname=alc user=postgres password=postgres");

    $training_id= "SELECT m.training_mst_id FROM tbl_user u, training_mst m WHERE u.user_id = m.trainer_id AND m.start_traning >= now() AND m.end_traning <= now() AND m.trainer_id = ".$_SESSION['user_id'];
    $result = pg_query($conn,$training_id);

    if( $result ){

        $data=pg_fetch_object( $result );
        $id=$data->training_mst_id;

        $query = "UPDATE training_mst set status='TRUE' where training_mst_id= ".$id;
        $result2 = pg_query($conn,$query);
    }

?>