-3

I'm trying to insert my forms into my database, but the "reden" input will only enter when I type it in my textbox. The values of my radio buttons don't seem to enter. How do I fix this? this is a file for making a simple registration. The varchar value of "reden" in my databse is (20) so I don't understand why it doesn't show up. It's just black.

 <?php 
    $Groep = $_POST['Groep'];
    $reden = $_POST['reden'];
    //$RDatum = $_POST[STR_TO_DATE('$$RDatum', '%m/%d/%Y')];

    date_default_timezone_set('UTC');
    $timestamp = strtotime($_POST['tijdstip']);
    $tijdstip=date("Y-m-d", $timestamp);


    if (!empty($Groep) || !empty($tijdstip) ) 
        {
        $host = "localhost";
        $dbUsername = "root";
        $dbPassword = "root"; 
        $dbname = "gevuldetomaat"; 

    //create connection
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
    if ($conn->connect_error)   {
     die("Connection failed: ". $conn->connect_error);
                                } else      {
    $INSERT ="INSERT INTO reservering (Tijdstip, reden, Groep) values(?, ?, ?)";
    $stmt = $conn->prepare($INSERT);
    $stmt->bind_param("sss",$tijdstip, $reden, $Groep);
    $stmt->execute(); // Run the INSERT statement
    if ($stmt->errno)   { // Check for errors
        die("Error in inserting: " . $stmt->error); // Print last error
                        } 
        else    { // No errors
        echo "New record inserted sucessfully"; // Success message
                }
                                                            }
    }                                               
?>

This is my html code

    <!DOCTYPE html>

<html lang="en" 
<head>
    <title>De Gevulde Tomaat</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="Reservering.css">
</head>
<body> 

<form action="Reservering.php" method="POST">
  <table>
  <tr>
    <td style="color:white">GROEP :</td>
    <td><input type="number" name="Groep" required></td>
    </tr>
<tr>
<tr>
    <td style="color:white">Reden :</td>
</tr>
<tr>
    <td><input type="radio" name="reden" value="Verjaardag">Verjaardag</td>
    <td style="color:white"><label for="Verjaardag">Verjaardag</label><br></td>
</tr>
<tr>
    <td><input type="radio" name="reden" value="Anniversary">Anniversary</td>
    <td style="color:white"><label for="Anniversary">Anniversary</label><br></td>
 </tr>
 <tr>
    <td style="color:white">Other :</td>
    <td><input type="text" name="reden"></td>
  </tr>
<tr>
    <td style="color:white">DAG :</td>
    <td><input type="date" name="tijdstip" required></td>
   </tr>
<tr>
    <td><input type="submit" value="Submit"></td>
   </tr>
  </table>
 </form>
</body> 
Quantum
  • 3
  • 5
  • Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman May 30 '20 at 23:02
  • `if (!empty($Groep) || !empty($tijdstip) )` ...don't you need both values to be non-empty? That condition only requires one of those variables to be non-empty. – mickmackusa May 30 '20 at 23:56
  • This is an **Off-topic: Typo" question because your snippet demonstrates that you know how to create a radio button, you just failed to do it in your third (`Other`) option. – mickmackusa May 31 '20 at 01:50

1 Answers1

1

Your third element named reden is not a radio input. This overwrites the radio value with the empty input.

<input type="text" name="reden">

rename the other option:

<td style="color:white">Other :</td>
<td><input type="text" name="reden_other_text" /></td>

You can use JS to have that behave differently.

Additionally, your empty check should use and not or to ensure both values are not empty.

if (!empty($Groep) && !empty($tijdstip))

and should check for $reden as well if that is required.

You can use ternary to determine which one to use:

$reden = !empty($_POST['reden_other_text']) ? $_POST['reden_other_text'] : reden_other_text$_POST['reden'];
user3783243
  • 5,368
  • 5
  • 22
  • 41
  • But if I add another name "reden_other_text", what should I change to my insert function? because I need to enter either reden or reden_other_text not both. sorry for my low comprehension I'm still pretty new to coding. I also just noticed, that my radio button values are inserting so that works, But now when I want to enter the textbox value it inserts "Other" – Quantum May 30 '20 at 22:38
  • @Quantum Just updated to show an option. – user3783243 May 30 '20 at 23:55