使用PHP将数据插入MYSQL数据库时出现问题

时间:2015-08-13 15:50:33

标签: php mysql database

我仍然是PHP / MYSQL的初学者,我在将数据插入MYSQL数据库时遇到了困难。 (我最初尝试使用我的localhost数据库但是一旦我转移到在线服务器,一切似乎都停止了。)

现在,只要我从index.php页面提交数据,它就会刷新页面,不会添加任何数据。

但是,当我去submit.php时,一切正常,它会在我的results.php中添加一组空数据。

我的代码如下。任何帮助将不胜感激。谢谢!

的index.php

<html>
<head>
<title>POST variables</title>

<link rel="stylesheet" type="text/css" href="css/style.css" media="all">

</head>
<body>

<?php

$con = mysqli_connect('localhost', 'anas12_test', 'a1b2c3d4', 'anas12_test');

if (!$con) {
die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}

    echo '<div class="container">

            <form id="profiles">
            <div class="header">

            <h3>Hello there!</h3>
            <p>We want to know more about you! Share a few interesting details about yourself using the form below!</p>

            </div>

            <div class="sep"></div>

            <div class="inputs">

            <form action="submit.php" method="post">

            <input id="name" name="name" placeholder="Full Name" required="" autofocus="" autocomplete="on" type="text">

            <input id="email" name="email" placeholder="Email Address" required="" autofocus="" autocomplete="on" type="text">

            <input id="colour" name="colour" placeholder="Favourite Colour" required="" autofocus="" autocomplete="on" type="text">

            <input id="music" name="music" placeholder="Favourite Song" required="" autofocus="" autocomplete="on" type="text">

            <input id="superpower" name="superpower" placeholder="If you had a superhero ability, what would it be?" required="" autofocus="" autocomplete="on" type="text">  

    <button id="submit" type="submit"name="submit" value="added">Submit!</button>


            </form> </div>

            </div>'; 
?>

</body>
</html>

Submit.php     

$con = mysqli_connect('localhost', 'anas12_test', 'a1b2c3d4', 'anas12_test');


if(isset($_POST["name"])){
    $name = $_POST["name"];
} else {
    $name = "";
}

if(isset($_POST["email"])){
    $email = $_POST["email"];
} else {
    $email = "";
}

if(isset($_POST["colour"])){
    $colour = $_POST["colour"];
} else {
    $colour = "";
}

if(isset($_POST["music"])){
    $music = $_POST["music"];
} else {
    $music = "";
}

if(isset($_POST["superpower"])){
    $superpower = $_POST["superpower"];
} else {
    $superpower = "";
}

$sql = "INSERT INTO profiles (name, email, colour, music, superpower) VALUES ('$name', '$email', '$colour', '$music', '$superpower')";
if(mysqli_query($con, $sql)){
header ('location: results.php'.$query_string);
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
}

if($name !== "" && $email !== "" && $colour !== "" && $music !== "" && $superpower !== "") {

    $query_string = '?name=' . $name.'&email='.$email.'&colour='.$colour.'&music='.$music.'&superpower='.$superpower;
    header('HTTP/1.1 303 See Other');
    header ('location: results.php'.$query_string);     

} 

?>

我的结果页面。

<html>
<head>
<title>POST Success</title>
</head>
<body>
<?php

$con = mysqli_connect('localhost', 'anas12_test', 'a1b2c3d4', 'anas12_test');


if(isset($_GET["name"])){
    $name = $_GET["name"];
} else {
    $name = "no name";
}

if(isset($_GET["email"])){
    $email = $_GET["email"];
} else {
    $email = "no email";
}

if(isset($_GET["colour"])){
    $colour = $_GET["colour"];
} else {
    $colour = "no colour:";
}

if(isset($_GET["music"])){
    $music = $_GET["music"];
} else {
    $music = "music";
}

if(isset($_GET["superpower"])){
    $superpower = $_GET["superpower"];
} else {
    $superpower = "superpower";
}

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM profiles");

  echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>";
    echo "<table border='1'> <tr> <th>Name</th> <th>Email</th> <th>Favourite Colour</th>
    <th>Favourite Music</th>
    <th>Superhero Ability</th>
 </tr>";


while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['colour'] . "</td>";
echo "<td>" . $row['music'] . "</td>";
echo "<td>" . $row['superpower'] . "</td>";

         echo "</tr>";}
    echo "</table>";
  echo "</div>";
mysqli_close($con);
?>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

您的表单没有action,因此它会将表单提交到您加载页面的网址,该网址为index.php

你需要这个:

        <form id="profiles" action="Submit.php" method="POST">
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

同样注意method部分 - 没有方法,表格默认使用GET

答案 1 :(得分:0)

小心你的index.php中有两个表单。

<form id="profiles">

<form action="submit.php" method="post">

我认为第一个没用。