Simple PHP task of updating database with radio buttons

11,595

The simplest way to run this update (or the way I use anyways) is by leveraging javascript. Have each section of radio blocks in its own form that submits automatically onChange, and include a hidden variable to indicate page submission. Then just write a function to update the database, based on the inputted user's id (or however you're validating) and their selection choice...

Something like:

if($_POST["submit"] == 1)
{
  $id = $_POST["id"];
  $radio = $_POST["radio"];
  $query = "UPDATE fire_alert SET warning = '$radio' WHERE id = '$id'";
  $result = mysql_query($query) or die(mysql_error());
}

You have a problem with how you formatted your view.php link, that line should be as follows:

'Update'; ?>'

Though the above won't quite work as you're intending since it's not submitting the form, so the radio button values won't be passed... instead you should do:

<form method="post" action="edit.php">
    <input type="radio" id="radio1" name="radio" value="1" onChange="this.submit()"/><label for="radio1">Safe</label>
    <input type="radio" id="radio2" name="radio" value="2" onChange="this.submit()"/><label for="radio2">Caution L1</label>
    <input type="radio" id="radio3" name="radio" value="3" onChange="this.submit()" /><label for="radio3">Caution L2</label>
    <input type="radio" id="radio4" name="radio" value="4" onChange="this.submit()"/><label for="radio4">Closed</label>
    <input type="hidden" name="id" value="<?=$row["id"]?>" />
</form>

And you'll need to edit your php to reflect that difference as well

Share:
11,595
Doctype Designer
Author by

Doctype Designer

Updated on June 04, 2022

Comments

  • Doctype Designer
    Doctype Designer almost 2 years

    Front-end designer here, not a PHP developer and I really need the help. Need to make a system that will allow the client to log into their own area to update a table. The table will have about 20 records and each row will be edited with radio buttons (4 choices). Only 2 columns, one for ID and one called status (the editable data).

    The viewer will only see the changes made by the client and a background color change to that row depending on the status, which means the editable part will have to hold 2 pieces of data (integer value to change color and the name of the status, e.g. value of the radio button to change color, and label of button to echo the text into the cell). Will deal about login system later...

    Database set-up:

    Database: test
    Table: fire_alert
    Structure:
       id (INT, PRIMARY); color(INT); warning(VACHAR);
    

    connect.php:

    <?php
    // Database Variables (edit with your own server information)
    $server = 'localhost';
    $user = 'root';
    $pass = 'root';
    $db = 'test';
    
    // Connect to Database
    $connection = mysql_connect($server, $user, $pass) 
        or die("Could not connect to server ... \n" . mysql_error());
    mysql_select_db($db) 
        or die("Could not connect to database ... \n" . mysql_error());
    ?>
    

    view.php:

    <div id="container">
    <?php
    // connect to the database
    include('connect.php');
    include("header.php");
    
    // get results from database
    $result = mysql_query("SELECT * FROM fire_alert") 
        or die(mysql_error());  
    echo "Current date - " . date("Y/m/d") . "<br /><br />";
    
    // display data in table
    echo "<table>";
    echo "<tr> <th>Area</th> <th>Status</th></tr>";
    
    // loop through results of database query, displaying them in the table
    while($row = mysql_fetch_array($result)) {
        // echo out the contents of each row into a table
        echo "<tr>";
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . $row['warning'] . '</td>';
        echo "</tr>"; 
    } 
    
    // close table>
    echo "</table>";
    
    echo '<td><a href="edit.php' . $row['id'] . '">Change Area Status</a></td>';
    ?>
    </div>
    <body>
    </body>
    </html>
    

    edit.php (client access only) no dynamic data changes yet, hardcoded HTML so far:

    <?php include("header.php"); ?>
    <?php include("connect.php"); ?>
    
    <div id="container" class="edit">
    <div id="radio1">
    
    <?
    $result = mysql_query("SELECT * FROM fire_alert") 
        or die(mysql_error());  
    echo "Current date - " . date("Y/m/d") . "<br /><br />";
    ?>
    
    <table class="edit">
        <tr><th>Area</th><th>Status</th></tr>
        <tr>
            <td>1</td>
            <td>
                <form method="post" action="edit.php">
                    <input type="radio" id="radio1" name="radio" value="1" /><label for="radio1">Safe</label>
                    <input type="radio" id="radio2" name="radio" value="2" /><label for="radio2">Caution L1</label>
                    <input type="radio" id="radio3" name="radio" value="3" /><label for="radio3">Caution L2</label>
                    <input type="radio" id="radio4" name="radio" value="4" /><label for="radio4">Closed</label>
                </form>
            </td>
        </tr>
    </table>
    
    </div>
    <?php echo '<td><a href="view.php' . $row['id'] . '">Update</a></td>'; ?>
    </div>