Insert radio button value to mysql

后端 未结 3 1174
傲寒
傲寒 2021-01-07 08:32

I have created a test form just to try to send my radio button value to mysql. I am having problems with it at the moment. The code below is just a test, I want the radio bu

相关标签:
3条回答
  • 2021-01-07 08:43

    This is done by adding values to radio button input. For instance:

    <form method="post">
    <input type="radio" name="sex" value="male">Male<br>
    <input type="radio" name="sex" value="female">Female
    <input type="submit">
    </form> 
    
    0 讨论(0)
  • 2021-01-07 08:43

    You should start with closing all of your <input> tags with </input> or at least a slash at the end (like <input name="case" type="radio" id="case1"></input>).

    You should set values to your radios (like this they always return 'on'), whereas the submit button needs neither name nor value.

    EDIT:

    Define a default radio with selected in yout input tag! If none is selected, there's no case getting transmitted and PHP will throw Undefined index: case when accessing $_POST['case'].

    A good way to prevent such errors is to check if all necessary indices are set. You can do the following:

    if(isset($_POST['name']) and isset($_POST['case']) and isset($_POST['email'])) { ... }
    
    0 讨论(0)
  • 2021-01-07 08:45

       <!-- Once you have created Mysql connection and column in specified database table,-->
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "website";
    
    // Create connection
    $conn = mysqli_connect($servername, $username, $password,$dbname);
    
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    else{
    echo "Connected successfully";}           
    $sql="INSERT INTO Registration(Name,FatherName,CNIC,FCNIC,Email,Password,Contact,Gender) VALUES ('$_POST[Name]','$_POST[FatherName]','$_POST[CNIC]','$_POST[FCNIC]','$_POST[Email]','$_POST[Password]','$_POST[Contact]','$_POST[Gender]')"; 
    if (!mysqli_query($conn,$sql))
    {
      die('Error:'.mysqli_error($conn));
    }
    echo "         & 1 record added";
    mysqli_close($conn);
    
    
    
    
    ?>
     <!--after that you just need to  write this code and make sure to adjust this code because i'm posting some portion of my code."-->
    
    
    
    <h4>Gender</h4> <input type="radio" value="Male"  name="Gender"> Male 
         &nbsp;&nbsp;&nbsp; 
    <input type="radio" value="Female"   name="Gender" >Female <br><br>
            <button type="Submit">Submit</button><br>
    //This answer is for you.

    0 讨论(0)
提交回复
热议问题