duanbiyi7319 2016-03-26 04:28 采纳率: 100%
浏览 228
已采纳

在PHP中的未定义索引

I get an error of Undefined index, what is the reason?

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'];

    }

Here is the error

Array ( [0] => 1 [CandidateID] => 1 [1] => Jejomar Binay [CandidateName] => Jejomar Binay [2] => President [Position] => President [3] => [NumberofVotes] => ) Array ( [0] => 2 [CandidateID] => 2 [1] => Mar Roxas [CandidateName] => Mar Roxas [2] => President [Position] => President [3] => 1 [NumberofVotes] => 1 )

I will show you now the whole code and the its working now, my output here is to add 1 in number of votes when the radio button is working. it has no error but when i selected the first radio button it only updates the second data.

<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="text" 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
";

                    mysql_close($conn);
                 }
        ?>
  • 写回答

3条回答 默认 最新

  • duankousong9637 2016-03-26 06:48
    关注

    The field "candidateid" should be integer data type, but you are enclosed this field value with ''(single quotes) in the update query?

    $sql = "UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = '$candidateid'";
    

    if it is an integer datatype then you should remove the single quote

    $sql = "UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = $candidateid";
    

    and in MySQL every field names are case sensitive, so as you told the field names are

    candidateid, candidatename, position, numberofvotes

    so, you should use these names when you retrieving the values as well

        <?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 = 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
    ";
    
                    mysql_close($conn);
                 }
        ?>
    <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");
        ?>
        <form method = "post" action = "<?php $_PHP_SELF ?>">
        <?php
        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>";
        }
        echo "</table>";
        mysqli_close($con);
        ?>
    
        <br>
        <br>
        <input name = "update" type = "submit" id = "update" value = "update">
        </form>
        </center>
        </font>
        </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。