You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP如何通过函数正确向数据库表插入数据?

Hey there! Let's figure out why your PHP insert isn't writing to the database. I’ve spotted a few key issues in your code that are probably causing the problem:

1. Hardcoded table name in the insert method

Look at your insert function—you’re hardcoding users in the SQL statement instead of using the $table parameter you passed in! While this doesn’t break your current call (since you’re inserting into users anyway), it’s a bug that’ll bite you later if you try to insert into other tables. Fix it by replacing the hardcoded table name with the parameter:

$sql = "INSERT INTO `{$table}` (`" . implode('`, `', $keys) . "`) VALUES (".$values.")";

Adding backticks around the table name also protects you from conflicts with MySQL keywords.

2. Your query method tries to fetch results for non-SELECT queries

When you run an INSERT statement, there’s no result set to fetch—but your query method calls fetchAll(PDO::FETCH_OBJ) no matter what kind of SQL you run. While this might not directly fail the INSERT, it’s unnecessary and could cause unexpected behavior later. Update the query method to only fetch results when you’re running a SELECT:

if($this->_query->execute()) {
    // Only fetch results for SELECT statements
    if (stripos(trim($sql), 'SELECT') === 0) {
        $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
    }
    $this->_count = $this->_query->rowCount();
    echo 'success<br>';
} else {
    $this->_error = true;
}

3. PDO is set to silent error mode (so you can’t see what’s wrong)

Right now, your DB class uses PDO’s default silent error mode. That means if there’s a SQL syntax error, missing field, or permission issue, you won’t get a clear error message to debug. Fix this by adding an attribute to your PDO connection in the constructor:

private function __construct() {
    try {
        $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname='.Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
        // Turn on exceptions for errors
        $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo 'Connected<br>';
    } catch(PDOException $e) {
        die($e->getMessage());
    }
}

This will throw a detailed exception if anything goes wrong with your SQL or connection, which makes debugging way easier.

Your delete method uses DELETE * FROM—that’s invalid SQL. The * isn’t needed for DELETE statements. Update the action call to:

public function delete($table, $where) {
    return $this->action('DELETE', $table, $where);
}

Quick debugging steps to check next:

  • After adding the PDO error mode, run your code again—if there’s a specific error (like a missing column, wrong table name, or permission issue), it’ll show up immediately.
  • Double-check that your users table has exactly the columns you’re inserting: username, password, salt—and that their data types match what you’re sending (e.g., username is a VARCHAR long enough for 'Dale').
  • Confirm your database user has INSERT permissions on the users table.

Once you fix these issues, your insert should work as expected!

内容的提问来源于stack exchange,提问作者Ace Thanks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:01:48