将表单导出数据加载到php数组中

时间:2017-02-25 09:46:34

标签: php html forms

我正在尝试这种方式,但它不起作用:

  $sql = "INSERT INTO customers (userName,password,email,tel,companyName,idNumber,streetAddress,zipCode,city,coAdress) VALUES ($_POST['userName'],$_POST['password'],$_POST['email'] ,$_POST['tel'],$_POST['companyName'],$_POST['idNumber'],$_POST['streetAddress'],$_POST['zipCode'],$_POST['city'],$_POST['coAdress']);

 if ($conn->query($sql) === TRUE) {
     echo "New record created successfully";
 } else {
     echo "Error: " . $sql . "<br>" . $conn->error;
 }

我现在终于能够看到输出:

form.php的

<form class="form" method="post" id="compReg" action="process.php">
<input type="submit">

connect.php

 <?php
 print_r($_POST);?>

导出字段时的输出是:

Array ( [name] => Hedda Calhoun [email] => pinecozy@gmail.com [tel] => +539-48-6021045 [password] => Pa$$w0rd! [cpassword] => Pa$$w0rd! [companyName] => Underwood and Christian LLC [personalNumber] => 119 [streetAdress] => Enim officia et at numquam est voluptas placeat nulla et corporis dolorem in eum modi ratione incidunt nihil [zipCode] => 58366 [city] => Unde quis vel quam eiusmod rerum quaerat voluptatum labore provident [coAdress] => Nostrud culpa sint quis ab est in obcaecati illo consequat Ducimus nulla )

如何将这些加载到php数组中,以便我可以访问并将它们导出到数据库?

2 个答案:

答案 0 :(得分:1)

您可以使用密钥

进行访问
$_POST['name']
$_POST['email']
$_POST['tel']

用于在数据库中插入记录

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

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO USERS (name, email, tel)
VALUES ('".$_POST['name']."', '".$_POST['email']."', '".$_POST['tel']."')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

答案 1 :(得分:0)

默认情况下,$ _POST是通过post方法传递的变量的关联数组。因此,您可以将此数组的值作为

访问
$_POST['your_input_fieldname']

但是,如果要绑定新数组,可以在$ _POST上触发foreach循环

$newarr = array();
foreach($_POST as $key=>$val)
{
    $newarr[$key] = $val;
}
print_r($newarr);