所以,我很确定我的查询应该没问题。但我做了一个var_dump并且确实返回false,所以我不确定为什么。有人能告诉我我的查询或诸如此类的错误,以及如何解决它?我很确定这不是我的自定义函数db_connect,它以前工作过。可能是什么问题?
这是我的代码:
require_once("includes/helpers.php");
session_start();
$dbh = connect_db('mysql:host=localhost;dbname=project1', 'jharvard', 'crimson');
if (!isset($_POST['quantity'])) { //go back if incomplete
redirect('search');
}
if (!isset($_SESSION['email'])) {
redirect('index');
}
elseif (!isset($_SESSION['row'])) {
redirect('stocks');
}
$amount = htmlspecialchars($_POST['quantity']);
$value = htmlspecialchars($_SESSION['row']);
$email = htmlspecialchars($_SESSION['email']);
$name = htmlspecialchars($_SESSION['q']);
if (!filter_var($amount, FILTER_VALIDATE_INT) || $amount < 0) {
redirect('search');
}
$dbh->beginTransaction();
$result = $dbh->query("SELECT balance FROM users WHERE email = $email"); //get balance
$result1 = $dbh->query("SELECT id FROM users WHERE email = $email"); //get id
var_dump($result);
$bal = $result->fetchAll(PDO::FETCH_NUM);
$id = $result1->fetch(PDO::FETCH_ASSOC);
//if ($bal < $amount * $value) {
// redirect ('search');
//}
$bal = $bal - $amount * $value; //subtracting from total
$dbh->exec("INSERT INTO stocks (name, shares, id) VALUES ('$name', '$amount', '$id')");
$dbh->commit();
答案 0 :(得分:1)
您需要将$ email变量放在引号内:
$result = $dbh->query("SELECT balance FROM users WHERE email = '$email'"); //get balance
$result1 = $dbh->query("SELECT id FROM users WHERE email = '$email'");
或连接:
$result = $dbh->query("SELECT balance FROM users WHERE email = '".$email."'");
$result1 = $dbh->query("SELECT id FROM users WHERE email = '".$email."'");