无法使用mysqli将数据存储在数据库中

时间:2017-04-23 13:28:34

标签: php mysqli

我有联系表单并尝试将数据存储在数据库中并发送到电子邮件。但是,我能够发送电子邮件,但我的数据库中没有保存数据。

我的代码如下:

<?php
/*database connection  */
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "webcontact_db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
/*Email connection*/
$captcha;
if (isset($_POST['g-recaptcha-response'])) { $captcha = $_POST['g-recaptcha-response']; }
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LfwRh0UAAAAAAKKCQxtUUGdOgIXETU2rEPRtpqz&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
if (!$captcha || $response.success == false) {
    echo "<script>alert('Veuillez valider le captcha de s\351curit\351;');window.location.href='contact.php';</script>";
    exit ;
  } 
    else {
         $title = $_POST['title'];
 $fname = $_POST['fname'];
 $lname = $_POST['lname'];
 $tel = $_POST['tel'];
 $address = $_POST['address'];
  $email = $_POST['email'];
  $message = $_POST['message'];
$sql = "INSERT INTO contact(title,fname,lname,tel,address,email,message) values('$title',$fname','$lname','$tel','$address','$email','$message')";
if(!mysqli_query($con,$sql)){
    $to = 'myemail.com';
    $subject =  $_POST['sub'];
    $message = "<strong>D\351tails du formulaire:&nbsp;</strong> "."</strong>" ."<br /><br /> " . "<strong>Nom:&nbsp;</strong>" .$_POST['title']. $_POST['fname'].$_POST['lname']."<br /><br /> " . "<strong>Tel:&nbsp;</strong>" . $_POST['tel'] ."<br /><br /> " . "<strong>Adresse:&nbsp;</strong>" . $_POST['address']. "<br /><br /> " . "<strong>E-mail:&nbsp;</strong>" . $_POST['email']. "<br /><br /> " ."<strong>Message:&nbsp;</strong>" . $_POST['message'];
    $headers = "From:" .$_POST['fname']. $_POST['lname']."$email";
    $headers .= "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $message = wordwrap($message, 70, "\r\n");
    if (mail($to, $subject, $message, $headers)) {
        echo "<script>alert('Merci ! Votre formulaire a \351t\351 envoy\351.!');window.location.href='contact.php';</script>";
    } else {
        echo "<script>('Echec de l'envoi, veuillez r\351essayer')</script>";
        exit ;
    }
}
    }
mysqli_close($conn);
?>

2 个答案:

答案 0 :(得分:0)

你在mysqli_query()函数中使用了另一个变量,即$ conns of $ conn你也有错误的condtion也希望下面的代码可以帮你删除!从条件

签名
    <?php
        /*database connection  */
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "webcontact_db";
        // Create connection
        $conn = mysqli_connect($servername, $username, $password, $dbname);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        /*Email connection*/
        $captcha;
        if (isset($_POST['g-recaptcha-response'])) { $captcha = $_POST['g-recaptcha-response']; }
        $response = file_get_contents("google.com/recaptcha/api/…; . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']."); 
        $result = json_decode($response);
        if (!$captcha || $result['success'] == false)  {
            echo "<script>alert('Veuillez valider le captcha de s\351curit\351;');window.location.href='contact.php';</script>";
            exit ;
          } 
            else {
                 $title = $_POST['title'];
         $fname = $_POST['fname'];
         $lname = $_POST['lname'];
         $tel = $_POST['tel'];
         $address = $_POST['address'];
          $email = $_POST['email'];
          $message = $_POST['message'];
        $sql = "INSERT INTO contact(title,fname,lname,tel,address,email,message) values('$title',$fname','$lname','$tel','$address','$email','$message')";
        if(mysqli_query($conn,$sql)){
            $to = 'myemail.com';
            $subject =  $_POST['sub'];
            $message = "<strong>D\351tails du formulaire:&nbsp;</strong> "."</strong>" ."<br /><br /> " . "<strong>Nom:&nbsp;</strong>" .$_POST['title']. $_POST['fname'].$_POST['lname']."<br /><br /> " . "<strong>Tel:&nbsp;</strong>" . $_POST['tel'] ."<br /><br /> " . "<strong>Adresse:&nbsp;</strong>" . $_POST['address']. "<br /><br /> " . "<strong>E-mail:&nbsp;</strong>" . $_POST['email']. "<br /><br /> " ."<strong>Message:&nbsp;</strong>" . $_POST['message'];
            $headers = "From:" .$_POST['fname']. $_POST['lname']."$email";
            $headers .= "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
            $message = wordwrap($message, 70, "\r\n");
            if (mail($to, $subject, $message, $headers)) {
                echo "<script>alert('Merci ! Votre formulaire a \351t\351 envoy\351.!');window.location.href='contact.php';</script>";
            } else {
                echo "<script>('Echec de l'envoi, veuillez r\351essayer')</script>";
                exit ;
            }
        }
            }
        mysqli_close($conn);
        ?>

答案 1 :(得分:0)

您的 $conn 只是打印错误,只需输入 $con 即可。以下代码已在此处修复。

$sql = "INSERT INTO contact(title,fname,lname,tel,address,email,message) values('$title',$fname','$lname','$tel','$address','$email','$message')";
if(!mysqli_query($conn,$sql)){
   //Some codes right here...