I have this html form, which should work like a survey:
<body class="container">
<form method = "post">
<h4>Scheint morgen die Sonne?</h4>
<input name = "sonne" type="radio" value="Ja">Ja<br>
<input name = "sonne" type="radio" value="Nein">Nein
<br>
<br>
<h4>Welches Eis isst du am liebsten?</h4>
<input type="checkbox" name="erd" value="erdbeere">Erdbeere<br>
<input type="checkbox" name="him" value="himbeere">Himbeere<br>
<input type="checkbox" name="schok" value="schokolade">Schokolode<br>
<input type="checkbox" name="van" value="vanille">Vanille<br>
<br>
<input name="anwenden" class="btn btn-info" type ="submit" value ="Anwenden">
</form>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</body>
I like to save the clicked choices into a SQL-Table by clicking the button to submit. First question is a radio button and the others are checkboxes with multiple choice possibility. The rows in SQL are all boolean
type.
I get after submitting 6
rows with always one "1"
. Others are "0"
. Instead I like to have just one row with the clicked values as "1"
or "0"
depends on what was chosen.
So how can I do this in PHP?
if ($_POST['email'] == 'Ja'){
$query = "INSERT INTO auswahl (sonne) VALUES ('1')";
} else {
$query = "INSERT INTO auswahl (sonne) VALUES ('0')";
}
if ($_POST['erd']){
$query = "INSERT INTO auswahl (erdbeere) VALUES ('1')";
} else {
$query = "INSERT INTO auswahl (erdbeere) VALUES ('0')";
}
if ($_POST['him']){
$query = "INSERT INTO auswahl (himbeere) VALUES ('1')";
} else {
$query = "INSERT INTO auswahl (himbeere) VALUES ('0')";
}
if ($_POST['schok']){
$query = "INSERT INTO auswahl (schokolade) VALUES ('1')";
} else {
$query = "INSERT INTO auswahl (schokolade) VALUES ('0')";
}
if ($_POST['van']){
$query = "INSERT INTO auswahl (vanille) VALUES ('1')";
} else {
$query = "INSERT INTO auswahl (vanille) VALUES ('0')";
}
mysqli_query($link, $query);