0

I have a form:

<form action="createPoll.php" method="POST">
<!-contents->
<input type="submit" value="Submit">    
</form>

When the submit button is clicked it runs createPoll.php which contains:

<?php
ini_set('session.gc_maxlifetime', 3600);
session_set_cookie_params(3600);
session_start();

//contents

$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
    if(mysqli_connect_error()){
    die("Connection Error (" .mysql_connect_errno(). ") " . mysql_connect_error());
    header('Location: user.php'); 
    }
    else{
        $sql="select count(poll_id) from poll_info where poll_id=$poll_id;";

        while(mysqli_query($conn,"SELECT count(poll_id) from table where poll_id=$poll_id") >=1){
            $poll_id= random_int(0,1000000);
        }
        $sql= "INSERT INTO poll_info (poll_id, username, input_date_time, open_time, data) values('$poll_id','$username','$input_date_time','$open_time','$data')"; //defined above in contents

        if($conn->query($sql)){
            echo "<script>alert('success!')</script>";
        }
        else{
            echo"Error: ". $sql ."<br>". $conn->error;
        }
    }
$conn->close;
?>

The problem is that I get this error:

Notice: Undefined property: mysqli::$close in C:\xampp\htdocs\createPoll.php in line 61

which refers to the $conn->close(); I have similar code elsewhere (for logging in and signing up) and it works fine there (connection closes fine) but I seem to get an error here for some reason. Any ideas?

LTM
  • 527
  • 1
  • 9
  • 18

3 Answers3

0

You don't specify your MySQLi connection params. You call new mysqli ($host, $dbusername, $dbpassword, $dbname) yet never define $host, $dbusername, $dbpassword, or $dbname. You're also mixing MySQL APIs (such as mysqli_connect_error() and mysql_connect_error()), which won't work.

Additionally, you should really consider using prepared statements to prevent SQL injection.

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • Those variables are defined in the contents section. By mixing apis do you mean to say that I should do everything object style? – LTM Aug 24 '18 at 04:18
  • Assuming your PHP file is exactly as as above, those variables are never created. If they are indeed created correctly in the part that you have omitted, the problem is the mixture of APIs. And by mixing APIs I mean that you cannot use `mysqli_*` in conjunction with `mysql_*`. While you **can** mix object-oriented programming with the procedural style, I'd recommend against it (mainly for the sake of clarity). And you should never, ever use `mysql_*` functions; they've been deprecated / removed for years and are full of security vulnerabilities. `mysqli_*` is OK though. – Obsidian Age Aug 24 '18 at 04:21
0

as exactly the error message says.there is no property called close in mysqli object.close is a function which you will have to call as below.

$conn->close();

You have missed the parentheses() at the end of the function

Nuwan Attanayake
  • 1,161
  • 9
  • 20
0

I think you need to fix your code to something like this:

<?php

    // if anybody wants to format this better go ahead... 
    //...

    $host = '';
    $dbusername = '';
    $dbpassword = ''; 
    $dbname = '';

    $mysqli = new mysqli($host, $dbusername, $dbpassword, $dbname);
    if ($mysqli->connect_errno) {
      die("Connection Error");
      header('Location: user.php');
    } else {
      $mysqli->query("SELECT count(poll_id) FROM `poll_info` WHERE `poll_id` = '$poll_id'");
      while ($conn,"SELECT count(poll_id) FROM table WHERE `poll_id` = '$poll_id'") >=1)) {
          $poll_id= random_int(0,1000000);
      }

      $sql= "INSERT INTO poll_info (poll_id, username, input_date_time, open_time, data) values('$poll_id','$username','$input_date_time','$open_time','$data')";
      if($mysqli->query($sql)){
          echo "<script>alert('success!')</script>";
      } else {
          echo"Error ...";
      }
    }
    $mysqli->close();
?>

Don't use those mysql_* functions like Obsidian Age mentioned.