-3

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\n";

                    mysql_close($conn);
                 }
        ?>

3 Answers3

1

there is no key in your array $row as 'candidateid' please do var_dump($row); and see what is the key name, or if these are just same as your column name in your DB table, check the name of it.

Dray
  • 887
  • 8
  • 25
1

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\n";

                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>
Thiyagesan
  • 75
  • 6
  • it works great bit why i cant transfer radiobutton value in the textbox so i can update it – Alexiusjoe Coronel Mar 26 '16 at 07:06
  • Why do you want to transfer the value to the textbox? – Thiyagesan Mar 26 '16 at 07:12
  • You can directly start the form tag before table tag then you can automatically get the selected radio button value after submitting the form. – Thiyagesan Mar 26 '16 at 07:14
  • my goal here is when I press the radiobutton the numbervotes will increase by one, can you help me with that? – Alexiusjoe Coronel Mar 26 '16 at 07:15
  • Wow TY so much its a big help but it only adds 1 how can i increase it? i mean two persons vote the same candidate how it will increase by 1 and save in database? – Alexiusjoe Coronel Mar 26 '16 at 07:26
  • in that case you can update your update query like `$sql = "UPDATE candidate_info SET numberofvotes = numberofvotes + 1 WHERE candidateid = '$candidateid'" ;` – Thiyagesan Mar 26 '16 at 07:27
  • it works so great TY so much but i think theres an error like for example i have 2 data and i select the 1st one the second data will be updated and theres an instance that the past command will execute something like that. I will approve now your code. TY so much this is my last error @Thiyagesan – Alexiusjoe Coronel Mar 26 '16 at 07:34
  • If I understand your problem, then my guess is correct, You should move your PHP code which is below to the

    tag to the top of entire code, so that your update query will update before displaying the form. Then you will get the updated result too.

    – Thiyagesan Mar 26 '16 at 07:42
  • What do you mean sir? at the top of ? the whole code of inside the php tag? – Alexiusjoe Coronel Mar 26 '16 at 07:47
  • Sir its done, All I want to say is thank you so so much! :) – Alexiusjoe Coronel Mar 26 '16 at 07:52
  • Sir its ok now but when i refresh it the sql query works i mean its updating. – Alexiusjoe Coronel Mar 26 '16 at 07:53
  • To avoid resubmitting the form you can reload the page after completing the update query like this `header('Location: [CurrentFileName]');` replace **[CurrentFileName]** with your file name. – Thiyagesan Mar 26 '16 at 08:00
  • Sir where i will put that? – Alexiusjoe Coronel Mar 26 '16 at 08:05
  • After `$retval = mysql_query( $sql, $conn );` line but you will not get that success message, then you can remove the code `if(! $retval ) { die('Could not update data: ' . mysql_error()); } echo "Updated data successfully\n";` – Thiyagesan Mar 26 '16 at 08:07
  • Sir its ok now, A very big Thank you so much – Alexiusjoe Coronel Mar 26 '16 at 08:09
  • Hi Sir its an honor for me to be helped by you ahmmm sir I have a new question pls see this link http://stackoverflow.com/questions/36233712/bar-graph-using-the-data-from-mysql-database @NanaPartykar – Alexiusjoe Coronel Mar 26 '16 at 09:56
1

Points To Be Noted :

  1. No Need to create separate <form></form> for sending candidateid for updating numberofvotes.
  2. If you are submitting in same page then avoid multiple database connection.
  3. Your database table candidate_info field name is not matching with what you wrote in <table><tr></tr></table>. So, use exact column name what is there in database table.
  4. Put your complete <table></table> inside <form></form>.
  5. Since you are looking for single candidate value to get get updated, so radio button is helpful. If multiple candidate value need to be updated, then you have to use checkbox with name as array type.

Updated Code:

<html>
    <center>
        <font size="2" face = "century gothic">
        <?php
        $con=mysqli_connect("localhost","root","","election2016");
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        ?>
        <form method="post" action = "<?php $_PHP_SELF ?>">
            <?php
            $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']."' ></td>";
                        echo "<td>" . $row['numberofvotes'] . "</td>";
                        $candidateID=$row['candidateid'];
                    }
            echo "</table>";
            mysqli_close($con);
            ?>
            <br>
            <br>
            <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);
}
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77