0

This is my code for creating an html form that reads from a database and will allow the user to check and uncheck boxes for each of the 640 items. This is the form.php:

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another 
    echo "<html><body>  <table cellpadding=10 border=1>"; 
    while($row = mysql_fetch_assoc($result)) { 
        echo "<tr>"; 
        echo "<td>".$row['stickerID']."</td>"; 
        echo "<td>" .$row['stickerName']."</td>"; 
        echo "<td>".$row['stickerSection']."</td>"; 
        echo "<td>"?>
                 <form name="some form" action="editform.php" method="post">
                 <input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
                 <?php "</td>";
        echo "</tr>"; 
    }
    echo "</table></body></html>";
    echo " " ?>
                 <input type="submit" name="editWish" value="Edit">
                 </form>
                 <?php " ";
} else { 
    // no 
    // print status message 
    echo "No rows found!"; 
} 

The user must then be able to click on submit and have those values updated in the mysql database.

Right now when I click the submit button, it posts to edit form.php which has this:

<?php

//echo results
foreach($_POST['stickerID'] as $k=>$v ){
echo $k;
echo $v;
}

?>

But I don't get anything echoed. I was thinking the problem could be that Im actually creating a form for every row instead of 1 form with many rows/checkboxes. But when I move the form code after the and the tag to the line where line, I can't even load the form.php, it just loads blank.

Where is my problem? :) Thx

3
  • There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. Commented Apr 9, 2014 at 13:57
  • Thanks Amal, could you help me with that? Right now I just need to get this working even with a quick and dirty fix. Then I could spend more time studying how to upgrade it. Commented Apr 9, 2014 at 13:59
  • 1
    add [] to your checkbox name, for example name="myCheckbox[]". This will allow you to submit everything as array and your loop will work Commented Apr 9, 2014 at 14:01

2 Answers 2

2

Name your checkbox like this:

<input type="checkbox" name="stickerID[]" value=" <?php echo $row['stickerStatus']; ?> ">

And as Amal already said update your code to PDO or MySQLi

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Now Im getting key=>value for only the items check marked in the form. However, since all values in the database are initially zero, I need to now get the value from the actual post for those selected items. meaning that if I select items 1, 2 & 3, I get 1=>0, 2=>0 & 3=>0. But I would actually want to update those to 1, not 0. How would I do that?
in the value from your checkbox give it the value $row['stickerID']; Than in your foreach update your table WHERE stickerID = $value and set value to 1.
0

you can do this with a tag :-

echo "<td>" .$row['stickerName']."</td>"; 
        echo "<td>".$row['stickerSection']."</td>"; 
        echo "<td>"?>


                 <form name="some form" action="editform.php" method="post">
                 <input type="checkbox" name="checkbox[]" value=" <?php echo $row['stickerStatus'] ?> ">
                 <?php "</td>";
        echo "</tr>";

on your php code you get :-

$all_checkes_checkbox = $_POST['checkbox'];

here is your all checked checkbox:- and this array also bale key and value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.