我有一个表格:
<form action="createPoll.php" method="POST">
<!-contents->
<input type="submit" value="Submit">
</form>
单击提交按钮后,它将运行createPoll.php,其中包含:
<?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;
?>
问题是我收到此错误:
注意:未定义的属性:第61行中C:\ xampp \ htdocs \ createPoll.php中的mysqli :: $ close
指的是$ conn-> close(); 我在其他地方也有类似的代码(用于登录和注册),并且在那里工作正常(连接关闭正常),但是由于某种原因,我似乎在这里出现错误。 有什么想法吗?
答案 0 :(得分:0)
您未指定MySQLi连接参数。您调用new mysqli ($host, $dbusername, $dbpassword, $dbname)
,但从未定义$host
,$dbusername
,$dbpassword,
或$dbname
。您还将混合使用 won't work 的MySQL API(例如mysqli_connect_error()
和mysql_connect_error()
)。
此外,您应该真正考虑使用prepared statements来防止SQL injection。
答案 1 :(得分:0)
确切地显示错误消息。close
对象中没有名为mysqli
的属性。close
是一个您必须按以下方式调用的函数。
$conn->close();
您错过了函数结尾处的括号()
答案 2 :(得分:0)
我认为您需要将代码修复为以下内容:
<?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();
?>
请勿使用提到的“黑曜石时代”之类的mysql_ *函数。