mysqli预处理语句字符串被NULL替换

时间:2014-12-29 18:11:14

标签: php sql mysqli

我在使用以下代码时遇到了一些问题。

<?php

$mysql = new mysqli("localhost", "user", "pw", "db");
unset($_POST);

$_POST = array();

$q =    "INSERT INTO shk_marketplace_requests (".
            "village_id, Grafschaft, player_id, village_name, ".
            "Holz, Stein, Eisen, Pech, ".
            "_Ae_pfel, K_ae_se, Fleisch, Brot, Gem_ue_se, Fisch, Bier, ".
            "Wild, M_oe_bel, Metallwaren, Gew_ae_nder, Wein, Salz, Gew_ue_rze, Seide, ".
            "B_oe_gen, Piken, R_ue_stungen, Schwerter, Katapulte".
            ") VALUES (".
            "?, IFNULL((SELECT id FROM shk_marketplace_grafschaft WHERE name=?),0), ".
            "IFNULL((SELECT id FROM shk_marketplace_player WHERE name=?),0), ?, ".
            "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";

#mysqli_report(MYSQLI_REPORT_ALL);
if (mysqli_connect_errno()) {
    print(mysqli_connect_error());
    exit(0);
}

$stmt = $mysql->prepare($q);
if (!$stmt) {
    exit(0);
}
$stmt->bind_param("isssiiiiiiiiiiiiiiiiiiiiiiii", 
    $id, $grafschaft, 
    $_POST["player"], $_POST["name"],
    $_POST["Holz"], $_POST["Stein"], $_POST["Eisen"], $_POST["Pech"],
    $_POST["_Ae_pfel"], $_POST["K_ae_se"], $_POST["Fleisch"], $_POST["Brot"], $_POST["Gem_ue_se"], $_POST["Fisch"],
    $_POST["Bier"],
    $_POST["Wild"], $_POST["M_oe_bel"], $_POST["Metallwaren"], $_POST["Gew_ae_nder"],
    $_POST["Wein"], $_POST["Salz"], $_POST["Gew_ue_rze"], $_POST["Seide"],
    $_POST["B_oe_gen"], $_POST["Piken"], $_POST["R_ue_stungen"], $_POST["Schwerter"], $_POST["Katapulte"]);

$data = json_decode(file_get_contents("requests.dat"));

$search = array("_ae_", "_oe_", "_ue_", "_Ae_", "_Oe_", "_Ue_", "_DD_", "_KM_", "_US_");
$replace = array("ä", "ö", "ü", "Ä", "Ö", "Ü", ":", ",", "_");

$resources = array(     "Holz", "Stein", "Eisen", "Pech",
                        "_Ae_pfel", "K_ae_se", "Fleisch", "Brot", "Gem_ue_se", "Fisch",
                        "Bier",
                        "Wild", "M_oe_bel", "Metallwaren", "Gew_ae_nder", "Wein", "Salz", "Gew_ue_rze", "Seide",
                        "B_oe_gen", "Piken", "R_ue_stungen", "Schwerter", "Katapulte");

foreach ($data as $grafschaft => $val) {
    foreach ($val as $id => $req) {
        $_POST = array();
        foreach ($req as $k => $v) {
            $_POST[$k] = str_replace($search, $replace, $v);
        }
        foreach ($resources as $k => $v) {
            if (!isset($_POST[$v]) || !is_numeric($_POST[$v]) || $_POST[$v] < 0) {
                $_POST[$v] = 0;
            }
        }
        if ($stmt->execute()) {
            print("INSERT of '".$id."' sucessful!\n");
        } else {
            print("ERROR: INSERT of '".$id."' failed!\n".$stmt->error." (".$stmt->errno.")\n");
            #var_dump($_POST["name"]);
        }
    }
}

$stmt->close();
$mysql->close();






?>

脚本应该从文件中读取数据并将其写入数据库。 示例文件内容:

{"Chemnitz":{"99853":{"name":"01","player":"King_Eik","Eisen":"50000","Pech":"50000"}}}

现在我得到了这个结果:

ERROR: INSERT of '99853' failed!
Column 'village_name' cannot be null (1048)

预计将“插入'99853'成功!”结果。 所以现在我的问题是为什么PHP或SQL将文件中的名称字段转换为数据库中的NULL。 $ _POST [“name”]在执行语句之前和/或之后不是NULL。

任何人都可以帮我吗?

我已经使用此代码进行了大量测试,但总是出现问题... 我在SO或其他地方也没有发现这样的问题:(

2 个答案:

答案 0 :(得分:0)

解决方案非常简单。

只需交换

$_POST = array();

foreach ($resources as $k => $v) {
    $_POST[$v] = 0;
}

此后,数据将被插入数据库。我仍然不知道为什么它以某种方式删除了这个名字,但它不再重要:)

答案 1 :(得分:0)

这是一条关于如何在没有所有引用的情况下开发查询的评论 问题,并有一些易于阅读的东西。

如果您将查询保留在文件中,则可以对其进行编辑,测试等。

然后使用&#39; file_get_contents&#39;加载到PHP&#39; $ sql&#39;变量

示例代码:

/*
 * Developing  SQL Queries
 *
 * If you keep them in a file then you can easily edit the SQL Query.
 *
 * Run in in an 'SQL' IDE and also easily run it in PHP by using
 * 'file_get_contents' to load it into a PHP $variable.
 *
 * Advantages.
 *  It is easy to see what the structure is.
 *  It is easy to change and run.
 *  No issues with 'quotes'.
 *  It is easy 'post' on 'SO' and it be understandable :-/
 */

$sqlFilename = 'outstanding.sql';

$sql = file_get_contents($sqlFilename);

echo '<pre>';
echo $sql;
echo '</pre>';

// Do what you want with the SQL...

示例SQL文件...

select event.*, source.*
from event
     join event_source
        on event_source.event_id = event.id        
        and event.state in ('waitingForData', 'newRequest') 
           join source 
                on  source.id = event_source.source_id
                and source.sourceid = ?