PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 23, 2022

[FIXED] How to upload a radio button value entered in an html form

 February 23, 2022     html, php, phpmyadmin, wamp     No comments   

Issue

I have made the form so that it gets the selected radio value button and it gets passed to the php section and to the database.But in the database it shows as "on" no matter what the selection is.

I have no idea where I have gone wrong

HTML form:

<form action="Database.php" name="register" method="post">
<div>
    First Name   <input type="text"  name="fname"/><br>
    Last Name    <input type="text"  name="lname"/><br>
    Email        <input type="email" name="email"/><br>
    Contact No.  <input type="text"  name="num"/><br>
    Gender <br>  <input type="radio" name="g1" value="Male"/>Male

                     <input type="radio" name="g1" value="Female"/>Female
            <br>
            <br>
</form>

PHP:

$fname      = $conn->real_escape_string($_POST['fname']);
$lname      = $conn->real_escape_string($_POST['lname']);
$email      = $conn->real_escape_string($_POST['email']);
$cnumber    = $conn->real_escape_string($_POST['num']);
$gender     = $conn->real_escape_string($_POST['g1']);

$sql="INSERT INTO data (fname,  lname, email, cnumber, gender) 

VALUES ('".$fname. "','".$lname."','".$email."', '".$cnumber."', '".$gender."')";

I expected output to be male/female but it says "on"


Solution

Using your form I have used a small code and found this works

<form action="Database.php" name="register" method="post">
    <div>
        First Name   <input type="text"  name="fname"/><br>
        Last Name    <input type="text"  name="lname"/><br>
        Email        <input type="email" name="email"/><br>
        Contact No.  <input type="text"  name="num"/><br>
        Gender <br>  <input type="radio" name="g1" value="Male"/>Male

        <input type="radio" name="g1" value="Female"/>Female
        <br>
        <input type="submit" style="min-width:100%" align="center" name='submit' value="SUBMIT">
        <br>
</form>

Your PHP side should be this:

if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
    echo "Submitted";
}
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$g1 = $_POST['g1'];   
               $sql = "INSERT INTO etrack.test SET
                    fname = '".$fname."',
                    lname = '".$lname."',
                    g1 = '".$g1."'";

    if ($conn->query($sql) === TRUE) {
        echo "";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
        echo("Error description: " . mysqli_error($con));
        echo "Error 1";
    }

I am getting the required results



Answered By - Trevor Ackermann
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,214,210

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing