动态mysqli_stmt_bind_param

时间:2019-12-30 22:12:46

标签: php mysqli dynamic get prepared-statement

我想通过调用PHP脚本来更改表行的数据。那应该是动态的。从理论上讲,每列都有一个GET变量,但并非必须所有人。只有'method'和'id'是强制性的。

这是我尝试执行的操作:

require 'inc.auth.php';
require 'inc.dbh.php';

if (!isset($_GET['method']))
    die("No method defined");

if ($_GET['method'] == 'update') {
    if (!isset($_GET['id']))
        die ("Missing player id [id=...]");

    $id = intval($_GET['id']);

    $format = '';
    $params = array();
    $sets = array();

    if (isset($_GET['g'])) {
        $format = $format.'i';
        $params[] = intval($_GET['g']);
        $sets[] = "Games = ?";
    }

    if (isset($_GET['w'])) {
        $format = $format.'i';
        $params[] = intval($_GET['w']);
        $sets[] = "Wins = ?";
    }

    if (isset($_GET['l'])) {
        $format = $format.'i';
        $params[] = intval($_GET['l']);
        $sets[] = "Losses = ?";
    }

    if (isset($_GET['h'])) {
        $format = $format.'i';
        $params[] = intval($_GET['h']);
        $sets[] = "Honor = ?";
    }

    if (isset($_GET['ws'])) {
        $format = $format.'i';
        $params[] = intval($_GET['ws']);
        $sets[] = "WinStreak = ?";
    }

    if (isset($_GET['ls'])) {
        $format = $format.'i';
        $params[] = intval($_GET['ls']);
        $sets[] = "LosingStreak = ?";
    }

    if (count($params) == 0)
        die("");

    $params[] = $id;

    $sql = 'UPDATE Player SET '.implode(', ', $sets).' WHERE ID = ?';
    $stmt = mysqli_stmt_init($connection);

    if (!mysqli_stmt_prepare($stmt, $sql))
        die ("MySQL Prepare Error");

    if (!mysqli_stmt_bind_param($stmt, $format, $params))
        die ("MySQL Bind Error");

    print_r($stmt->fullQuery);
    mysqli_stmt_execute($stmt);

    mysqli_stmt_close($stmt);
    mysqli_close($connection);
}

但是方法mysqli_stmt_bind_param()总是返回false。

有帮助吗?

0 个答案:

没有答案