Azizul Alam Prottoy
Azizul Alam Prottoy

Reputation: 1

set the variable as constant

the SQL gets the latest entry from the table ''users" . and $check is the variable where it's stored , i want the code run in a way , if the $check get's a value , it remains constant , even if ''users'' get any other latest entry ..

here is my code

    <?php
    include 'db.php';
    
    
    $email = isset($_POST['email']);
    $fname = isset($_POST['fname']);
    $user_id = isset($_POST['user_id']);
    
    $sql = "SELECT * FROM users WHERE user_id=(SELECT max(user_id) FROM users)";
    
    
    $result = $conn->query($sql);
    
    while ($row = $result->fetch_assoc()) {   $check=$row['user_id'] ; }
    
    ?>

Upvotes: 0

Views: 39

Answers (1)

Newish
Newish

Reputation: 92

A little tidy up here. The query you wish to use, is to get the last added user, and there is no need for a sub-select, as shown in the query below. Here we use some fake variables, which will be derived from you validation of the POSTed variables. You will not need to loop over the results with foreach, as the results are directly in the $res variable.

<?php

$dbh = new PDO("mysql:host=localhost;dbname=my_database", 'oldboot', 'bigkev123');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// validated variables from POST 
$email = '[email protected]';
$fname = 'kevvo';
$user_id = 2;

$stmt = $dbh->prepare("SELECT * FROM users ORDER BY id DESC LIMIT 1");
$stmt->execute();
$res = $stmt->fetch();

echo $res['name']."\n";
$check = $res['user_id'];
?>

Upvotes: 1

Related Questions