【问题标题】:Update / echo dont work [closed]更新/回声不起作用[关闭]
【发布时间】:2016-08-02 16:51:39
【问题描述】:

我不明白为什么这不起作用:

$sql = "UPDATE tbl_users SET balance = balance - 250 WHERE userID = <?php echo $row['userID']; ?>";

我与 PDO 合作

这里有什么问题?需要帮助

session_start();
require_once 'class.user.php';
$user_home = new USER();

if(!$user_home->is_logged_in()) {
    $user_home->redirect('index.php');
}

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

$servername = "test.de.mysql";
$username = "test";
$password = "test";
$dbname = "test";


try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "UPDATE tbl_users SET balance = balance - 250 WHERE userID = <?php echo $row['userID']; ?>";

    // Prepare statement
    $stmt = $conn->prepare($sql);

    // execute the query
    $stmt->execute();

    // echo a message to say the UPDATE succeeded
    echo $stmt->rowCount() . " records UPDATED successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;

【问题讨论】:

  • 嗯....你已经在 PHP 中 &lt;?php echo $row['userID']; ?&gt; 并且应该得到一个错误;解析错误。
  • 我没有出错!你可以在这里看到它。 kevinshop.de/test.1php
  • 那是因为您没有通过 PHP 方法进行检查。 php.net/manual/en/function.error-reporting.php
  • @Fred-ii- 我该怎么办?

标签: php mysql sql pdo


【解决方案1】:
$sql = "UPDATE tbl_users SET balance = balance - 250 WHERE userID = " . intval($row['userID']);

我还要提到你选择了所有的用户字段,而你只需要用户 ID,这是不必要的内存浪费。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多