V
V
Vladislav Rudakevich2020-09-27 03:47:49
MySQL
Vladislav Rudakevich, 2020-09-27 03:47:49

Why is the variable not being passed from PHP to MySQL?

I have a database from CMS called DLE, it contains the dle_users table, which contains the name and user_balance lines, the user_balance contains the user's balance and I check if it is higher than the price, if so, I subtract the price from the balance and send this value to the database. But for some reason, the value in the database does not change, but if you pass not a variable, but a text or a number, everything changes. I ask you not to beat the code or blunts for a crooked code, I have been living these php and database for 2 weeks ..

The problem is that I cannot check the name string for the presence of the $user variable, I just get the error
Fatal error: Uncaught mysqli_sql_exception: Unknown column 'Hexagon' in 'where clause'

PHP code:

<?php
$dblink = mysqli_connect('localhost', 'hexagon', 'password', 'hex-server'); // Соединяемся с базой
//$user = $member_id['name'];
$user = 'Hexagon';
$allow_to_buy = 0;
$current_monets = 0;
$price = 1;
$sql = mysqli_query($dblink, 'SELECT `name`, `user_balance` FROM `dle_users`');
  while ($result = mysqli_fetch_array($sql)) 
  {
    if ($result['name'] == $user)
    {
    echo $result['name'];
    $current_monets = $result['user_balance'];
    echo $current_monets;
    }
  }
  if ($current_monets >= $price)
    {
      $allow_to_buy = 1;
      echo 'Покупка совершена!';
      $current_monets-=$price;
      echo $current_monets;
      mysqli_query($dblink, 'UPDATE `dle_users` SET `user_balance` = '.$current_monets.' WHERE `name` = '.$user.'');
      $sql2 = mysqli_query($dblink, 'SELECT `name`, `user_balance` FROM `dle_users`');
      while ($result = mysqli_fetch_array($sql2)) {
        echo '<br>';
        echo $result['name'];
        echo $result['user_balance'];
      }
    }
    else
      echo 'Недостаточно монет';
?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladislav Rudakevich, 2020-09-27
@Hexagon14

As a result, I took information from the user_id field from the db and simply assigned it to the $id variable, and when I changed the information in the database, I did this:

mysqli_query($dblink, 'UPDATE `dle_users` SET `user_balance` = '.$current_monets.' WHERE `user_id` = '.$id.'');

For some reason it didn't want to compare the text..

V
vism, 2020-09-27
@vism

read about the difference between single and double quotes.

V
Vitsliputsli, 2020-09-27
@Vitsliputsli

mysqli_query($dblink, 'UPDATE `dle_users` SET `user_balance` = '.$current_monets.' WHERE `name` = '.$user.'');

Fatal error: Uncaught mysqli_sql_exception: Unknown column 'Hexagon' in 'where clause'

In SQL, text values ​​must be enclosed in quotation marks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question