PHP致命错误 - 在资源上调用成员函数prepare()

时间:2017-03-31 09:32:24

标签: php sql

我已经查看了其他问题,但我看不到我和其他问题之间的联系,所以我想知道我是否可以就我出错的地方得到一些建议。

我读过关于不混用API的内容,但我不确定我是不是?

submitNoteText.php

?php include 'connectionDetails.php'; ?>

<?php


if (isset($_POST['noteid1'], $_POST['notetext1'])) 
{
    var_dump($_POST['notetext1']);

    $stmt = $conn->prepare("UPDATE Notes SET Note = ? WHERE NoteID = ?");
    $stmt->bind_param("si", $notetext2, $noteid2);

    $noteid2 = $_POST['noteid1'];
    $notetext2 = $_POST['notetext1'];
    $stmt->execute();

    $stmt->close();
}
else
{
    if (isset($_POST['notetext1'])) {
        var_dump($notetext2);
    }
    else
    {
        echo "Test";
    }

}


?>

connectionDetails.php

<?php
$myServer = "test ip";
$connectionInfo = array('Database' => 'DiscoverThePlanet', 'UID' => 'Test', 'PWD' => 'Test');

//connection to the database
$conn = sqlsrv_connect($myServer, $connectionInfo)
  or die("Couldn't connect to SQL Server on $myServer"); 

//Test connection to server
// if ($conn) 
// {
//     echo "connection successful";    # code...
// }

?>

错误在 submitNoteText.php 中,并且是这一行:

$stmt = $conn->prepare("UPDATE Notes SET Note = ? WHERE NoteID = ?");

2 个答案:

答案 0 :(得分:2)

在声明变量之前调用变量

$stmt = $conn->prepare("UPDATE Notes SET Note = ? WHERE NoteID = ?");
$stmt->bind_param("si", $notetext2, $noteid2);

$noteid2 = $_POST['noteid1'];
$notetext2 = $_POST['notetext1'];

将订单切换为

$noteid2 = $_POST['noteid1'];
$notetext2 = $_POST['notetext1'];

$stmt = $conn->prepare("UPDATE Notes SET Note = ? WHERE NoteID = ?");
$stmt->bind_param("si", $notetext2, $noteid2);

好的,我刚注意到:

你正在打电话:

$conn->prepare()

但$ conn不是PDO的实例。你正在混合api。根据文档更改$ conn: http://php.net/manual/pl/pdo.construct.php

答案 1 :(得分:2)

sqlsrv_* API没有prepare()方法。您正在混合数据库API。

使用PDO或使用sqlsrv_query()