4 Replies Latest reply on Jun 8, 2015 7:26 AM by osgood_

    how can i update the data in mysql using the button that set in table?

    aydent58241302

      Hello,

      right now I'm doing my project for online election system using dreamweaver cc14. I create a table using php code that bind the data from mysql and in this table also i create a "vote" button for voter to vote. My question is, how i can update my vote data in mysql database when voters click "vote" button based on id of the candidates? here is my code that i try:

       

      <form method="post" id="form1">
            <?php
      $servername = "localhost";
      $username = "root";
      $password = "pass";
      $dbname = "ses";
      
      
      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
           die("Connection failed: " . $conn->connect_error);
      } 
      
      
      $sql = "SELECT No, Calon, ID, Jurusan, Image FROM candidates";
      $result = $conn->query($sql);
      
      
      if ($result->num_rows > 0) {
           echo "<table >
        <tr>
        <th>NO</th>
        <th>Candidate</th>
        <th>INFO</th>
        <th>Vote</th>
        </tr>";
           // output data of each row
           while($row = $result->fetch_assoc()) {
               echo "<tr>
        <td>" . $row["No"]. "</td>
        <td><img src=" . $row['Image'] . "></td>
        <td><br/>-" . $row["Calon"]. " <br/>-" . $row["ID"]. " <br/>-" . $row["Jurusan"]. "<br/></td>
        <td><input type="."submit"." name=".$row["Calon"]." id=".$row["No"]. " value="."Vote"."></td>
      
        </tr>";
        if(isset($_POST["".$row['No'].""])){
      
        $vote_sachin = "UPDATE candidates SET Undi=Undi+1 WHERE No=".$row["No"]. "";
      
        $run_sachin = mysqli_query($conn, $vote_sachin);
      
      
      }
          }
        echo "</table>";
      } else {
           echo "0 results";
      }
      
      
      
      
      
      
      $conn->close();
      ?>
      
      
      
      
          </form>
      

      I hope anyone can help me in this, because I'm still new in this programming language.

      thank you.

        • 1. Re: how can i update the data in mysql using the button that set in table?
          osgood_ Level 8

          Youre most probably going to have to insert a 'radio button' alongside of the candidates name and grab the database ID from that (I'm assuming that ID is the primary key in your database which uniquely identifies each record.

           

          Its confusing though because you seem to be updating the database using WHERE No=".$row["No"]. so I'm not sure ID is the primary key?


          IF 'No' IS your primary database key then you will have to change the below code:


          <td><input type='radio' name='candidate_id[]' value='".$row['ID']."'></td>


          To:

          <td><input type='radio' name='candidate_id[]' value='".$row['No']."'></td>

           

           

           

          Here's the complete code based on ID being your primary database key.

           

           

          <?php

          $servername = "localhost";

          $username = "root";

          $password = "pass";

          $dbname = "ses";

           

           

          // Create connection

          $conn = new mysqli($servername, $username, $password, $dbname);

          // Check connection

          if ($conn->connect_error) {

               die("Connection failed: " . $conn->connect_error);

          }

           

          $sql = "SELECT No, Calon, ID, Jurusan, Image FROM candidates";

          $result = $conn->query($sql);

           

           

          //if the form 'vote' button is clicked run the below code to update the database

          if(isset($_POST['vote'])) {

           

          //get the ID value from the radio button form field and store in an array.

          $update_vote = $_POST['candidate_id'];

           

          //loop through the array and update the database

          foreach ($update_vote as $value) {

          $vote_sachin = "UPDATE candidates SET Undi=Undi+1 WHERE ID=".$value."";

          $run_sachin = mysqli_query($conn, $vote_sachin);

              }

          }

          ?>

           

          <form method="post" id="form1" action="">

          <?php

          if ($result->num_rows > 0) {

          echo "<table >

          <tr>

          <th>NO</th>

          <th>Candidate</th>

          <th>INFO</th>

          <th>Vote</th>

          </tr>";

          // output data of each row

          while($row = $result->fetch_assoc()) {

          echo "<tr>

          <td>" . $row["No"]. "</td>

          <td><img src=" . $row['Image'] . "></td>

          <td><br/>-" . $row["Calon"]. " <br/>-" . $row["ID"]. " <br/>-" . $row["Jurusan"]. "<br/></td>

          <td><input type='radio' name='candidate_id[]' value='".$row['ID']."'></td>

          </tr>";

          }

              echo "<tr>

              <td><input type='submit' name='vote' value='VOTE'</td>

              </tr>";

          echo "</table>";

          } else {

          echo "0 results";

          }

          $conn->close();

          ?>

          </form>

          • 2. Re: how can i update the data in mysql using the button that set in table?
            aydent58241302 Level 1

            thanks osgood_ for the explaination and code. for the question you asked me is yes No is my primary key in database.. from the explanation you give me that i need to change the ID to No, i already done it..but nothing happen when i click the radio button...do i need to add submit button also?

            • 3. Re: how can i update the data in mysql using the button that set in table?
              aydent58241302 Level 1

              okay I already fixed it! thanks so much osgood_! I really appreciate your help..

              • 4. Re: how can i update the data in mysql using the button that set in table?
                osgood_ Level 8

                aydent58241302 wrote:

                 

                thanks osgood_ for the explaination and code. for the question you asked me is yes No is my primary key in database.. from the explanation you give me that i need to change the ID to No, i already done it..but nothing happen when i click the radio button...do i need to add submit button also?

                 

                If you copy the code below it should work. (and yes you do have to add a 'VOTE' submit button as in the code below - just selecting the radio button alone wont do anything).

                 

                <?php

                $servername = "localhost";

                $username = "root";

                $password = "pass";

                $dbname = "ses";

                 

                 

                // Create connection

                $conn = new mysqli($servername, $username, $password, $dbname);

                // Check connection

                if ($conn->connect_error) {

                     die("Connection failed: " . $conn->connect_error);

                }

                 

                $sql = "SELECT No, Calon, ID, Jurusan, Image FROM candidates";

                $result = $conn->query($sql);

                 

                 

                //if the form 'vote' button is clicked run the below code to update the database

                if(isset($_POST['vote'])) {

                 

                //get the ID value from the radio button form field and store in an array.

                $update_vote = $_POST['candidate_id'];

                 

                //loop through the array and update the database

                foreach ($update_vote as $value) {

                $vote_sachin = "UPDATE candidates SET Undi=Undi+1 WHERE ID=".$value."";

                $run_sachin = mysqli_query($conn, $vote_sachin);

                    }

                }

                ?>

                 

                <form method="post" id="form1" action="">

                <?php

                if ($result->num_rows > 0) {

                echo "<table >

                <tr>

                <th>NO</th>

                <th>Candidate</th>

                <th>INFO</th>

                <th>Vote</th>

                </tr>";

                // output data of each row

                while($row = $result->fetch_assoc()) {

                echo "<tr>

                <td>" . $row["No"]. "</td>

                <td><img src=" . $row['Image'] . "></td>

                <td><br/>-" . $row["Calon"]. " <br/>-" . $row["ID"]. " <br/>-" . $row["Jurusan"]. "<br/></td>

                <td><input type='radio' name='candidate_id[]' value='".$row['No']."'></td>

                </tr>";

                }

                    echo "<tr>

                    <td><input type='submit' name='vote' value='VOTE'</td>

                    </tr>";

                echo "</table>";

                } else {

                echo "0 results";

                }

                $conn->close();

                ?>

                </form>

                1 person found this helpful