2017-09-28 1 views
0

mysqli에 새로운 기능이 추가되었습니다. mysql을 사용하고 mysqli에서 변환했다. 내 데이터베이스의 데이터가 비어있을 때 오류가 나타 났으며 "정의되지 않은 변수 : candidate_name_5"라고 표시되었지만 mydatabase의 데이터가 비어 있으면 mysql에서 여전히 작동합니다. 나는 그 문제가 뭔지 모른다. MySQL에서는 잘 작동하지만 mysqli에서는 많은 오류가 발생합니다. 난 그냥mysqli를 사용할 때 정의되지 않은 변수

error_reporting(1); 

를 추가

<?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

데이터베이스의 데이터가 비어있는 바로 그 때 오류가 오류 "UNDEFINE"이 표시 되나 데이터베이스에 데이터가 있으면 mysqli에서도 잘 작동한다. 하지만 내 DB에있는 데이터가 비어있는 경우에도 그것은 완벽하게 오류없이 작동합니다 mysql에서 –

+0

그런 fetch하지 마십시오. 가져 오기를 반복하고 루프에서 할당하십시오. 결과가 없으면 전혀 가져 오지 않고 변수를 사용하지 마십시오. 또한 쿼리를 매개 변수화하십시오. '$ name_' +'X'와 같은 변수가 여러 개인 경우 아마 배열을 사용해야합니다. – chris85

+0

fetch_assoc()과 관련이 있습니까? null 데이터가있는 것에 대해? –

답변

-1

문제가 무엇인지 좀 도와 모든 것이 지금 잘 작동하십시오

+0

이것은 코딩 문제를 고치기보다는 PHP의 경고를 우회하는 것입니다. 대신 오류보고를 'E_ALL'로 설정하고 실제 문제를 해결해야합니다. –

관련 문제