INSERT INTO mysqli返回非对象

时间:2014-11-15 15:38:05

标签: php mysql mysqli insert

EDITED!

一直在查看有类似问题的帖子,但似乎无法找到适用于我的情况的答案 - 希望你们能提供帮助。

更新:根据输入更新了我的代码 - 谢谢!而且我看到了进步,但是:

现在,只有在Call to a member function bind_param() on a non-object - 循环内调用INSERT INTO时,我才会收到错误while

$ stmtTjekFindesOevelse,$ stmtFindOevelsesID和$ stmtGemNyVaegtOgRepsSet可以自行工作(当在代码中手动输入变量时)仅当在while循环内时,$ stmtGemNyVaegtOgRepsSet失败,并显示以上错误消息。

$ stmtFindOevelsesID将始终返回一个整数(id)。如果没有这样的ID,if ($stmtTjekFindesOevelse->num_rows() > 0)将会发现它,并且代码将无法运行。

猜测while循环存在问题 - 只是不知道在这种情况下使用哪种替代方案......

    $navn = 'Brystpres';
    $vaegt = 10;
    $reps = 11;
    $antalGangeUdfoert = 11;

    global $mysqli;

        $stmtTjekFindesOevelse = $mysqli->prepare("SELECT * FROM oevelser WHERE navn=?");
        $stmtTjekFindesOevelse->bind_param("s", $navn);
        $stmtTjekFindesOevelse->execute();
        $stmtTjekFindesOevelse->store_result();

if ($stmtTjekFindesOevelse->num_rows() > 0)   
{

    $stmtFindOevelsesID = $mysqli->prepare("SELECT id FROM oevelser WHERE navn=?");
    $stmtFindOevelsesID->bind_param("s", $navn);
    $stmtFindOevelsesID->execute();
    $stmtFindOevelsesID->bind_result($oevelsesID);

while ($stmtFindOevelsesID->fetch()) {


$stmtGemNyVaegtOgRepsSet = $mysqli->prepare("INSERT INTO vaegtogreps (oevelsesreference, vaegt, reps, antalgangeudfoert) VALUES (?, ?, ?, ?)");

    $stmtGemNyVaegtOgRepsSet->bind_param("idii", $oevelsesID, $vaegt, $reps, $antalGangeUdfoert);

    $stmtGemNyVaegtOgRepsSet->execute();
}
}

3 个答案:

答案 0 :(得分:3)

您的列使用了错误的identifiers

('oevelsesreference', 'vaegt', 'reps', 'antalgangeudfoert')

删除引号

(oevelsesreference, vaegt, reps, antalgangeudfoert)

或在列名称周围使用反引号:

(`oevelsesreference`, `vaegt`, `reps`, `antalgangeudfoert`)

对您的查询使用错误检查会发出信号。

变化:

$stmtGemNyVaegtOgRepsSet->execute(); 

为:

if(!$stmtGemNyVaegtOgRepsSet->execute()){
trigger_error("There was an error....".$mysqli->error, E_USER_WARNING);
}

以获取错误的原因。


修改

我相信我找到了解决方案,这对我有用,它在" vaegtogreps"中插入了以下内容:表

oevelsesreference   vaegt   reps    antalgangeudfoert
       1             10      11            11

您需要使用store_result()作为第二个SELECT。

只需向其添加$stmtFindOevelsesID->store_result();

$stmtFindOevelsesID = $mysqli->prepare("SELECT id FROM oevelser WHERE navn=?");
$stmtFindOevelsesID->bind_param("s", $navn);
$stmtFindOevelsesID->execute();
$stmtFindOevelsesID->bind_result($oevelsesID);

// added and working
$stmtFindOevelsesID->store_result();

答案 1 :(得分:2)

问题是你的插入声明:

INSERT INTO vaegtogreps ('oevelsesreference', 'vaegt', 'reps', 'antalgangeudfoert')
                         ^                 ^  ^     ^  ^    ^  ^                 ^

列的引号是反引号而不是单引号:

`column1`, `column2` // backticks

所以要么省略它们要么使用反引号:

INSERT INTO vaegtogreps (`oevelsesreference`, `vaegt`, `reps`, `antalgangeudfoert`) VALUES (?, ?, ?, ?)

答案 2 :(得分:0)

关于你得到的错误:
    Fatal error: Call to a member function bind_param() on a non-object

    Trying to get property of non-object
看来你没有正确地设置你的mysqli连接。

尝试这样做,如PHP Doc中所述:

$mysqli = new mysqli("dbhost", "dbuser", "dbpass", "dbname");  

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();  
}