如何将Web窗体中的数据传输到数据库

时间:2016-05-03 22:21:03

标签: html mysql database

我确定已经被问了1000次,但是我已经看了一遍,似乎无法让它发挥作用。

形式:

<form action="sendinfo.php" method="post">               
    <h4>ID:</h4>
    <input type="text" name="CustomerID">
<h4>First name:</h4>
    <input type="text" name="FirstName">
    <h4>Last Name:</h4>
    <input type="text" name="LastName">
    <h4>Street:</h4>
    <input type="text" name="Street">
    <h4>City:</h4>
    <input type="text" name="City">
    <h4>Zip:</h4>
    <input type="text" name="Zip">
    <h4>State:</h4>
    <input type="text" name="State">
    <h4>Phone:</h4>
    <input type="text" name="Phone">
    <h4>Email:</h4>
    <input type="text" name="Email">
    <input type="submit">
</form>

sendinfo.php

    <?php
    include('connection.php');
    $dbh = con();

    $dbh->query = "INSERT INTO Customer (CustomerID, FirstName, LastName, Street, City, State, Zip, Phone, Email)
    VALUES  ('$_POST[CustomerID]', ('$_POST[FirstName]', ('$_POST[LastName]', ('$_POST[Street]', ('$_POST[City]', ('$_POST[State]', ('$_POST[Zip]', ('$_POST[Phone]', ('$_POST[Email]')";
    if (!mysql_query($user_info, $connect)) { die('Error: ' . mysql_error()); } echo “Your information was added to the database.”; mysql_close($connect); 

    ?>

connection.php

<?php
define("DB_HOST", "localhost");
define("DB_NAME", "Impact_Technologies");
define("DB_USER", "root");
define("DB_PASS", "password");

function con(){
    try {
        $db_connection = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);
        return $db_connection;
    } catch (PDOException $e) {
        echo "Sorry, there was a problem connecting to the database." . $e->getMessage();
    }
}
?>

单击提交时,不会显示任何消息,也不会在db

中输入任何信息

2 个答案:

答案 0 :(得分:1)

我知道问题是什么,('$_POST[CustomerID]', ('$_POST[FirstName]', ('$_POST[LastName]', ('$_POST[Street]', ('$_POST[City]', ('$_POST[State]', ('$_POST[Zip]', ('$_POST[Phone]', ('$_POST[Email]')

你在每个值之前打开一个括号,它应该是这样的:

('$_POST[CustomerID]', '$_POST[FirstName]', '$_POST[LastName]', '$_POST[Street]', '$_POST[City]', '$_POST[State]', '$_POST[Zip]', '$_POST[Phone]', '$_POST[Email]')

答案 1 :(得分:1)

首先,通过执行此操作简化您的代码

    $id = $_POST['custormerID'];
    $firstName = $_POST['FirstName'];
//and so on

其次,删除每个值之前的所有左括号

    $dbh->query = "INSERT INTO Customer (CustomerID, FirstName, 
LastName, Street, City, 
State, Zip, Phone, Email)
    VALUES  ('$id', '$FirstName', 
'$LastName', '$Street', '$City', 
'$State', '$Zip', '$Phone', '$Email')";
    if (!mysqli_query($user_info, $connect)) { 
die('Error: ' . mysqli_error()); 
} 
echo “Your information was added to the database.”; mysql_close($connect); 

第三, mysql被折旧,改为使用mysqli或PDO

    if (!mysql_query($user_info, $connect)) { 
die('Error: ' . mysql_error()); } 
echo “Your information was added to the database.”; mysql_close($connect);

改为:

 if (!mysqli_query($user_info, $connect)) {
 die('Error: ' . mysqli_error()); 
} 
echo “Your information was added to the database.”; mysqli_close($connect);

旁注:要么使用mysqli或PDO,请不要同时使用它们。 您可以了解PDO http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developershttp://www.w3schools.com/php/php_mysql_intro.asp