使用用户输入将PHP中的用户数据插入表单

时间:2017-05-04 11:26:23

标签: php html

我正在尝试弄清楚如何使用PHP从HTML表单的用户输入向我的数据库插入信息,但我不是百分之百确定如何。

这是我目前所拥有的。

HTML

    <form id="playerForm" action="insert.php" method="post">
        Player Name: <input type="text" value="playerName">
        Player Race: <input type="text" value="playerRace">
        Player Class: <input type="text" value="playerClass">
        <button id="submit">Submit</button>
    </form>

PHP

<?php
  $servername = "localhost";
  $username = "username";
  $password = "password";
  $dbname = "database";

  //create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);

  //check connection
  if(!$conn){
    die("Unable to connect to MySQL: " . mysqli_connect_error());
  }

  echo "connected successfully"."<br><br>";

  //selects the different data types from the professions table
  $sql = "INSERT INTO Player (PlayerName, PlayerRace, PlayerClass)
  VALUES()";

  $result = $conn->query($sql);                 

  $conn->close();

3 个答案:

答案 0 :(得分:2)

尝试下面的代码。并且必须在“播放器”中有一个AUTO_INCREMENT字段。表

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "database";

    //create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    //check connection
    if(!$conn){
        die("Unable to connect to MySQL: " . mysqli_connect_error());
    }

    echo "connected successfully"."<br><br>";
    if(isset($_REQUEST["submit"])){
        $p_name = $_POST['playerName'];
        $p_race = $_POST['playerRace'];
        $p_class = $_POST['playerClass'];
        //selects the different data types from the professions table
        $sql = mysqli_query($conn,"INSERT INTO Player (PlayerName, PlayerRace, PlayerClass)
            VALUES('$p_name','$p_race','$p_class')");


    }             

    $conn->close();
    ?>


<form id="playerForm" method="post">
            Player Name: <input type="text" name="playerName">
            Player Race: <input type="text" name="playerRace">
            Player Class: <input type="text" name="playerClass">
            <input type="submit" name="submit" value="SUBMIT"/>
        </form>

答案 1 :(得分:1)

您需要指定表单name attribute,如果您不使用名称则无法获取数据,如果您想将输入字段设置为空,那么在这种情况下不要设置值

 <form id="playerForm" action="insert.php" method="post">
        Player Name: <input type="text" name="playerName" value="playerName">
        Player Race: <input type="text"  name="playerRace" value="playerRace">
        Player Class: <input type="text" name="playerClass" value="playerClass">
        <input type="submit" value="submit">
    </form>

如果要将输入字段设置为空,则在这种情况下不要设置值

<form id="playerForm" action="insert.php" method="post">
        Player Name: <input type="text" name="playerName" value="">
        Player Race: <input type="text"  name="playerRace" value="">
        Player Class: <input type="text" name="playerClass" value="">
        <input type="submit" value="submit">
    </form>

这是你的insert.php文件,重要的是你的表结构必须正常并且还使用自动增量和帮助你的主键

 <?php

    $link = mysqli_connect("localhost", "jgbydtoz_root", "123warcraft", "jgbydtoz_warcraft");


    if($link === false){

        die("ERROR: Could not connect. " . mysqli_connect_error());

    }


    $playerName = mysqli_real_escape_string($link, $_POST['playerName']);

    $playerRace = mysqli_real_escape_string($link, $_POST['playerRace']);

    $playerClass = mysqli_real_escape_string($link, $_POST['playerClass']);

    $sql = "INSERT INTO Player (`PlayerName`, `PlayerRace`, `PlayerClass`) VALUES ('$playerName', '$playerRace', '$playerClass')";

    if(mysqli_query($link, $sql)){

        echo "added successfully.";

    } else{

        echo "ERROR: . " . mysqli_error($link);

    }



    mysqli_close($link);

    ?>

答案 2 :(得分:1)

您需要做的第一件事是html form tutorial

您的表单输入根本没有名称值,因此您将无法在php和商店中提取任何值

这是表单的外观:

<form id="playerForm" action="insert.php" method="post">
        Player Name: <input type="text" name="playerName">
        Player Race: <input type="text" name="playerRace">
        Player Class: <input type="text" name="playerClass">
        <button id="submit" type="submit">Submit</button>
</form>

然后在insert.php中检查输入是否已设置且在处理之前是否为空。然后,如果所有内容都设置好,那么请使用mysqli prepared statements插入数据/ pdo prepared statements

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";    

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$errors = "";
$fields = array("playerName","playerRace","playerClass");

foreach ($fields as $fieldName) {

    if (!isset($_POST[$fieldName]) && !empty($_POST[$fieldName])) {

        $errors++;
        die("Fill in all the fields");
    }
}

if ($errors <= 0) {

    $playername  = $_POST['playerName'];
    $playerrace  = $_POST['playerRace'];
    $playerclass = $_POST['playerClass'];

    $sql = $conn->prepare("INSERT INTO Player (PlayerName,PlayerRace,PlayerClass) VALUES(?,?,?");
    $sql->bind_param("sss", $playername, $playerrace, $playerclass);

    if ($sql->execute()) {

        echo "data inserted";
    } else {

        echo "Error : " . $conn->error; // in live site send this to error log not browser
    }
}


?>