Ryan
Ryan

Reputation: 61

Radio button check on load from mysql

Hi guys i have a table/form I'm making and in one of the <TD>.

I'm making a button to ban or unban a user but i would like it to auto check based on whats already in the DB using MySql.

Here is what i currently have but its not auto populating:

echo "<td>" . '<form method="post" action=""> <input type="hidden" name="id" value=' . $ID . '>  Ban:<input type="radio" value="1" name="unban" onclick="javascript: submit()" if({$row[\'Banned\']}==1 {echo "checked"; }/><br> Unban:<input type="radio" value="0" name="name" onclick="javascript: submit()" if({$row[\'Banned\']}=="0 {echo "checked"; }/><br /> </td>';

Thanks in advanced.

EDIT: Here is the complete code minus the DB info:

$LINK = mysql_connect($DB_HOST, $DB_USER, $DB_PASS) or die(mysql_error()); // Connect to database server(localhost) with username and password.
mysql_select_db($DB_DTBS) or die(mysql_error()); // Select registration database.

$search = mysql_query('SELECT * FROM users') or die(mysql_error()); 

$match  = mysql_num_rows($search);


if($match > 0){
    echo "Users";
    echo "<table border='1'>
            <tr>
            <th>UserName</th>
            <th>Email</th>
            <th>Access Level</th>
            <th>Ban</th>
            <th>Ban Reason</th>

            </tr>";

    while($row = mysql_fetch_array($search)) {
    echo "<tr>";
    $ID = $row['AccountID'];
   # echo "<td>" . $row['AccountID'] . "</td>";
    echo "<td>" . $row['Username'] . "</td>";
    echo "<td>" . $row['Email'] . "</td>";
    echo "<td>" . '<form method="post" action=""><select     onchange="this.form.submit()" name="access"><option value="0">Standard</option><option    value="300">VIP</option><option value="500">Moderator</option><option value="600">Admin</option><option value="700">OP</option></select></td>';
    #echo "<td>" . $row['Banned'] . "</td>";
    echo "<td>" . 
    '<form method="post" action=""> <input type="hidden" name="id" value=' . $ID . '>
    Ban:<input type="radio" value="1" name="unban" onclick="javascript: submit()" '.    (($row['Banned'] == "1") ? 'checked=checked"' : '').' /><br> 
       Unban:<input type="radio" value="0" name="name" onclick="javascript: submit()" '.   (($row['Banned'] == "0") ? 'checked=checked"' : '').' /><br /> </form></td>';
     #the above now pulls code thanks to Fred and everyone els
    #echo "<td>" . '<input type="text" name="reason" value=""> </form> </td>';
    echo "<td>" . $row['Banned_Reason'] . "</td>";
    #echo "<td>" . $row['LastLogin'] . "</td>";
    echo "</tr>";
}

echo "</table>";



} else {
    echo("Oops Something Went wrong");
}


?>

*note the above is already in php tags.

Upvotes: 2

Views: 876

Answers (2)

Kevin
Kevin

Reputation: 41885

I strongly suggest rewriting this approach, but if you want some logic inside that echo, you cannot include them inside the string and expect them to be interpolated as logic since they will be considered as strings. You can utilize a ternary in this case:

echo '
    <td>
        <form method="post" action="">
            <input type="hidden" name="id" value=' . $ID . ' />
            Ban: <input type="radio" value="1" name="unban" onclick="javascript: submit()" ' . (($row['Banned'] == 1) ? 'checked=checked' : '') . ' /> <br/> 
            Unban: <input type="radio" value="0" name="name" onclick="javascript: submit()" ' . (($row['Banned'] == 0) ? 'checked=checked' : '') . ' /> <br/>
        </form>
    </td>
';

Upvotes: 2

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Edit for future readers.

OP found the solution. Hopefully OP will post it as an answer.

i fixed it i just had to add 'checked=checked"' : ''). – Ryan


Originally posted answer for originally posted question before OP's edited question with modification to show it as being working code.

You're missing closing ) brackets for both your conditionals along with an extra quote in the other.

if({$row[\'Banned\']}==1
                        ^ bracket missing

which should read as

if({$row[\'Banned\']}==1)

and

if({$row[\'Banned\']}=="0
                       ^ ^ extra quote and bracket missing

which should read as

if({$row[\'Banned\']}==0)

Edit:

You have two <form> tags but only one closing </form> tag. You need to close them both off.

Plus this line is commented out:

#echo "<td>" . '<input type="text" name="reason" value=""> </form> </td>';

If that is part of your working code, you need to have an echo'd </form> tag and for both forms.


Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); which would have signaled it.

Upvotes: 1

Related Questions