我无法将表单信息插入到我的数据库中,我一直收到404错误

时间:2015-08-08 05:20:35

标签: php html sql mysqli xampp

您好我在将表单信息插入数据库时​​遇到了一些问题。 我收到连接到数据库回声但是当点击提交时我得到404错误。 任何帮助将不胜感激。提前谢谢!

<?php
    $host="localhost";
    $dbuser="root";
    $pass="******";
    $dbname="learn";
    $con = mysqli_connect ($host, $dbuser, $pass, $dbname);
    if (mysqli_connect_errno ())
    {
        die ("Connection Failed!" . mysqli_connect_error());
    }
    else
    {
    echo "Connected to database {$dbname} ";
    }

            if(isset($_POST['submit']))
        {
        $fname=$_POST ['fname'];
        $lname=$_POST ['lname'];
        $email=$_POST ['email'];
        $pswrd=$_POST ['pswrd'];

            $sql = $con->query ("INSERT INTO users ( fname, lname, email, pswrd,)
                     VALUES ( ' {$fname} ' , ' {$lname} ' , ' {$email} ' , ' {$pswrd} ' , ')" );
        }
?>

<!DOCTYPE html>
<html lang="en-US">
<head>
    <title>Signup</title>
    <meta charset="utf-8">
    <meta name="description" content="description of webpage">
    <meta name="keywords" content="keywords go here">
    <meta name="author" content="me">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/signup.css">
    <link rel="index" href="index.php">
    <link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png">
</head>
<body>

    <div class="header">
        <div id="logo">
                <a href="index.php"><img src="img/logo.png" alt="logo" title="learnlogo"/></a>
        </div>
    </div>

        <div id="signupform">
            <form action="submit" method="post"><br>
                <input type="text" name="fname" placeholder="First name">
                <input type="text" name="lname" placeholder="Last name"><br><br>
                <input type="text" name="email" placeholder="Email address">
                <input type="password" name="pswrd" placeholder="Password"><br><br>
                <input type"submit" value="Submit">
            </form>
        </div>
<?php
    include ("inc/footer.php");
?>

3 个答案:

答案 0 :(得分:1)

action="submit"标记中删除<form>。 action =“{URL}”属性“指定在提交表单时发送表单数据的位置” - 发送回同一网址时不需要它。

e.g。改变:

<form action="submit" method="post"><br>

为:

<form method="post"><br>

答案 1 :(得分:0)

下面,action应该是处理代码的php文件的名称。在这种情况下,可能是$ _SERVER ['PHP_SELF']。

<div id="signupform">
   <form action="submit" method="post"><br>

答案 2 :(得分:0)

您应该在另一个文件中包含您的连接信息。

创建名为Config.php 的文件为只读

<?
/* Attempt MySQL server connection. Assuming you are running MySQL
    server with default setting (user 'root' with no password) */
    $link = mysql_connect("localhost", "root", "******", "demo");

    // Check connection
    if($link === false){
        die("ERROR: Could not connect. " . mysql_connect_error());
    }

?>

创建一个名为Insert.php的文件

    <?
    include 'Config.php';
    ?>
    <?

    // Escape user inputs for security
    $first_name = mysql_real_escape_string($link, $_POST['firstname']);
    $last_name = mysql_real_escape_string($link, $_POST['lastname']);
    $email_address = mysql_real_escape_string($link, $_POST['email']);
    $pswr = mysql_real_escape_string($link, $_POST['pswrd']);

    // attempt insert query execution
    $sql = "("INSERT INTO users ( fname, lname, email, pswrd,)
                     VALUES ( ' {$fname} ' , ' {$lname} ' , ' {$email} ' , ' {$pswrd} ' , ')" );
    if(mysql_query($link, $sql)){
        echo "Records added successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysql_error($link);
    }

    // close connection
    mysql_close($link);
    ?>

您的表格:

<div id="signupform">
            <form action="Insert.php" method="post"><br>
                <input type="text" id="fname" name="fname" placeholder="First name">
                <input type="text" id="lname" name="lname" placeholder="Last name"><br><br>
                <input type="text" id="email" name="email" placeholder="Email address">
                <input type="password" id="pswrd" name="pswrd" placeholder="Password"><br><br>
                <input type"submit" value="Submit">
            </form>
        </div>