我想从我的数据库中显示食物名称和foodRate,但我无法找到为什么我不能用下面的脚本来做。 输入类型无线电和名称所有这些按钮具有不同的值 插入数据没有问题 问题是在php错误的第一个块中注意:未定义索引:foodID在C:\ wamp \ www \ web \ polling \ includes \结果和警告:mysqli_query()期望参数1为mysqli,整数给定i
$pollid = $_POST['foodID'];
$connection = include('connection.php');
$query = "SELECT * FROM polling WHERE foodID='$pollid'";
$q = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($q)) {
$id = $row[0];
$food = $row[1];
$foodRate = $row[2];
$userEmail = $row[3];
echo "<h1>$food</h1>";
echo "<h1>$userEmail</h1>";
}
?>
下面的脚本很好用
if(isset($_POST['submit'])){
if(isset($_POST['button'])&&isset($_POST['email'])){
$query=$DBH->prepare('SELECT COUNT(*) FROM polling WHERE userEmail=?');
$query->bind_param('s',$_POST['email']);
$query->execute();
$query->bind_result($count);
$query->close();
if($count>0)echo"You've already used this email address to vote!";
else{
$update=$DBH->prepare('INSERT INTO polling (userEmail,foodRating) VALUES(?,?)');
$update->bind_param('si',$_POST['email'],$_POST['button']);
$update->execute();
$update->close();
echo"you have successfully voted! Thank you!";
}
}elseif(!isset($_POST['email']))echo"No email address enter";
elseif(!isset($_POST['button']))echo"You've not selected a vote";
}
?>
答案 0 :(得分:0)
您在声明中缺少一个符号
$query = "SELECT * FROM polling WHERE foodID='$pollid'";
S / B
$query = "SELECT * FROM `polling` WHERE foodID='$pollid'";
答案 1 :(得分:0)
尝试mysqli_affected_rows()并查看$ q是否获取任何数据,如果不是,它将永远不会进入while循环
除此之外,您的连接中出现了问题,您是否可以在connection.php中显示您的连接方式?
我不确定但同一页面上的两种不同类型的mysql交互会引发一个红旗。你有其他页面可以使用两种类型的mysql交互吗?
编辑1:试试这个
$connection = mysqli_connect("localhost", "root", "", "test");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
这适用于您的第一个脚本
您使用两种不同类型的mysql交互是否有原因?