使用PDO从动态表单保存数据

时间:2017-12-12 03:23:14

标签: php mysql pdo dynamicform

我正在尝试从动态表单中保存数据库中的数据。我卡住了,我无法理解如何正确保存这些数据。我有以下文件。

的config.php

<?php
class DatabaseConnection{
public $pdo;
public function __construct(){
    $user = 'user';
    $pass = 'pass';
    $dsn  = 'mysql:charset=utf8;database;host=localhost;charset=utf8';
    $opt  = array(
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    );
    $this->pdo = new PDO($dsn, 'editor17_admin', 'Eciim_DB_17', $opt);
}
}

?>

的functions.php

class test{
   function __construct($db){
      $this->db= $db;
   }

   public function savedata($field1,$field2,$field3$,field4,$field5){
      $query = $this->db->prepare("INSERT INTO tabletest(fone,ftwo,fthree,ffour,ffive) VALUES (?,?,?,?,?)");
    $query->execute(array($field1,$field2,$field3$,field4,$field5));
      return true;
   }

   public function register($ex1,$ex2,$ex3,$ex4){
     $query = $this->db->prepare("INSERT INTO userdata(name,password,age,email) VALUES (?,?,?,?)");
     $query->execute(array($ex1,$ex2,$ex3,$ex4));
     return true;
   }

   public function login($ex1,$ex2){
      $query = $this->db->prepare("SELECT id,name FROM userdata WHERE name=? AND pass=?");
    $query->execute(array($ex1,$ex2));
    $userdata = $query->fetch();

    $num=$query->rowCount();
    if($num==1){
        session_start();
        $_SESSION['login']=true;
        $_SESSION['uid']=$data['id'];
        $_SESSION['uname']=$data['name'];
        $_SESSION['login_msg'] = "Welcome!";
        return true;
    }else{
        return false;
    }
}

savedata.php

<?php
session_start();
require_once "functions.php";
$db = new DatabaseConnection();
$user = new test($db->pdo);

$uid = $_SESSION['uid'];
$username = $_SESSION['uname'];
$nombre = $_SESSION['name'];

if(!$user->getSession()){
  header('Location: login.php');
  exit(); 
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>

        $(document).ready(function(e){
          var html= '<p /><table><tr><td><input type="text" name="field1[]" id="field1child" placeholder="Year"></td><td><input type="text" name="field2[]" id="field2child" placeholder="Month"></td><td><input type="text" name="field3[]" id="field3child" placeholder="Name"></td> <td><input type="text" name="field4[]" id="field4child" placeholder="Surname"></td><td><input type="text" name="field5[]" id="field5child" placeholder="Nickname"></td></tr></table>';

                $("#add").click(function(e){
                    $("#container").append(html);
                });
         });

      </script>
   </head>
   <body>
     <div>

        <?php
            if($_SERVER['REQUEST_METHOD'] == 'POST'){
                $field1   = $_POST['field1'];
                $field2   = $_POST['field2'];
                $field3   = $_POST['field3'];
                $field4   = $_POST['field4'];
                $field5   = $_POST['field5'];


                $save = $user->savedata($field1,$field2,$field3,$field4,$field5);

                        if($save){
                            echo "Data saved succesfully";
                        }
                    }
?>

<form action="" method="post" name="reg">
<div id="container">
                <table>
                    <tr><td><input type="text" name="field1[]" id="field1" placeholder="Year"></td> 
                        <td>

                        <td><input type="text" name="field2[]" id="field2" placeholder="Month"></td> 
                        <td><input type="text" name="field3[]" id="filed3" placeholder="Name"></td> 
                        <td><input type="text" name="field4[]" id="field4"       placeholder="Surname"></td> 
                        <td><input type="text" name="field5[]" id="field5" placeholder="Nickname"></td> 
                        </tr>

                </table>
                </div>
            <a href="#" id="add"> More inputs </a>
            <input type="submit" name="submit" value="Send data">

    </form>
    <a href="logout.php">Disconnect</a>

</div>
</body>
</html>

我可以注册新用户并正确登录。 我知道我需要一个foreach来保存动态表单中的数据,问题是我不知道如何正确编写它,也不知道在哪里放它。

1 个答案:

答案 0 :(得分:0)

您应该阅读本手册"How do I create arrays in a HTML ?"

基本上如果你有一个没有Array的html表单,你可以直接读取数据:

<form method="POST">
    <input type="hidden" name="foo" value="bar" />
</form>

// var_dump($_POST);
// output: array(1) { ["foo"]=> string(3) "bar" }

但是,如果表单作为数组发送如下,则应该使用foreach来解析数据:

<form method="POST">
    <input type="hidden" name="foo[]" value="bar" />
    <input type="hidden" name="foo[]" value="bar2" />
</form>

// var_dump($_POST);
// output: array(1) { ["foo"]=> array(2) { [0]=> string(3) "bar" [1]=> string(4) "bar2" } }
// foreach ($_POST['foo'] as $value) { ... }