2017-09-28 1 views
0

Ich bin neu in Mysqli. Ich benutzte mysql und konvertierte es in mysqli. Wenn die Daten in meiner Datenbank leer sind, erschien der Fehler und sagte "Undefinierte Variable: Kandidat_Name_5", aber in Mysql, wenn die Daten in meiner Datenbank leer sind, funktioniert es immer noch. Ich weiß nicht, was das Problem ist. Es funktioniert gut in mysql, aber viele Fehler in mysqli. Bitte helfen Sie mir, was das Problemundefined Variable, wenn ich mysqli verwende

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

der Fehler nur, wenn die Daten in der Datenbank leer ist es zeigt Fehler „undefine“ aber wenn es Daten in der Datenbank hat, funktioniert es auch in mysqli. Aber in mysql funktioniert es perfekt ohne Fehler, obwohl die Daten in meiner Datenbank leer sind –

+0

Holen Sie sich nicht so. Wiederholen Sie den Abruf und ordnen Sie ihn nur in der Schleife zu. Wenn es keine Ergebnisse gibt, rufen Sie überhaupt nicht ab und verwenden Sie die Variable nicht. Parametrieren Sie auch Ihre Abfrage. Wenn Sie mehrere Variablen wie '$ name_' und' X' haben, sollten Sie wahrscheinlich ein Array verwenden. – chris85

+0

Gibt es etwas mit fetch_assoc() zu tun? für eine Null Daten? –

Antwort

-1

i fügen Sie einfach

error_reporting(1); 

und alles funktioniert jetzt

+0

Dadurch werden die PHP-Warnungen zu Coding-Problemen mehr oder weniger umgangen, anstatt sie zu beheben. Sie sollten stattdessen die Fehlermeldung auf "E_ALL" setzen und die tatsächlichen Probleme beheben. –