Jump to content

mySQL update with check boxes


wev

Recommended Posts

im developing a voting system where you can vote for multiple choices using checkboxes..

upon submitting the vote form, votecounts must automatically be updated.. values for votecount will come from the checkbox value 1.

 

 

ive been having a hard time trying to figure out hot to update multiple rows with checkbox values..any help would be appreciated..thanks in advance!

 

here's the code:

 

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form_vote"))
$i=1;{
while ($i<= $totalRows_rs_candi) {
  $updateSQL = "UPDATE tbl_candidates SET votecount={$_POST['votecount']} WHERE user_id={$_POST['candi']}";

  mysql_select_db($database_organizazone_db, $organizazone_db);
  $Result1 = mysql_query($updateSQL, $organizazone_db) or die(mysql_error());
  
  $i++;
}

  if (isset($_SERVER['QUERY_STRING'])) {
  $updateGoTo = "vote_success.php";
  }
  header(sprintf("Location: %s", $updateGoTo));
}

 

 

<form id="form_vote" name="form_vote" method="POST" action="<?php echo $editFormAction; ?>">
  <table border="1">
    <tr>
      <td> </td>
      <td>user_id</td>
      <td>l_name</td>
      <td>course_id</td>
      <td>yearlevel</td>
      <td>about</td>
    </tr>
    <?php $i=1; do { ?>
      <tr>
        <td><span id="sprycheckbox1">
        <input name="votecount[<?php echo $i ?>]" type="checkbox" id="votecount" value="1" />
        <span class="checkboxMinSelectionsMsg">Minimum number of selections not met.</span></span>          
        <label for="votecount[]"></label></td>
        <td><label for="candi"></label>
        <input name="candi" type="text" disabled="disabled" id="candi" value="<?php echo $row_rs_candi['user_id']; ?>" readonly="readonly" /></td>
        <td><?php echo $row_rs_candi['l_name'].' ,'.$row_rs_candi['f_name'].' '.$row_rs_candi['m_name']; ?></td>
        <td><?php echo $row_rs_candi['course_id']; ?></td>
        <td><?php echo $row_rs_candi['yearlevel']; ?></td>
        <td><?php echo $row_rs_candi['about_me']; ?></td>
      </tr>
      <?php $i++; ?>
      <?php } while ($row_rs_candi = mysql_fetch_assoc($rs_candi)); ?>
  </table>

Link to comment
Share on other sites

A different way of doing it.  I don't care for SERVER SELF or other things you had going.  Why have htmlentities when updating a voting count with a checkbox?  Anyway this is my version of what you're trying to do.

<?php 
//Page name test21.php

//Personally I would open connection at the top of the page instead of within each query.
/*
$host = "localhost";
$login = "";
$dbpass = "";
mysql_connect("$host","$login","$dbpass") OR DIE
        ("There is a problem with the system.  Please notify your system administrator." .mysql_error());
mysql_select_db($database_organizazone_db, $organizazone_db);
*/

if (isset($_POST['submitvote'])){ 
foreach ($_POST['vote'] as $k => $id){
$newcount=$_POST['votecount'][$k]+1;
$sql="UPDATE tbl_candidates SET votecount='$newcount' WHERE user_id='$id'";
mysql_query($sql) OR die(mysql_error());
}
}

//Personally would not use $_SERVER['PHP_SELF'] but submit to a named page.  For this example I'll use test21.php
?>
<html>
<body>

<form id="form_vote" name="form_vote" method="post" action="test21.php">
  <table border="1">
    <tr>
      <td> </td>
      <td>user_id</td>
      <td>l_name</td>
      <td>course_id</td>
      <td>yearlevel</td>
      <td>about</td>
    </tr>	  
<?php
$sql_candi="SELECT * FROM tbl_candidates";
$rs_candi=mysql_query($sql_candi) OR die(mysql_error()); 
while ($row_rs_candi = mysql_fetch_array($rs_candi)){
//Note: don't nest spans//	  
echo "<tr>
        <td><input name=\"votecount[]\" type=\"hidden\"  value=\"{$row_rs_candi['votecount']}\" /><input name=\"vote[]\" type=\"checkbox\" value=\"{$row_rs_candi['user_id']}\" />
        <span class=\"checkboxMinSelectionsMsg\">Minimum number of selections not met.</span></td>
        <td><input name=\"candi\" type=\"text\" disabled=\"disabled\" id=\"candi\" value=\"{$row_rs_candi['user_id']}\" readonly=\"readonly\" /></td>
        <td>{$row_rs_candi['l_name']} {$row_rs_candi['f_name']} {$row_rs_candi['m_name']}</td>
        <td>{$row_rs_candi['course_id']}</td>
        <td>{$row_rs_candi['yearlevel']}</td>
        <td>{$row_rs_candi['about_me']}</td>
      </tr>";
}
?>
<tr>
      <td colspan="6"><input type="submit" name="submitvote" value="Submit Your Vote" /></td>
    </tr>
  </table>
</form>
</body>
</html>

Link to comment
Share on other sites

Guess I jumped the gun on the last post.  As we're using check boxes in this form, not every loop is passed so array keys don't match.  You could query DB to find current count for selected person or pass the value as part of the value of the check box, then explode values for processing.  The last option is what I've done here.

<?php 
//Page name test21.php

//Personally I would open connection at the top of the page instead of within each query.
/*
$host = "localhost";
$login = "";
$dbpass = "";
mysql_connect("$host","$login","$dbpass") OR DIE
        ("There is a problem with the system.  Please notify your system administrator." .mysql_error());
mysql_select_db($database_organizazone_db, $organizazone_db);
*/

if (isset($_POST['submitvote'])){ 
foreach ($_POST['vote'] as $k => $id){
$values=explode(",",$id	);
$newcount=$values[1]+1;
$sql="UPDATE tbl_candidates SET votecount='$newcount' WHERE user_id='{$values[0]}'";
mysql_query($sql) OR die(mysql_error());
}
}

//Personally would not use $_SERVER['PHP_SELF'] but submit to a named page.  For this example I'll use test21.php
?>
<html>
<body>

<form id="form_vote" name="form_vote" method="post" action="test21.php">
  <table border="1">
    <tr>
      <td> </td>
      <td>user_id</td>
      <td>l_name</td>
      <td>course_id</td>
      <td>yearlevel</td>
      <td>about</td>
    </tr>	  
<?php
$i=0;
$sql_candi="SELECT * FROM tbl_candidates";
$rs_candi=mysql_query($sql_candi) OR die(mysql_error()); 
while ($row_rs_candi = mysql_fetch_array($rs_candi)){
//Note: don't nest spans//	  
echo "<tr>
        <td><input name=\"vote[]\" type=\"checkbox\" value=\"{$row_rs_candi['user_id']},{$row_rs_candi['votecount']}\" />
        <span class=\"checkboxMinSelectionsMsg\">Minimum number of selections not met.</span></td>
        <td><input name=\"candi\" type=\"text\" disabled=\"disabled\" id=\"candi\" value=\"{$row_rs_candi['user_id']}\" readonly=\"readonly\" /></td>
        <td>{$row_rs_candi['l_name']} {$row_rs_candi['f_name']} {$row_rs_candi['m_name']}</td>
        <td>{$row_rs_candi['course_id']}</td>
        <td>{$row_rs_candi['yearlevel']}</td>
        <td>{$row_rs_candi['about_me']}</td>
      </tr>";
$i++;
}
?>
<tr>
      <td colspan="6"><input type="submit" name="submitvote" value="Submit Your Vote" /></td>
    </tr>
  </table>
</form>
</body>
</html>	

Link to comment
Share on other sites

Or like this I guess.

<?php 
//Page name test21.php

//Personally I would open connection at the top of the page instead of within each query.
/*
$host = "localhost";
$login = "";
$dbpass = "";
mysql_connect("$host","$login","$dbpass") OR DIE
        ("There is a problem with the system.  Please notify your system administrator." .mysql_error());
mysql_select_db($database_organizazone_db, $organizazone_db);
*/

if (isset($_POST['submitvote'])){ 
foreach ($_POST['vote'] as $k => $id){
$newcount=$_POST['votecount'][$id][0]+1;
$sql="UPDATE tbl_candidates SET votecount='$newcount' WHERE user_id='$id'";
mysql_query($sql) OR die(mysql_error());
}
}

//Personally would not use $_SERVER['PHP_SELF'] but submit to a named page.  For this example I'll use test21.php
?>
<html>
<body>

<form id="form_vote" name="form_vote" method="post" action="test21.php">
  <table border="1">
    <tr>
      <td> </td>
      <td>user_id</td>
      <td>l_name</td>
      <td>course_id</td>
      <td>yearlevel</td>
      <td>about</td>
    </tr>	  
<?php
$sql_candi="SELECT * FROM tbl_candidates";
$rs_candi=mysql_query($sql_candi) OR die(mysql_error()); 
while ($row_rs_candi = mysql_fetch_array($rs_candi)){
//Note: don't nest spans//	  
echo "<tr>
        <td><input name=\"votecount[{$row_rs_candi['user_id']}][]\" type=\"hidden\"  value=\"{$row_rs_candi['votecount']}\" /><input name=\"vote[]\" type=\"checkbox\" value=\"{$row_rs_candi['user_id']}\" />
        <span class=\"checkboxMinSelectionsMsg\">Minimum number of selections not met.</span></td>
        <td><input name=\"candi\" type=\"text\" disabled=\"disabled\" id=\"candi\" value=\"{$row_rs_candi['user_id']}\" readonly=\"readonly\" /></td>
        <td>{$row_rs_candi['l_name']} {$row_rs_candi['f_name']} {$row_rs_candi['m_name']}</td>
        <td>{$row_rs_candi['course_id']}</td>
        <td>{$row_rs_candi['yearlevel']}</td>
        <td>{$row_rs_candi['about_me']}</td>
      </tr>";
}
?>
<tr>
      <td colspan="6"><input type="submit" name="submitvote" value="Submit Your Vote" /></td>
    </tr>
  </table>
</form>
</body>
</html>	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.