0

I am having trouble with a part of my code. I want the checkbox in the last column to be checked if the corresponding DB value is 1, but something keeps going wrong with my code, anyone who sees what's wrong? It's probably very simple but I can't find it.

<?php
    $sql = "SELECT * FROM registered ORDER BY datum"; 
$myData=mysql_query($sql,$con) ;
?>

<table width="1100" border="1">
<tr>
<th style="text-align:center; padding:0 10px">naam</th>
<th style="text-align:center; padding:0 10px">betaald?</b></th>
</tr>

<?php

$betaald = $record['betaald'];

while($record = mysql_fetch_array($myData)) {

    echo  "<tr>";
    echo  "<td>" . $record['naam'] . "</td>";
    echo  "<td> <input type='checkbox' name='betaald' id='betaald' value='1' ". echo ($betaald==1 ? 'checked' : ''); . " ></td>";
    echo  "</tr>";

    }

mysql_close($con);
?>

4 Answers 4

3
<?php
$sql = "SELECT * FROM registered ORDER BY datum";
$myData = mysql_query($sql, $con);
?>

<table width="1100" border="1">
    <tr>
        <th style="text-align:center; padding:0 10px">naam</th>
        <th style="text-align:center; padding:0 10px">betaald?</b></th>
    </tr>

    <?php while ($record = mysql_fetch_array($myData)) { ?>

        <tr>
            <td><?php echo $record['naam'] ?></td>
            <td> 
                <input type='checkbox' name='betaald' id='betaald' value='1'
                        <?php if($record['betaald'] == 1){ ?>
                            checked="checked"
                        <?php } ?>
                        />
            </td>
        </tr>
   <?php  } ?>
</table>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

<?php
    $sql = "SELECT * FROM registered ORDER BY datum";
    $myData= mysql_query($sql) ;
?>

    <table width="1100" border="1">
    <tr>
        <th style="text-align:center; padding:0 10px">naam</th>
        <th style="text-align:center; padding:0 10px">betaald</b></th>
    </tr>

<?php

    while($record = mysql_fetch_array($myData))
    {
        ?>

        <tr>
            <td> <?php echo  $record['naam'] ?></td>
            <td> <input type='checkbox' name='betaald' id='betaald' value='1' <?php echo ($record['betaald']==1 ? 'checked' : '')?>> </td>
        </tr>
<?php
    }

    mysql_close($con);
?>

mysql_query($sql) no need to define $con

Note: This extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0

Comments

0
<?php
    $sql = "SELECT * FROM registered ORDER BY datum"; 
$myData=mysql_query($sql,$con) ;
?>

<table width="1100" border="1">
<tr>
<th style="text-align:center; padding:0 10px">naam</th>
<th style="text-align:center; padding:0 10px">betaald?</b></th>
</tr>

<?php



while($record = mysql_fetch_array($myData)) {

    $betaald = $record['betaald'];
    echo  "<tr>";
    echo  "<td>" . $record['naam'] . "</td>";
    echo  "<td> <input type='checkbox' name='betaald' id='betaald' value='1' ";
    echo $betaald==1 ? 'checked' : '';
    echo " ></td>";
    echo  "</tr>";

    }

mysql_close($con);
?>

You have assigned $betaald value outside the while loop so there is no value in $betaald.

I have included $betaald inside while loop.

1 Comment

Thanks for your answer but I tried it without succes. I've noticed the problem is in the line echo "<td> <input type='checkbox' name='betaald' id='betaald' value='1' ". echo ($betaald==1 ? 'checked' : ''); . " ></td>";
0

You need several changes in your php and html code. First of all I've some confusion about your code. Let me clear those.

Point 1 [name='betaald']: If you have a name like this you will not get multiple selected value.

Point 2 [id='betaald']: ID must be unique in a page.

Point 3 [value='1']: How will you differ values if all have same value?

so your code should be something like this:

<?php
$sql = "SELECT * FROM registered ORDER BY datum"; 
$myData=mysql_query($sql,$con) ;
?>

<table width="1100" border="1">
<tr>
<th style="text-align:center; padding:0 10px">naam</th>
<th style="text-align:center; padding:0 10px">betaald?</b></th>
</tr>

    <?php
$betaald = $_POST['betaald'];
$i = 1;
while($record = mysql_fetch_array($myData)) {

    echo  "<tr>";
    echo  "<td>" . $record['naam'] . "</td>";
    echo  "<td> <input type='checkbox' name='betaald[]' id='betaald".$i."' value='".$record['betaald']."' ";
    if ($record['betaald'] == $betaald)
    {
        echo "checked";
    }
    echo " ></td>";
    echo  "</tr>";
    $i++;
    }

mysql_close($con);
?>

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.