所以,我很确定我的查询应该没问题。但是我做了一个 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();