PHP代码可在localhost上运行,但在namecheap上托管时会给出错误

时间:2019-01-07 06:02:12

标签: php mysql mysqli

因此,我正在制作一个联系页面,该页面将从用户获取值并将其保存在数据库中。该代码在localhost中完全可以正常工作,但是当我在Namecheap主机上运行它时,它表示没有数据在传递。

包括contact.inc.php

<?php


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

include_once 'dbh.inc.php';

$first   = mysqli_real_escape_string($mysqli, $_POST['first']);
$last    = mysqli_real_escape_string($mysqli, $_POST['last']);
$email   = mysqli_real_escape_string($mysqli, $_POST['email']);
$number  = mysqli_real_escape_string($mysqli, $_POST['number']);
$message = mysqli_real_escape_string($mysqli, $_POST['message']);


if (empty($first) || empty($email) || empty($number) || empty($last)) {
    header("Location: ../index.php?contact=empty");
    exit();
}

elseif (!preg_match("/^[a-zA-Z]*$/", $first)) {
    header("Location: ../index.php?contact=firstchar&email=$email&number=$number&last=$last");
    exit();
} elseif (!preg_match("/^[a-zA-Z]*$/", $last)) {
    header("Location: ../index.php?contact=lastchar&email=$email&number=$number&first=$first");
    exit();
} elseif (!preg_match('/^[0-9]{10}+$/', $number)) {
    header("Location: ../index.php?contact=number&first=$first&last=$last&email=$email");
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("Location: ../index.php?contact=email&first=$first&last=$last&number=$number");
    exit();
} else {
    $sql  = "INSERT INTO con (user_first, user_last, user_email, user_number, user_message) VALUES (?, ?, ?, ?, ?);";
    $stmt = mysqli_stmt_init($mysqli);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        echo "SQL Error *_*";
    } else {
        mysqli_stmt_bind_param($stmt, "sssss", $first, $last, $email, $number, $message);
        mysqli_stmt_execute($stmt);
    }
    header("Location: ../index.php?contact=success");
}
}



?>

包括index.php

<form action="includes/contact.inc.php" method="POST"   
class="contact-form">
<h4 class="formh">How can we help you?</h4>
<?php
    if(isset($_GET['first'])) {
              $first = $_GET['first'];
              echo '<input type="text" name="first" placeholder=" First Name" required value="'.$first.'">';
       } else {
             echo '<input type="text" name="first" placeholder=" First Name" required>' ;

       }
     echo "<br />";

     if(isset($_GET['last'])) {
              $last = $_GET['last'];
              echo '<input type="text" name="last" placeholder=" Last Name" required value="'.$last.'">';
       } else {
             echo '<input type="text" name="last" placeholder=" Last Name" required>' ;

       }
      echo "<br />";


     if(isset($_GET['email'])) {
       $email = $_GET['email'];
       echo '<input type="text" name="email" placeholder=" Email" required value = "'.$email.'">';
     } else {
       echo '<input type="text" name="email" placeholder=" Email" required>';
     }
     echo "<br />";


     if(isset($_GET['number'])) {
       $number = $_GET['number'];
       echo '<input type="text" name="number" placeholder="Mobile (+91-97809-09887)" required value = "'.$number.'">';
     } else {
       echo '<input type="text" name="number" placeholder="Mobile (+91-97809-09887)" required>';
     }
     echo "<br />";

     if(isset($_GET['message'])) {
       $message = $_GET['message'];
       echo '<textarea  name="message" id="" cols="50" rows="15" placeholder="Message" value = "'.$message.'">'.'</textarea>';
     } else {
       echo '<textarea  name="message" id="" cols="50" rows="10" placeholder="Message"></textarea>';
     }
     echo "<br />";

     echo '<button name="submit" type="submit" class="form_button">Submit</button>';

if (!isset($_GET['contact'])) {
     exit();
     } else {
     $registerCheck =  $_GET['contact'];

     if ($registerCheck == "empty") {
     echo "<p class = 'error'>*You did not fill in all the fields.</p>";
     }
     elseif ($registerCheck == "firstchar") {
     echo "<p class = 'error'>*You used invalid character in first name.</p>";
     }
     elseif ($registerCheck == "lastchar") {
     echo "<p class = 'error'>*You used invalid character in last name.</p>";
     }

     elseif ($registerCheck == "email") {
     echo "<p class = 'error'>*You used an invalid email.</p>";
     }
     elseif ($registerCheck == "number") {
     echo "<p class = 'error'>*You used an invalid mobile number.</p>";
     }
     elseif ($registerCheck == "success") {
     echo "<p class = 'success'>*Cool! We got your message!!</p>";
     }

     }



?>

index.php具有一种形式,该形式具有method = post,其目标是contacts.inc.php。在contacts.inc.php文件中,它检查错误处理并将数据发送到数据库。这在localhost中完全可以正常工作。但是在namecheap托管服务中,它输出“ *您未填写所有字段。”,这是您跳过表格中的某些输入或contacts.inc.php文件未获取数据时的输出。

请帮助!

0 个答案:

没有答案