2017-09-28 83 views
0

我是mysqli中的新成員。我正在使用mysql並將其轉換爲mysqli。當數據庫中的數據爲空時,錯誤出現並顯示「Undefined variable:candidate_name_5」,但在mysql中當mydatabase中的數據爲空時仍然有效。我不知道最新的問題。它在mysql中工作正常,但mysqli中有很多錯誤。請幫我這是什麼問題未定義變量當我使用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()">&times;</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()">&#9776; 
</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

錯誤只是當數據庫中的數據爲空,它顯示錯誤「取消定義」,但當它有數據庫中的數據時,它在mysqli中也能正常工作。但在MySQL中,即使數據庫中的數據爲空,它也可以毫無錯誤地工作。 –

+0

不要像那樣獲取數據。循環取回並在循環中分配。如果沒有結果,則根本不提取,也不要使用該變量。還要參數化您的查詢。如果你有多個變量,比如'$ name_' +'X',你應該使用一個數組。 – chris85

+0

與fetch_assoc()有什麼關係?有空數據? –

回答

-1

我只是添加

error_reporting(1); 

,一切工作現在罰款

+0

這或多或少地繞過了PHP關於編碼問題的警告,而不是修復它們。您應該將錯誤報告設置爲'E_ALL'並解決實際問題。 –