无法从网站检索数据

时间:2015-10-21 19:40:41

标签: php mysql

当我检查连接但是我无法检索数据时,我能够连接到mysql数据库。我是php和mysql的新手。如果有人能帮助我,我真的很感激。

这是我的代码:

<?php
$conn = mysqli_connect($servername, $username, $password);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

if (isset($_POST['submit'])) {
    $name=$_POST['name'];
    $email=$_POST['email'];
    $subject=$_POST['subject'];
    $message=$_POST['message'];
    $result=mysql_query("INSERT INTO contact('user','email','subject','message') VALUES ('$name','$email','$subject','$message')");

if ($result) {
    $message="successfully sent the query!!";
}
else
    {$message="try again!!";}
}

?>

<form action="index.php" method="post" class="form-horizontal">
    <div class="form-group">
        <label for="name" class ="col-lg-2 control-label" > Name</label>
        <div class="col-lg-7">
            <input type="text" name="name" class="form-control" id ="name" placeholder="Enter your Name">
         </div>
    </div>
</form>
</div>
<div class="col-lg-1">

</div>
<div >
    <form action="index.php" method="post" class="form-horizontal">
         <div class="form-group">
             <label for="email" class ="col-lg-2 control-label" > Email</label>
             <div class="col-lg-7">
                 <input type="text" name="email" class="form-control" id ="email" placeholder="Enter your email address">
             </div>
         </div>
    </form>
</div> <div class="col-lg-1">

</div>
<div >
     <form action="index.php" method="post" class="form-horizontal">
          <div class="form-group">
              <label for="subject" class ="col-lg-2 control-label" > Subject</label>
              <div class="col-lg-7">
                   <input type="text" name="subject" class="form-control" id ="subject" placeholder="Your Subject">
              </div>
          </div>
     </form>
</div>
<div class="col-lg-1">

</div>
<div>
     <form action="index.php" method="post" class="form-horizontal">
         <div class="form-group">
             <label for="message" class ="col-lg-2 control-label" > Message</label>
             <div class="col-lg-7">
                 <textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
              </div>
          </div> <!-- end form -->
          <div class="col-lg-1">

          </div>
          <div class="form-group">
              <div class="col-lg-7 col-lg-offset-2">
                 <button type="submit" name="submit" class="btn btn-primary">Submit</button>
              </div>
          </div>
    </form>

1 个答案:

答案 0 :(得分:2)

最终(?)更新

您可以按照这种方法学习PDO。

<?php
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
    die("Database connection failed: " . mysqli_error($conn));
}

echo "Connected successfully";

if (isset($_POST['submit'])) {

    //Escaping string, not 100% safe, also consider validating rules and sanitization
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $subject = mysqli_real_escape_string($conn, $_POST['subject']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);
    $result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message');");

    if ($result) {
        $message = "successfully sent the query!!";
    } else {
        $message="try again!!";
    }
}
?>

<form action="index.php" method="post" class="form-horizontal">
    <fieldset>
        <div class="form-group">
            <div class="col-lg-7">
                <input type="text" name="name" class="form-control" id ="name" placeholder="Enter your name">
            </div>
            <label for="email" class="control-label">Email</label>
            <div class="col-lg-7">
                <input type="text" name="email" class="form-control" id ="email" placeholder="Enter your email address">
            </div>
            <label for="subject" class ="control-label">Subject</label>
            <div class="col-lg-7">
                <input type="text" name="subject" class="form-control" id ="subject" placeholder="Your Subject">
            </div>
            <label for="message" class ="control-label">Message</label>
            <div class="col-lg-7">
                <textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
            </div>
            <button type="submit" name="submit" class="btn btn-primary">Submit</button>
        </div>
    </fieldset>
</form>

OLD

你在这里使用mysql_ *和mysqli_ *函数。此外,您还必须清理并验证您的输入。尝试使用PDO。对于此代码段,请尝试以下操作:

变化:

$conn = mysqli_connect($servername, $username, $password);

$result=mysql_query("INSERT INTO contact ('user', 'email', 'subject', 'message') VALUES ('$name', '$email', '$subject', '$message')");

用这个:

$conn = mysqli_connect($servername, $username, $password, $db_name);

$result = mysqli_query($conn, "INSERT INTO contact(user, email, subject, message) VALUES ('$name','$email','$subject','$message')");

另外,至少使用:

$name = mysqli_real_escape_string($connection, $_POST['name']);

//还有其余的$ _POST变量

此外,您只需使用一个包含所有输入的html表单。