Fatal error: Call to a member function prepare() o

2020-02-02 01:41发布

问题:

this is how I have to build the transmitters infomation on to a php file to send to login page when it is

<form action="http://,,,.dk/activate_updater.php" method="post" name="formular" onsubmit="return validerform ()">
                                 <table border="0">
                                     <tr>
                                         <td id="tb-w_a"><p>Kategori</p></td>
                                         <td>:</td>
                                         <td>
                                         <select name="kategori">
                                         <?php
                                         $query = "SELECT id_katogori, navn FROM kategori";
                                         $result = $mysqli->query($query);
                                         while(list($id_katogori, $navn) = $result->fetch_row())
                                         {
                                             echo "<option value=\"$id_katogori\">$navn</option>";  
                                         }
                                         ?>
                                         </select>
                                         </td>
                                     </tr>
                                     <tr>
                                         <td id="tb-w_a"><p>Djnavn</p></td>
                                         <td>:</td>
                                         <td><input type="text" name="djnavn"></td>
                                     </tr>
                                     <tr>
                                         <td id="tb-w_a"><p>Facebook</p></td>
                                         <td>:</td>
                                         <td><input type="text" name="facebook"></td>
                                     </tr>
                                     <tr>
                                         <td id="tb-w_a"><p>Pris</p></td>
                                         <td>:</td>
                                         <td><input type="text" name="pris"></td>
                                     </tr>
                                     <tr>
                                         <td id="tb-w_a"><p>Booking Email</p></td>
                                         <td>:</td>
                                         <td><input type="email" name="booking"></td>
                                     </tr>
                                     <tr>
                                         <td id="tb-w_a"><p>Mobil</p></td>
                                         <td>:</td>
                                         <td><input type="text" name="mobil"></td>
                                     </tr>
                                     <tr>
                                         <td id="tb-w_a"><p>Upload Profil</p></td>
                                         <td>:</td>
                                         <td><input type="file" name="profilbillede" /></td>
                                     </tr>
                                 </table>
                             <textarea name="profiltekst" style="width:500px; height:170px;"></textarea><br />
                             <input type="submit" value="Godkend brugere" name="godkendt-brugere">
                         </form>

so the new page file is like this where there are errors on the page ..

<?php
     if($stmt = $mysqli->prepare('UPDATE `brugere` SET `rank`=2, `katogori`=?, `djnavn`=?, `profilbillede`=?, `profiltekst`=?, `facebook`=?, `pris`=?, `booking`=?, `mobil`=? WHERE `code`=?'))
     {

         $stmt->bind_param('iiiiiiiis', $katogori, $djnavn, $profilbillede, $profiltekst, $facebook, $pris, $booking, $mobil, $g_code);
         //fra input ting ting..
         $katogori = $_POST["kategori"];
         $djnavn = $_POST["djnavn"];
         $profilbillede = $_POST["profilbillede"];
         $profiltekst = $_POST["profiltekst"];
         $facebook = $_POST["facebook"];
         $pris = $_POST["pris"];
         $booking = $_POST["booking"];
         $mobil = $_POST["mobil"];
         $g_code = $_GET["code"];

         $stmt->execute();
         $stmt->close();
         header('http://....dk/ (...));
     }
     else
     {
         echo 'Der opstod en fejl i erklæringen: ' . $mysqli->error;
     }
 ?>

but when it comes onto the file / page says it like this ...

Fatal error: Call to a member function prepare() on a non-object in /home/jesperbo/public_html/....dk/activate_updater.php on line 2

that is the where it says like this

if($stmt = $mysqli->prepare('UPDATE `brugere` SET `rank`=2, `katogori`=?, `djnavn`=?, `profilbillede`=?, `profiltekst`=?, `facebook`=?, `pris`=?, `booking`=?, `mobil`=? WHERE `code`=?'))

I'm not exactly the best person to see the error but it would be super if you had seen what I've needless churning wrong since it will not be up on the database

hope you can help me with this problem!

回答1:

This is happening because $mysqli isn't the object you think it is. Either you didn't instantiate the MySQLi class properly, or it didn't make a connection. Do a var_dump($mysqli) to get more info as to what's going wrong.



回答2:

As far as I can see, you never actually create your $mysqli object (unless it's loaded in another file, which includes this one). You need to create it:

$mysqli = new MySQLi('localhost', 'username', 'password', 'dbname');

// then
if($stmt = $mysqli->prepare('UPDATE `brugere` SET `rank`=2, `katogori`=?, `djnavn`=?, `profilbillede`=?, `profiltekst`=?, `facebook`=?, `pris`=?, `booking`=?, `mobil`=? WHERE `code`=?'))


标签: php mysqli