hey all.

Have a list of checkboxes with SQL data in table. Currently using echo to ensure I'm using the correct data, however, echo it returning the last checkbox data only. Here is a segment of what I've got.

echo "<table border='1'>";
echo "<tr><th>Last Name</th><th>First Name</th><th>Party</th><th>Description</th><th>Cast Vote</th>";
    while($row = mysql_fetch_array($query))
    {
	$id= $row['Candidate_ID'];
        echo "<tr><td>";
        echo $row['LastName'];
        echo "</td><td>";
        echo $row['FirstName'];
        echo "</td><td>";
        echo $row["Party"];
        echo "</td><td>";
        echo $row["Description"];
        echo "</td><td>";
        echo "<input type='checkbox' name='addcandidate[]' value='<?php $id;?>' size='7'>";
	echo $id;
		if(isset($_POST['addcandidate'])){
				$candidate= $_POST['addcandidate'];
				$N= count($candidate);
					
				$test= "SELECT LastName FROM candidate WHERE Candidate_ID = '$id'";
				
				$sql4= mysql_query($test) or die("SQL error: ".mysql_error());
				$candidateID= mysql_fetch_array($sql4);
				$candidateID= $candidateID[0];
				for($i=1; $i <= $N; $i++){
				$alter= "ALTER TABLE '$votertable' ADD '$candidateID' int";
				}
				}	
       }
    echo "</table>";
?>
<input type='submit' value='Submit'>  
</form>
<?php
		echo $id;
		echo $candidateID;

?>

Any suggestions?

Recommended Answers

All 4 Replies

could you post what the output looks like?

Sorry for such as late reply. Its been a bit of a crazy week.
Essentially this loops through the following columns and puts them in a HTML table. I've assigned the checkbox value $id to the primary key in the sql table.

while($row = mysql_fetch_array($query))
    {
	$id= $row['Candidate_ID'];
        echo "<tr><td>";
        echo $row['LastName'];
        echo "</td><td>";
        echo $row['FirstName'];
        echo "</td><td>";
        echo $row["Party"];
        echo "</td><td>";
        echo $row["Description"];
        echo "</td><td>";
        echo "<input type='checkbox' name='addcandidate[]'  value='<?php $id;?>' size='7'>";

The loop works in assigning the values to each segment of the table. I know this by adding an echo $id statment to the end of the above code.
If I add an echo inside the If(isset) /* Below */ it only return the last entry of the table dispit me selecting different checkboxes.

include_once("unchecked.php");
		if(isset($_POST['addcandidate'])){
				$candidate= $_POST['addcandidate'];
				$N= count($candidate);
				echo $id;

What I'd like to accomplish is, for every checkbox selected, "ALTER table_name ADD $candidate"//

With $candidate = "SELECT LastName FROM candidate WHERE Candidate_ID = '$id'";

The MySql isn't the problem but the selecting of the final checkbox is.
Know its a long winded reply but hope it explains a bit clearer.

UPDATE:

Took a different approach to the situation and got it working. Deleted the isset segment of the code and replaced it with.

<?php
	if(sizeof($_POST['addcandidate'])){
		foreach($_POST['addcandidate'] as $id){
		echo $id;
		}
	}
?>

good to see you got it working on your own, good job :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.