简体   繁体   English

如何使用mysql连接使用php创建简单的html表单

[英]how to create a simple html form using php with mysql connection

I have been learning web designing. 我一直在学习网页设计。 As a first I learned to create a html form using php with mysql database. 最初,我学会了使用php和mysql数据库创建html表单。

The form works perfectly. 形式完美。 But whenever I click the submit button, it shows Your file was not found error. 但是,每当我单击“提交”按钮时,它就会显示“找不到文件”错误。

Here's my html code, 这是我的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>    

And here's my php code, 这是我的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);
?>

My sql connection is Localhost, user name is root and no password. 我的sql连接是Localhost,用户名是root,没有密码。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。

PHP code must save with name PHP代码必须使用名称保存

modified.php modified.php

and save same location where html saved. 并保存html保存的位置。 remove this line 删除此行
<div form action="C:\\Users\\user\\Desktop\\Students_Profiles\\connection.php" method="post"> rewrite submit button as <input type = "submit" value="submit" /> <div form action="C:\\Users\\user\\Desktop\\Students_Profiles\\connection.php" method="post">将提交按钮重写为<input type = "submit" value="submit" />

In your php code there is small mistake in 在您的php代码中有一个小错误

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

Forgot = sign, rewrite as 忘了=符号,改写为

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

When you click on the submit button, the form is redirected to the action in your form tag 当您单击“提交”按钮时,表单将重定向到表单标签中的操作

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

in this case action="modified.php" 在这种情况下, action="modified.php"

If the page does not exist then you get the error you described. 如果该页面不存在,那么您将得到所描述的错误。

also, in your modified.php page, you need a check if the $_POST exists before you start with the insert code. 同样,在您的Modifyed.php页面中,您需要在插入代码开始之前检查$ _POST是否存在。 You where also missing a ; 你在哪里也想念;

    // 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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM