Mysql中的调查

时间:2016-05-03 07:55:58

标签: php mysql survey

我真的需要一些帮助来完成这项调查。我想将其插入phpmyadmin。我正在学习,我需要这个项目。

<!DOCTYPE html>
<html lang="en">
<head>
    <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>
</head>
<body>
    <div>
<form action="action.php" method="get">
    <h1>KÆLEDYR</h1>
    <h3>Oplysninger om dig</h3>
    Navn:<br>
    <input type="text" name="name" value="Navn"><br><br>
    Email:<br>
    <input type="email" name="email" value="navn@gmail.com"><br><br>
    Køn:<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>
    Alder:
    <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?:
    <br>
    <input type="checkbox" name="animal1" value="Hund">Hund
    <br>
    <input type="checkbox" name="animal2" value="Kat">Kat
    <br>
    <input type="checkbox" name="animal3" value="Reptil">Reptil
    <br>
    <input type="checkbox" name="animal4" value="Fugl">Fugl
    <br>
    <input type="checkbox" name="animal5" value="Fisk">Fisk
    <br>
    <input type="checkbox" name="animal6" value="Hest">Hest
    <br>
    <input type="checkbox" name="animal6" value="andet">Andet  
    <br><br>

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

    <input type="submit">
</form>
</div>

</body>
</html>

我的action.php看起来像这样。

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

<body>

<?php
/*
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 
        . "', CURRENT_TIMESTAMP);";
        */          
        // INSERT
        $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>";
    }

?>
</body>
</html>

如何继续将调查问卷纳入数据库?

2 个答案:

答案 0 :(得分:3)

尝试考虑以下事项

  1. 更改sql查询。

    $sql = "INSERT INTO `person` (`name`) VALUES('".$_GET['name']."')";
    
  2. 如果不需要POST,则将方法更改为GET,它会保持您的网址清洁并隐藏您的数据

    <form action="action.php" method="post">
    
  3. 添加名称和值以提交按钮。

    <input type="submit" name="submit" value="true">
    
  4. 使用isset函数检查将if($_GET) {替换为

    if(isset($_POST['submit'])){
    

答案 1 :(得分:1)

您以错误的方式插入数据。您的查询应该是这样的:

//FOR SINGLE ITEM
    $sql = "INSERT INTO `person` (`name`) VALUES ('".$_POST['name']."')";
//FOR MULTIPLE ITEMS
    $sql = "INSERT INTO `person` (`name`,`email`,`gender`) VALUES ('".$_POST['name']."','".$_POST['email']."','".$_POST['gender']."')";
  

请参阅PHP Insert Data Into MySQL