Задать вопрос
  • Не могу решить проблему с синтаксисом sql?

    rozhnev
    @rozhnev Куратор тега PHP
    Fullstack programmer, DBA, медленно, дорого
    Можно так:
    <?php
    
    require_once 'vendor/connect.php';
    
    $login = mysqli_real_escape_string($connect, $_SESSION["login"]);
    
    $query = mysqli_query($connect, "SELECT * FROM `users` WHERE login = '$login'");
    
    $balance = mysqli_fetch_assoc($query);
    
    echo 'Balance:' . $balance['balance']. PHP_EOL;
    
    if(isset($_POST['click'])) {
    	mysqli_query($connect, "UPDATE `users` SET `balance`=`balance`+0.001 WHERE login = '$login'");
    	echo 'New balance:' . ($balance['balance'] + 0.001) . PHP_EOL;
    }
    
    ?>


    Share PHP code online

    А лучше так:

    <?php
    
    require_once 'vendor/connect.php';
    
    /* prepare statement */
    $stmt = $connect->prepare("SELECT `balance` FROM `users` WHERE login = ? ");
    
    /* bind parameters for login */
    $stmt->bind_param("s", $_SESSION["login"]);
    
    /* execute query */
    $stmt->execute();
    
    $stmt->bind_result($balance);
    
    /* fetch value */
    $stmt->fetch();
    
    /* free result to next use */
    $stmt->free_result();
    
    echo 'Balance:' . $balance . PHP_EOL;
    
    if (isset($_POST['click'])) {
    
    	/* prepare statement */
    	$stmt = $connect->prepare("UPDATE `users` SET `balance`=`balance`+0.001 WHERE login = ? ");
    
    	/* bind parameters for login */
    	$stmt->bind_param("s", $_SESSION["login"]);
    
    	/* execute query */
    	$stmt->execute();
    
    	echo 'New balance:' . ($balance['balance'] + 0.001) . PHP_EOL;
    }
    
    ?>


    Test PHP code online
    Ответ написан
    Комментировать