【问题标题】:how to create a simple html form using php with mysql connection如何使用 php 和 mysql 连接创建一个简单的 html 表单
【发布时间】:2017-07-22 18:07:22
【问题描述】:

我一直在学习网页设计。首先,我学会了使用 php 和 mysql 数据库创建一个 html 表单。

表单完美运行。但是每当我点击提交按钮时,它都会显示 Your file was not found 错误。

这是我的 html 代码,

<html>    
<head>    
    <title>Registration Form</title>    
</head>    
<body>    
    <link href = "registration.css" type = "text/css" rel = "stylesheet" />    
    <h2>Fun Quizzes</h2>    
    <form name = "form1" action="modified.php" method = "post" enctype = "multipart/form-data" >    
        <div class = "container">    
            <div class = "form_group">    
                <label>Questions:</label>    
                <input type = "text" name = "Questions" value = "" required/>    
            </div>    
            <div class = "form_group">    
                <label>Correct Answer :</label>    
                <input type = "text" name = "Correct_Answer" value = "" required />    
            </div>    
            <div class = "form_group">    
                <label>wrong - 1 :</label>    
                <input type = "text" name = "wrong_1 " value = "" required/>    
            </div>    
            <div class = "form_group">    
                <label>wrong - 2:</label>    
                <input type = "text" name = "wrong_2" value = "" required/>    
            </div>
            <div class = "form_group">    
                <label>wrong - 3 :</label>    
                <input type = "text" name = "wrong_3" value = "" required/>    
            </div>  
            <div form action="C:\Users\user\Desktop\Students_Profiles\connection.php" method="post">    
                <label>Submit:</label>    
                <input type = "submit"/>    
            </div>                      
        </div>    
    </form>    
</body>    

这是我的 php 代码,

<?php
// Grab our POSTed form values
// Note that whatever is enclosed by $_POST[""] matches the form input elements
$Questions = $_POST["Questions"];
$Correct_Answer = $_POST["Correct_Answer"];
$wrong_1 = $_POST["wrong_1"];
$wrong_2 - $_POST["wrong_2"];
$wrong_3 - $_POST["wrong_3"];


// Connect to our DB with mysql_connect(<server>, <username>, <password>)
$sql_connection = mysql_connect("localhost", "root", "");

mysql_select_db("test_series_quizz", $sql_connection);

// Probably should check to make sure the connection was successful
// But I'm too lazy...
$sql = "INSERT INTO history_of_india (
            Questions,
            Correct_Answer,
            wrong_1,
            wrong_2,
            wrong_3

        )
        VALUES (
            '$Questions',
            '$Correct_Answer',
            '$wrong_1',
            '$wrong_2',
            '$wrong_3'

        )"

mysql_query($sql, $sql_connection);

mysql_close($sql_connection);
?>

我的sql连接是Localhost,用户名是root,没有密码。任何帮助,将不胜感激。提前致谢。

【问题讨论】:

  • 您的 php 页面是否名为 modified.php ?在我看来,您的问题是找不到该文件。确保您在 action 属性或表单中拥有正确的路径和名称。
  • 谢谢@PatrickSimard 没有
  • 现在我更改了我的 php 文件名 connection.php
  • 你的问题呵呵
  • 你能告诉我如何使用提交按钮发送数据

标签: php html mysql forms http-post


【解决方案1】:

PHP代码必须用名字保存

修改.php

并保存 html 保存的相同位置。 删除此行
&lt;div form action="C:\Users\user\Desktop\Students_Profiles\connection.php" method="post"&gt; 将提交按钮重写为&lt;input type = "submit" value="submit" /&gt;

在你的php代码中有一个小错误

$wrong_2 - $_POST["wrong_2"];
$wrong_3 - $_POST["wrong_3"];

忘记=符号,改写为

$wrong_2 = $_POST["wrong_2"];
$wrong_3 = $_POST["wrong_3"];

【讨论】:

  • 它显示了这个页面文件:///C:/Users/user/Desktop/Students_Profiles/modified.php 以及准确的 php 代码。但不要在mysql中插入任何东西
  • 您检查过本地服务器(apache 服务器)是否运行?
  • 我找不到的人
  • 它只是重定向到 php 文件 file:///C:/Users/user/Desktop/Students_Profiles/modified.php 并且不向 mysql 中插入任何数据
  • 你是如何运行html代码的?直接将文件位置放入浏览器或从 localhost 运行?
【解决方案2】:

当您点击提交按钮时,表单将被重定向到您的表单标签中的操作

<form name = "form1" action="modified.php" method = "post" enctype = "multipart/form-data" >

在这种情况下action="modified.php"

如果页面不存在,那么您将收到您描述的错误。

另外,在您的 modified.php 页面中,您需要在开始插入代码之前检查 $_POST 是否存在。你哪里还缺一个;

    // Grab our POSTed form values
    // Note that whatever is enclosed by $_POST[""] matches the form input elements
    $Questions = $_POST["Questions"];
    $Correct_Answer = $_POST["Correct_Answer"];
    $wrong_1 = $_POST["wrong_1"];
    $wrong_2 - $_POST["wrong_2"];
    $wrong_3 - $_POST["wrong_3"];


    // Connect to our DB with mysql_connect(<server>, <username>, <password>)
    $sql_connection = mysql_connect("localhost", "root", "");

    mysql_select_db("test_series_quizz", $sql_connection);

    // Probably should check to make sure the connection was successful
    // But I'm too lazy...
    $sql = "INSERT INTO history_of_india (
                Questions,
                Correct_Answer,
                wrong_1,
                wrong_2,
                wrong_3

            )
            VALUES (
                '$Questions',
                '$Correct_Answer',
                '$wrong_1',
                '$wrong_2',
                '$wrong_3'

            )";

    mysql_query($sql, $sql_connection);

    mysql_close($sql_connection);
}else{
    echo "Nothing submitted";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2015-03-31
    相关资源
    最近更新 更多