简体   繁体   中英

Fatal error: Call to a member function query() on null in

I'm trying to create a PHP form where I can add values to a table in my database and this is my code:

<?php
  $host ="localhost";
  $db_nome = "my_farneseluca";
  $username = "farneseluca";

  // Create connection
  mysql_connect($host, $username) or die('Impossibile connettersi al server: ' . mysql_error());
  mysql_select_db($db_nome) or die ('Accesso al database non riuscito: ' . mysql_error());

  $table = $_POST['table'];
  $descr = $_POST['descr'];
  $certif = $_POST['certif'];

  $sql = "INSERT INTO $table (DESCRIZIONE, CERTIFICAZIONE) VALUES ('$descr', '$certif');";

  if ($con->query($sql) === TRUE) 
  {
    echo 'users entry saved successfully';
  }   
  else 
  {
    echo 'Error: '. $con->error;
  }

  mysqli_close($con);
?>
<form method="post" action="editdata.php" target="">
  Inserire tabella da modificare<br>
  <input type="text" name="table"><br>
  DESCRIZIONE<br>
  <input type="text" name="descr"><br>
  CERTIFICAZIONE<br>
  <input type="text" name="certif"><br>
  <input type="submit" value="Aggiorna">
</form>

When I click the "Aggiorna" button, the page gives this error:

Fatal error: Call to a member function query() on null in /membri/farneseluca/editdata.php on line 96

I tried to see in other posts or in google but i can understand what's the problem

You tried to call query() on $con, but you haven't declared $con anywhere.

Fatal error: Call to a member function query() on null

Define $con:

 $con = mysqli_connect($host, $username) or die('Impossibile connettersi al server: ' . mysqli_error());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM