matijap
matijap

Reputation: 59

Pass $_SESSION to database

I'm having trouble passing a variable to a database using an insert into statement. Im trying to execute a statement and also insert the $sesvariable into my database.

My code:

 session_start();
 $ses = $_SESSION['uid'];`

My SQL statement:

    $sql = "INSERT INTO narocila (nameNarocilo, priceNarocilo, nameNarocnik) VALUES
          ((SELECT nameTaco, priceTaco FROM taco
          WHERE idTaco = $idTaco), $ses)";  
    $result = mysqli_query($conn, $sql);

what am i missing?

Upvotes: 0

Views: 53

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

assuming that $idTaco and $ses are two integer values you could use an insert select and a prepared statement (for avoid sqlinjection ) and manage the assigmento of param values

assuming that $idTaco and $ses | $_SESSION['uid'] are two integer values you could use an insert select and a prepared statement (for avoid sqlinjection ) and manage the assigmento of param values

$sql = "INSERT INTO narocila (nameNarocilo, priceNarocilo, nameNarocnik) 
        select nameTaco, priceTaco, ? 
        from taco
        WHERE idTaco = ?;" ;

$stmt = mysqli_prepare($conn, $sql ) ;
mysqli_stmt_bind_param($stmt, "ii",$_SESSION['uid'], $idTaco);
mysqli_stmt_execute($stmt);

or directly

Upvotes: 1

Related Questions