MySQL'致命错误'

时间:2014-06-20 09:12:41

标签: php mysql

我的代码中有错误,我一直在谷歌搜索并试图找出问题所在。据我所知,执行我的sql代码是一个问题(围绕变量$ so)。谁能帮助我?

致命错误:在第15行的...中的非对象上调用成员函数execute()

    <?php
    $dbhost = "";
    $dbuser = "";
    $dbpass = "";
    $dbname = "";

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

    if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $so = $con->prepare("SELECT * FROM besteloverzicht");
    $so->execute();

    $result = $so->get_result();

    echo "<form name='overzicht' method='post'>";
    echo "<table align='center' border='2'>
    <tr>
            <th>Ordernr</th>
            <th>Klantnaam</th>
            <th>Productnaam</th>
            <th>ProductID</th>
            <th>Status</th>
            <th>Verwijderen</th>
    </tr>";

    while($row = $result->fetch_assoc()) {

    $ordernr = $row['ordernr'];
    $klantnaam = $row['klantnaam'];
    $productnaam = $row['productnaam'];
    $productid = $row['productid'];
    $status = $row['status'];

    echo "<tr>";
    echo "<td width='150px'>" . $ordernr . "</td>";
    echo "<td width='150px'>" . $klantnaam . "</td>";
    echo "<td width='300px'>" . $productnaam . "</td>";
    echo "<td width='100px'>" . $productid . "</td>";

    echo "<td width='200px'><select name='status[$ordernr]'>
     <option>" . $status . "</option>";
      if($row['status']  != "Niet besteld")
      echo "<option>Niet besteld</option>";
      if($row['status']  != "Besteld")
      echo "<option>Besteld</option>";
      if($row['status']  != "Onderweg naar hoofdlocatie")
      echo "<option>Onderweg naar hoofdlocatie</option>";
      if($row['status']  != "Onderweg naar vestiging")
      echo "<option>Onderweg naar vestiging</option>";
      if($row['status']  != "Ontvangen")
      echo "<option>Ontvangen</option>";
    echo "</select></td>";

    echo "<td align='center' width='50px'><input name='checkbox[]' id='checkbox[]' type='checkbox' value='$ordernr'></td>";

    echo "</tr>";
    }

    echo "<tr>";
    echo "<td></td><td></td><td></td><td></td>";
    echo "<td><input type='submit' name='wijzigen' value='Wijzigingen Opslaan'/></td>";
    echo "<td><input type='submit' name='verwijderen' value='Verwijderen'/></td>";
    echo "</tr>";
    echo "</table>";
    echo "</form>";

    $statuses = $_POST['status'];
    $delete = $_POST['delete'];
    $del_id = $_POST['checkbox'];

    if (isset($_POST['wijzigen'])) {
        foreach($statuses as $ordernr => $status)
        {
            if($status != "")
                $dbupdate = "UPDATE overzicht SET status='$status' WHERE ordernr='$ordernr'";
                $query = mysqli_query($con,$dbupdate);

            header("refresh: 0;");
        }
    }

    if (isset($_POST['verwijderen'])) {
            foreach($del_id as $value){
            $dbdelete = "DELETE FROM overzicht WHERE ordernr='".$value."'";
            $query = mysqli_query($con,$dbdelete);
        }
        header("refresh: 0;");
    }

    mysqli_close($con);
    ?>

3 个答案:

答案 0 :(得分:0)

表格besteloverzicht可能不存在? 请替换

$so = $con->prepare("SELECT * FROM besteloverzicht");

$so = $con->prepare("SELECT * FROM besteloverzicht") OR die(mysqli_error());

这可以让你更好地了解出了什么问题。

答案 1 :(得分:0)

包装您的prepare语句,以便在失败时产生错误

if (!($so = $con->prepare("SELECT * FROM besteloverzicht"))) {
    echo "Prepare failed: (" . $con->errno . ") " . $con->error;
}

这可能会为您提供更好的见解

答案 2 :(得分:0)

你可以使用mysqli_prepare($ con,&#39; SELECT * FROM besteloverzicht&#39;);