Adding radio button values to MYSQL table using PHP

Adding radio button values to MYSQL table using PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing Radio Button Submission in Legacy PHP

When building forms to capture single-choice data like "tall or short," developers often assume that if the HTML looks correct, the database insertion will work automatically. In the scenario provided, the form structure appears standard: a set of radio inputs named q1 with integer values ranging from 1 to 5. However, the code in save.php fails to persist data. The root cause isn't a mystery with how browsers handle radio groups; it is almost certainly a collision between legacy database extension syntax and modern PHP security practices.

The immediate red flag lies in the mysql_query() function used in save.php. This function belongs to the deprecated MySQL API, which was removed in PHP 7.0. If you are running on a current server environment, this code will not execute at all, resulting in a fatal error rather than a silent failure. Even if you are maintaining legacy code on an older PHP version, relying on mysql_* functions is dangerous because they offer no protection against SQL injection.

In the provided snippet, $q1 is retrieved directly from $_POST['q1'] and concatenated into the query string without sanitization:

$q1=$_POST['q1'];
mysql_query("INSERT INTO `assessment` (q1) VALUES ('$q1')");

While radio buttons guarantee that only one value is sent (assuming the form submits successfully), this naive concatenation is a recipe for injection if the input logic changes later. Furthermore, the use of backticks around column names suggests a specific database structure, but the lack of an explicit connection handle in the query string (e.g., mysql_query($sql, $con)) implies that connection.php might be using global variables or a deprecated resource leak pattern common in early PHP tutorials.

To resolve this, you must migrate to MySQLi or PDO. Assuming the goal is simply to capture the selected radio value, here is how a secure implementation using MySQLi looks:

<?php
session_start();
include('connection.php'); // Ensure this returns a mysqli connection object

// Retrieve the post data safely
$q1 = isset($_POST['q1']) ? $_POST['q1'] : null;

if ($q1 !== null) {
    // Prepare the statement to prevent SQL injection
    $stmt = $con->prepare("INSERT INTO `assessment` (q1) VALUES (?)");
    $stmt->bind_param("i", $q1); // 'i' denotes an integer type
    
    if ($stmt->execute()) {
        echo "Data saved successfully.";
        header("Location: question2.php");
        exit();
    } else {
        echo "Error saving data: " . $stmt->error;
    }
    
    $stmt->close();
}
?>

This approach ensures that the integer value from the radio button is typed correctly and injected safely into the database. It also handles the case where the user might skip the form, ensuring $q1 isn't treated as an empty string which could cause type errors in a strictly typed column.

Another potential pitfall in the original code is the placement of the closing PHP tag ?> followed by HTML content in question1.php. While PHP ignores whitespace after ?>, accidental extra characters can break scripts. Ensure that include 'includes/overall/overall_footer.php'; sits before the final closing tag if it contains any logic, or remove the ?> entirely to avoid parsing errors.

For modern projects, specifically those built with the Laravel ecosystem, handling this logic is even more streamlined. You would typically define a form request to validate that q1 is an integer between 1 and 5 before attempting insertion. This validation layer prevents invalid data from ever reaching the database query stage, reducing server load and improving application stability.

Ultimately, upgrading from mysql_* to a prepared statement API isn't just about fixing a "broken" script; it is a fundamental shift toward secure coding standards that prevent catastrophic data breaches. Always verify your database connection object exists before executing queries, and never trust user input without validation or sanitization.

Our network