使用PHP

时间:2015-10-08 10:16:17

标签: php mysql webforms

大家好我是PHP的新手,需要一点帮助来解决我的问题。如果已经讨论过,我会道歉。我有一个简单的webform,有两个字段。填写这些字段后,用户单击提交按钮,数据应存储在数据库中。但是由于我不知道的原因,我无法存储它。

这是我的index.php:

<html lang="en">
<head>
<meta charset="utf-8">
<title> INSERTING DATA TO DATABASE!!! </title>
<link rel="stylesheet" href="../CSS/testbox.css" type="text/css"/>
<!-- Style sheet for form -->
</head>
<body>

<form action="../PHP/test.php" method="post">

    <div class="container">
    <p class="header">Title *</p>
        <input class="singleline" type="text" name="Title">
        <p class="footer">Meaningful, short descriptive title</p>
        <p class="header">Summary *</p>
        <textarea name="Summary"></textarea>
        <p class="footer">Brief summary of what the engagement involved.</p>
    <br>
    <input class="submit" type="submit" name="submit">
    </div>
</form>
</body>
</html>

这是mytest.php:

<?php
$username = "root"; 
$password = "root";
$hostname = "127.0.0.1:80"; 
$dbname = "casestudy";

$dbcon = mysqli_connect($hostname, $username, $password, $dbname);
if (!dbcon) 
{
die ('error connecting to database');
}
echo 'you have connected successfully';

if(isset($_POST['submit']))  
{       
$Title= $_POST['Title']; 
$Summary= $_POST['Summary'];

    echo " this is a $Title and this is a $Summary !"; 

    $sql = "INSERT INTO example (title, summary) VALUES ('$Title', '$Summary')"; 

    $result = mysqli_query($sql);
        if ($result){
            echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
            }
                else
            {
               echo "<script type='text/javascript'>alert('failed!')</script>";

            }
}
?>

我的问题是最后一步,当它试图将其保存到数据库时,我收到消息&#34;失败!&#34;而且我不知道我的错误在哪里。 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您需要将连接变量作为first参数传递:

mysqli_query($dbcon, $sql);