我是mysqli的新手。我正在使用mysql并在mysqli中转换它。当我的数据库中的数据为空时,错误显示并显示“未定义的变量:candidate_name_5”,但在mysql中,当mydatabase中的数据为空时,它仍然有效。我不知道这个问题是什么。它在mysql中运行良好但在mysqli中有很多错误。请帮我解决问题
<?php
require('connection.php');
$connect = mysqli_connect('localhost','root','','smart_id_user');
// retrieving candidate(s) results based on position
if (isset($_POST['monthReport'])){
/*
$resulta = mysql_query("SELECT * FROM tbCandidates where candidate_name='Luis Nani'");
while($row1 = mysql_fetch_array($resulta))
{
$candidate_1=$row1['candidate_cvotes'];
}
*/
$monthReport = $_POST['monthReport'];
$results = mysqli_query($connect,"SELECT * FROM voting_tbCandidates where candidate_position='$monthReport'");
$row1 = mysqli_fetch_array($results); // for the first candidate
$row2 = mysqli_fetch_array($results); // for the second candidate
$row3 = mysqli_fetch_array($results);
$row4 = mysqli_fetch_array($results);
$row5 = mysqli_fetch_array($results);
$row6 = mysqli_fetch_array($results);
$row7 = mysqli_fetch_array($results);
$row8 = mysqli_fetch_array($results);
$row9 = mysqli_fetch_array($results);
$row10 = mysqli_fetch_array($results);
$row11 = mysqli_fetch_array($results);
$row12 = mysqli_fetch_array($results);
if ($row1){
$candidate_name_1=$row1['candidate_name']; // first candidate name
$candidate_1=$row1['candidate_cvotes']; // first candidate votes
}
if ($row2){
$candidate_name_2=$row2['candidate_name']; // second candidate name
$candidate_2=$row2['candidate_cvotes']; // second candidate votes
}
if ($row3){
$candidate_name_3=$row3['candidate_name'];
$candidate_3=$row3['candidates_cvotes'];
}
if($row4){
$candidate_name_4=$row4['candidate_name'];
$candidate_4=$row4['candidate_cvotes'];
}
if($row5){
$candidate_name_5=$row5['candidate_name'];
$candidate_5=$row5['candidate_cvotes'];
}
if($row6){
$candidate_name_6=$row6['candidate_name'];
$candidate_6=$row6['candidate_cvotes'];
}
if($row7){
$candidate_name_7=$row7['candidate_name'];
$candidate_7=$row7['candidate_cvotes'];
}
if($row8){
$candidate_name_8=$row8['candidate_name'];
$candidate_8=$row8['candidate_cvotes'];
}
if($row9){
$candidate_name_9=$row9['candidate_name'];
$candidate_9=$row9['candidate_cvotes'];
}
}
else
// do nothing
?>
<?php
// retrieving positions sql query
$positions=mysqli_query($connect,"SELECT * FROM voting_tbPositions")
or die("There are no records to display\n" . mysqli_error());
?>
<?php
session_start();
//If your session isn't valid, it returns you to the login screen for
protection//
if(empty($_SESSION['user_id'])){
}
?>
<?php if(isset($_POST['monthReport']))
{$totalvotes=$candidate_1+$candidate_2+$candidate_3+$candidate_4;} ?>
<html><head>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="student.php">HOME</a>
<a><hr></a>
<a href="vote.php">VOTE</a>
<a><hr></a>
<a href="refresh.php">POLL RESULTS</a>
<a><hr></a>
<a href="logout.php">LOGOUT</a>
</div>
<div id="main">
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰
</span>
<script language="JavaScript" src="js/admin.js">
</script>
</head><body bgcolor="tan">
<center>
<font color = "white" size="7"> <div id="hihi">ONLINE VOTING SYSTEM</div></font>
<br><br>
<br>
<br></center>
<div id="page">
<div id="header">
<h1>POLL RESULTS </h1>
</div>
<div id="container">
<table width="420" align="center">
<form method="POST" action="refresh.php">
<tr>
<td>Choose Position</td>
<td><SELECT name="monthReport" id="monthReport" onchange="form.submit()">
<OPTION VALUE="select">select
< ?php
//loop through all table rows
while ($row=mysqli_fetch_array($positions)){
echo "<OPTION VALUE=$row[position_name]>$row[position_name]";
}
?>
</SELECT></td>
<script type="text/javascript">
document.getElementById('month').value = "<?php echo $_POST['monthReport'];?>";
</script>
<?php
echo '<div id="gitna">';
echo $monthReport;
echo '</div>';
?>
<?php if(isset($_POST['monthReport'])){echo $candidate_name_1;} ?>:<br>
<img src="images/candidate-1.gif"
width='<?php if(isset($_POST['monthReport'])){ if ($candidate_2 ||
$candidate_1 || $candidate_3 || $candidate_4 || $candidate_5!= 0){echo(100*round($candidate_1/($candidate_2+$candidate_1+$candidate_3+$candidate_4+$candidate_5),5));}} ?>'
height='20'>
<?php if(isset($_POST['monthReport'])){ if ($candidate_2 ||
$candidate_1 || $candidate_3 ||$candidate_4 || $candidate_5!= 0)
{echo(100*round($candidate_1/($candidate_2+$candidate_1+$candidate_3+$candidate_4+$candidate_5),5));}} ?>% of <?php if(isset($_POST['monthReport'])){echo $totalvotes;} ?> total votes
<br> votes <?php if(isset($_POST['monthReport'])){ echo $candidate_1;} ?>
<br>
<br>
<?php if(isset($_POST['monthReport'])){ echo $candidate_name_2;} ?>:<br>
<img src="images/candidate-2.gif"
width='<?php if(isset($_POST['monthReport'])){ if ($candidate_2 || $candidate_1 || $candidate_3 || $candidate_4 || $candidate_5!= 0){echo(100*round($candidate_2/($candidate_2+$candidate_1+$candidate_3+$candidate_4+$candidate_5),5));}} ?>'
height='20'>
<?php if(isset($_POST['monthReport'])){if ($candidate_5 || $candidate_4 || $candidate_3 || $candidate_2 || $candidate_1!=0){echo(100*round($candidate_5/($candidate_5+$candidate_4+$candidate_3+$candidate_2+$candidate_1),5));}} ?>% of <?php if(isset($_POST['monthReport'])){echo $totalvotes;} ?> total votes
<br>votes <?php if(isset($_POST['monthReport'])){echo $candidate_5;} ?>
<br>
<br>
<br>
</div>
</body></html>
答案 0 :(得分:-1)
我只是添加
error_reporting(1);
现在一切正常