如何使用php文件将数据发布到数据库?[关闭]

7d7tgy0s  于 2023-06-28  发布在  PHP
关注(0)|答案(1)|浏览(109)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
4天前关闭。
Improve this question
我使用的是YouTube视频中的一个代码,每个人都说它有效。我不知道为什么我的不工作。它似乎在我的电脑上工作,但不是当我上传它,我不知道为什么。
这是我的php文件:

<?php
    require('connect.php');
    $name=$_POST('name');
    $comment=$_POST('comment');
    $submit=$_POST('submit');
    $webmaster_email = "me@mysite.com";
    $thankyou_page = "forms/thank_you.html";
    $error_page = "forms/error.html";
    if (empty($name))   { $name='Anonymous'; }
    $msg = "\r\nName: " . $name . "\r\n\r\nComment: " . $comment . "\r\n";  
    if ($submit) 
        {
            if ($comment)
            {
                $insert=mysql_query("INSERT INTO comment (name. comment) VALUES ('$name','$comment')"); 
                mail( "$webmaster_email", "Comment on Antarctica-UFO.com", $msg );
                header( "Location: $thankyou_page" ); 
            }
            else 
            {
                echo "Please fill out all fields";      
                header ( "Location: $error_page" );
            }
        }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<xhtml xmins="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" href="stylesheets/stylesheet.css">
    <title>Comment Box</title>
</head>
<body>
    <div class="comment-box">
    <font size="1">
    <br>
    <font size="3">
        <form action="comment.php" method="POST">
            <label><h4>Name</h4>
            <input type="text" name="name" placeholder="Name not required..." maxlength="40">
            </label>
            <br>
            <font size="3">
            <label><h4>Comment</h4>
            <textarea name="comment" minlength="10" maxlength="650" placeholder="Type your comment here...." required oninvalid="this.setCustomValidity('Please type a comment with at least 10 characters.')" oninput="setCustomValidity('')"></textarea>
            </label>
            <br>
            <button type="submit">Submit Comment</button>
            <br>
        </form>
    </div>
</body>
</html>

这里是connect.php的内容是这里或我上面的代码中有错误吗?

<?php

mysql_connect("MyHost","MyUsername","MyPassword");
mysql_select_db("MyDatabaseName");

?>

主机是否需要在前面有https://或其他内容?
我什么都试过了,我希望它已经起作用了,但我得到的是一张空白页。

bqucvtff

bqucvtff1#

好的,作为一个学习者,你需要考虑多种学习资源,它给你一个更好的视角来理解你要学习的概念。现在关于手头的问题,尝试使用PHP中的PDO(PHP PDO是一个数据库访问层,它为处理多个数据库提供了统一的接口)、Parameterized QueryError Handling
下面的代码是一个易于阅读(教育)但更有效的方法来保存POST Request的参数:

<?php

// Check if the request method is POST
if($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Get the submitted parameters
    $param1 = $_POST['param1'];
    $param2 = $_POST['param2'];
    $param3 = $_POST['param3'];
    /** 
     * something very important is missing here, Input Validation.
     * It's critical to validate user input before using it! Consider learning about it later on.
     */

    // Connect to the database using PDO
    $dsn = 'mysql:host=localhost;dbname=mydb';
    $username = 'myusername';
    $password = 'mypassword';

    try {
        $pdo = new PDO($dsn, $username, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // Prepare the insert statement
        $stmt = $pdo->prepare('INSERT INTO mytable(param1, param2, param3) VALUES(:param1, :param2, :param3)');

        // Bind the submitted parameters to the insert statement
        $stmt->bindParam(':param1', $param1);
        $stmt->bindParam(':param2', $param2);
        $stmt->bindParam(':param3', $param3);

        // Execute the insert statement
        $stmt->execute();

        // Output a success message or redirect
        echo 'Parameters saved successfully!';
    }
    catch(PDOException $e) {
        // Output an error message or redirect to a proper page (etc. you can have a page that show the error 500 to the user)
        echo 'Error: ' . $e->getMessage();
    }
}

相关问题