2

我对此失去了理智,我真的可以使用一些方向。只是试图与 PHP 一起学习 PDO,但无法理解逻辑。我一直试图在网上找到一些可以显示此测试尝试流程的好例子的东西,但我真的很难过。

是否有人,即使你不得不对我发火(尽管像任何人一样,我不希望你这样做),给我一些关于我做错了什么的方向?我正在构建它以开始我的理解。有很多关于使用 mysqli 但不是 pdo 的信息,这让我发疯了。

提前致谢。这是代码:

<?php

# connection info to the db
$host = "--shadowed--";
$dbname = "--shadowed--";
$user = "--shadowed--";
$pass = "--shadowed--";

# pdo options/attributes
$opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); // not getting errors
# data source name
$dsn = "mysql:host=$host;dbname=$dbname";


# basic pdo connection (with added option for error handling)
if (isset($_POST['submit'])) {
    try {
        $DBH = new PDO($dsn, $user, $pass, $opt);
        $STH = $DBH->prepare("INSERT INTO data (name,email,phone,detail,cost) VALUES (:name,:email,:phone,:detail,:cost)");

        $STH->bindParam(':name', $name);
        $STH->bindParam(':email', $email);
        $STH->bindParam(':phone', $phone);
        $STH->bindParam(':detail', $detail);
        $STH->bindParam(':cost', $cost);


        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $detail = $_POST['detail'];
        $cost = $_POST['cost'];


        $STH->execute();
        echo $STH; // attempted to echo back the data, but nothing happens


    } catch (PDOException $e) {
        echo $e->getMessage(); // no errors
    }
}

echo '<form method="POST" action="">';
echo '<p>Enter the below information if you want to live:</p>';
echo 'Name: <input type="text" name="name"><br />';
echo 'E-mail: <input type="text" name="email"><br />';
echo 'Phone: <input type="text" name="phone"><br />';
echo 'Order will be generated randomly from class (once built)<br />';
echo 'Description: <input type="text" name="detail"><br />';
echo 'Cost: <input type="text" name="cost"><br />';

echo '<input type="submit" value="Do-It"></form>';

# close the connection  
$DBH = null;
?>

------------- 解决后的最终代码 -------------

------------- 解决后的最终代码 -------------

仍然是新手,所以目前无法回答我自己的问题

所以首先,我没有想出这个......真的是这里的每个人的混合体。在我尝试从我的知识中学习所有缺失的链接时,我感谢大家的帮助和时间。

主要问题似乎是,当我最初尝试使用if (isset($_POST['submit']))时,它实际上并没有做或发送任何东西。没有错误......没有数据库问题......只是一堆什么都没有。我们删除了它,发现它阻碍了(ty @Fred)。虽然这并没有改变代码的工作方式,但使用@hjpotter92 建议变得更加高效。然后我们看看如何使用这个单页提交。我最终混合使用了@Fred 和@david strachan 的建议,因为两者都没有给我正确的反应,然后我添加了一个 if/else 语句来执行检查,如果它通过了,运行 try/catch。

这不是艺术品,但我学到了很多东西并感谢帮助。另外,我认为将一些东西放在那里人们可以碰到一个完整的例子会很好。如果你们中的任何人有任何其他建议,请告诉我。在学习基础知识的同时,我也在回顾如何帮助防止 sql 注入(本测试可能没有完全涵盖)。

    #------------------ Working Code ------------------#

    # pdo options/attributes
    $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION );
    # data source name
    $dsn = "mysql:host=$host;dbname=$dbname";


    # basic pdo connection (with added option for error handling)
    if ($_SERVER['REQUEST_METHOD'] == "POST") {

        if (!$_POST['name'] || !$_POST['email'] || !$_POST['phone'] || !$_POST['detail'] || !$_POST['cost']) {
            echo "<p>Please supply all of the data! You may press your back button to attempt again minion!</p>";
            exit;
        } else {

            try {        
                $DBH = new PDO($dsn, $user, $pass, $opt);
                $STH = $DBH->prepare("INSERT INTO data (name,email,phone,detail,cost) VALUES (:name,:email,:phone,:detail,:cost)");

                $STH->bindParam(':name', $_POST['name']);
                $STH->bindParam(':email', $_POST['email']);
                $STH->bindParam(':phone', $_POST['phone']);
                $STH->bindParam(':detail', $_POST['detail']);
                $STH->bindParam(':cost', $_POST['cost']);

                $STH->execute();

            } catch (PDOException $e) {
                echo $e->getMessage();
            }
            echo "<p>Data submitted successfully</p>";

        }
    }

    echo '<form method="POST" action="">';
    echo '<p>Enter the below information if you want to live:</p>';
    echo 'Name: <input type="text" name="name"><br />';
    echo 'E-mail: <input type="text" name="email"><br />';
    echo 'Phone: <input type="text" name="phone"><br />';
    echo 'Order will be generated randomly<br />';
    echo 'Description: <input type="text" name="detail"><br />';
    echo 'Cost: <input type="text" name="cost"><br />';

    echo '<input type="submit" value="Do-It"></form>';



    # close the connection  
    $DBH = null;
    ?>
4

4 回答 4

2

要检查请求是否为 POST 类型,请使用$_SERVER['REQUEST_METHOD'] 文档

// Get POST variables
$name = isset($_POST['name']) ? $_POST['name'] : ''; 
$name = isset($_POST['email']) ? $_POST['email'] : '';
$name = isset($_POST['phone']) ? $_POST['phone'] : '';
$name = isset($_POST['detail']) ? $_POST['detail'] : '';
$name = isset($_POST['cost']) ? $_POST['cost'] : '';
If($_SERVER['REQUEST_METHOD'] == "POST") {  
        Try{
   Remainder of code
于 2013-05-14T20:13:58.707 回答
1

从以下切换排序

$STH->bindParam(':name', $name);
$STH->bindParam(':email', $email);
$STH->bindParam(':phone', $phone);
$STH->bindParam(':detail', $detail);
$STH->bindParam(':cost', $cost);


$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$detail = $_POST['detail'];
$cost = $_POST['cost'];

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$detail = $_POST['detail'];
$cost = $_POST['cost'];

$STH->bindParam(':name', $name);
$STH->bindParam(':email', $email);
$STH->bindParam(':phone', $phone);
$STH->bindParam(':detail', $detail);
$STH->bindParam(':cost', $cost);

或简单地使用:

$STH->bindParam(':name', $_POST['name']);
$STH->bindParam(':email', $_POST['email']);
$STH->bindParam(':phone', $_POST['phone']);
$STH->bindParam(':detail', $_POST['detail']);
$STH->bindParam(':cost', $_POST['cost']);
于 2013-05-14T19:15:48.173 回答
1

This is not an answer but a form validation function that could be of help.

I'm sure there are multiple ways of achieving this, but it will surely get you started.

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$detail = $_POST['detail'];
$cost = $_POST['cost'];

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

if (empty($name) || empty($email) || empty($phone))) || empty($detail))) || empty($cost)) {
// do something
   exit;
    }

if (!empty($name) || !empty($email) || !empty($phone))) || !empty($detail))) || !empty($cost)) {
// do something else
// for example, write the data in database
   exit;
    }

}
于 2013-05-14T20:03:34.793 回答
0

你为什么要让用户发送一个空白表格?使用 javascript 在客户端处理“请提供所有数据”警告。不要让表单在不满足所有要求的情况下通过 javascript。

于 2013-10-11T23:33:51.107 回答