SQL Question

Survey in Mysql

I really need some help with this survey. I want to insert it into phpmyadmin. I'm studying and i need this for a project.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" type="text/css"/>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="script.js"></script>
<form action="action.php" method="get">
<h3>Oplysninger om dig</h3>
<input type="text" name="name" value="Navn"><br><br>
<input type="email" name="email" value="navn@gmail.com"><br><br>
<input type="radio" name="gender" value="male" checked> Mand<br>
<input type="radio" name="gender" value="female"> Kvinde<br>
<input type="radio" name="gender" value="other"> Ved ikke<br><br>
<input type="number" name="age" value="ex.20"><br><br>

<h3>Har du kæledyr?</h3>
Har du kæledyr?:<br>
<input type="radio" name="yes" value="yes" checked>Ja<br>
<input type="radio" name="no" value="no">Nej<br><br>

Hvis ja, hvor mange har du?:
<input type="number" name="pet-number" value="ex.20"><br><br>

Hvilke kældedyr har du?:
<input type="checkbox" name="animal1" value="Hund">Hund
<input type="checkbox" name="animal2" value="Kat">Kat
<input type="checkbox" name="animal3" value="Reptil">Reptil
<input type="checkbox" name="animal4" value="Fugl">Fugl
<input type="checkbox" name="animal5" value="Fisk">Fisk
<input type="checkbox" name="animal6" value="Hest">Hest
<input type="checkbox" name="animal6" value="andet">Andet

Kældedyrs quiz:
En dalmatiner er stribet?:
<input type="radio" name="true" value="true" checked>Sandt<br>
<input type="radio" name="false" value="false">Falsk<br><br>
En hest har 5 ben:
<input type="radio" name="true" value="true" checked>Sandt<br>
<input type="radio" name="false" value="false">Falsk<br><br>

<input type="submit">


My action.php looks like this.

<!doctype html>
<meta charset="utf-8">
<title>Untitled Document</title>


File: action.php
Purpose: INSERT INTO ...

$mysqli = new mysqli("localhost", "root", "","survey"); // creates the object
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; // if error messages

echo "You're connected to the database via: "
. $mysqli->host_info
. "\n";

if($_GET) {
$fn = $_GET['firstName'];
$ln = $_GET['lastName'];

// format the sql
$sql = "INSERT INTO `sakila`.`actor` (`actor_id`, `first_name`, `last_name`, `last_update`) VALUES (NULL, '"
. $fn
. "', '"
. $ln
$sql = "INSERT INTO `person` (`name`, '".$_GET['name']."')";
echo $_GET["name"];

$insert = $mysqli->query($sql);

echo "<p>et svar her ....</p>";
echo $sql;

else {
echo "<p>Error: Use the form please. No GET got.</p>";


How do i proceed and get my survey into the database?

Answer Source

You're inserting the data in the wrong way. Your query should be like this:

    $sql = "INSERT INTO `person` (`name`) VALUES ('".$_POST['name']."')";
    $sql = "INSERT INTO `person` (`name`,`email`,`gender`) VALUES ('".$_POST['name']."','".$_POST['email']."','".$_POST['gender']."')";

See PHP Insert Data Into MySQL