2016-03-25 13 views
1

Merhaba Herkes İyi Öğleden Sonra Bir soru sorabilir miyim? ama bundan önce benim kodum.Radyo Düğmesini kullanarak MySQL'de Veri Güncelleme?

<html> 
    <center> 
    <font size="2" face = "century gothic"> 
    <?php 
    $con=mysqli_connect("localhost","root","","election2016"); 
    // Check connection 
    if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    $result = mysqli_query($con,"SELECT * FROM candidate_info"); 
    echo "<table border='1'> 
    <tr> 
    <th>Candidate Name</th> 
    <th>Position</th> 
    <th>Vote</th> 
    <th>Number of Votes</th> 
    </tr>"; 
    while ($row = mysqli_fetch_array($result)) { 
     echo "<tr>"; 

     echo "<td>" . $row['CandidateName'] . "</td>"; 
     echo "<td>" . $row['Position'] . "</td>"; 
     echo "<td><input type='radio' name='candidateid'/>"; 
     echo "<td>" . $row['NumberofVotes'] . "</td>"; 
    } 
    echo "</table>"; 
    mysqli_close($con); 
    ?> 

    <br> 
    <br> 
    <form method = "post" action = "<?php $_PHP_SELF ?>"> 
    <input name = "update" type = "submit" id = "update" value = "Update"> 
    </form> 
    </center> 
    </font> 
    </html> 
    <?php 
      if(isset($_POST['update'])) { 
       $dbhost = 'localhost'; 
       $dbuser = 'root'; 
       $dbpass = ''; 

       $conn = mysql_connect($dbhost, $dbuser, $dbpass); 

       if(! $conn) { 
        die('Could not connect: ' . mysql_error()); 
       } 

       $candidateid = $row['candidateid']; 


       $sql = "UPDATE candidate_info SET numberofvotes = '1' WHERE candidateid = $candidateid" ; 
       mysql_select_db('election2016'); 
       $retval = mysql_query($sql, $conn); 

       if(! $retval) { 
        die('Could not update data: ' . mysql_error()); 
       } 
       echo "Updated data successfully\n"; 

       mysql_close($conn); 
      } 
    ?> 

İşte benim Kanununun Çıktı o Adayları, Pozisyon listesini gösterecek olan bir düğme ile Oy Radyo Düğmesi sayısı kaydedin.

Benim hatam, bir radyo düğmesi seçtiğimde ve numberofvotes alanına 1 yerleştirmek istediğim düğme güncellemesini tıklatıp güncellemediğimde. Kodumdaki sorun nedir?

Herhangi bir yardım için teşekkür ederiz. TY çok fazla

cevap

0
<html> 
     <center> 
     <font size="2" face = "century gothic"> 
     <?php 
     $con=mysqli_connect("localhost","root","","election2016"); 
     // Check connection 
     if (mysqli_connect_errno()) 
     { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 
     $result = mysqli_query($con,"SELECT * FROM candidate_info"); 
     echo "<table border='1'> 
     <tr> 
     <th>Candidate Name</th> 
     <th>Position</th> 
     <th>Vote</th> 
     <th>Number of Votes</th> 
     </tr>"; 
     while ($row = mysqli_fetch_array($result)) { 
      echo "<tr>"; 

      echo "<td>" . $row['CandidateName'] . "</td>"; 
      echo "<td>" . $row['Position'] . "</td>"; 
      echo "<td><input type='radio' name='candidateid' value='".$row['candidateid']."' >"; 
      echo "<td>" . $row['NumberofVotes'] . "</td>"; 
    $candidateid=$row['candidateid']; 
     } 
     echo "</table>"; 
     mysqli_close($con); 
     ?> 

     <br> 
     <br> 
     <form method = "post" action = "<?php $_PHP_SELF ?>"> 
    <input type="hidden" name="candidateid" value="<?php echo $candidateid;?>"> 
     <input name = "update" type = "submit" id = "update" value = "update"> 
     </form> 
     </center> 
     </font> 
     </html> 
     <?php 
       if(isset($_POST['update'])) { 
        $dbhost = 'localhost'; 
        $dbuser = 'root'; 
        $dbpass = ''; 
$candidateid = $_POST['candidateid']; 
        $conn = mysql_connect($dbhost, $dbuser, $dbpass); 

        if(! $conn) { 
         die('Could not connect: ' . mysql_error()); 
        } 

        $candidateid = $_POST['candidateid']; 


        $sql = "UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = '$candidateid'" ; 
        mysql_select_db('election2016'); 
        $retval = mysql_query($sql, $conn); 

        if(! $retval) { 
         die('Could not update data: ' . mysql_error()); 
        } 
        echo "Updated data successfully\n"; 

        mysql_close($conn); 
       } 
     ?> 
+0

Bu kodu kontrol ettim ve çalışıyor. Sorun, formunuzu gönderdiğinizde $ row dizisine sahip olmamanızdır. Bu yüzden kimliği gizli bir alanla sayfaya göndermelisiniz. Thant kodunuzu almak için ne yaptım. – DevMan

+0

Efendim Kodu deneyin ama çalışmıyor. Undefined index hatası alıyorum –

+0

@AlexiusjoeCoronel Lütfen veritabanı yapınızı gösterir misiniz? Kodda kullandığınız alan adlarında bir hata var ve koddaki bir veya daha fazla alan isimleri veri tabanındaki alan adları ile aynı değil. Tanımsız dizin hatası bunun nedeni. – DevMan

İlgili konular