数据未发送到CPANEL数据库表

时间:2019-07-28 21:22:56

标签: php database cpanel

数据没有发送到我的C PANEL数据库表中。

我使用相同的代码连接到XAMPP数据库,并且可以正常运行,我还用我网站的另一页测试了数据库名称,密码和主机名等,它成功地将数据发送到了C PANEL数据库表。所以我知道这两个都不是问题。

<?php



session_start();
$dbServername = "160.153.153.152";
     $dbUsername = "spartanfitnessG2";
     $dbPassword = "spguest2";
     $dbName = "spartanfitness";
$errors = array();

function alert($msg) {
    echo "<script type='text/javascript'>alert('$msg');</script>";
}

$conn = new mysqli($dbServername, $dbUsername, $dbPassword, $dbName);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
if (isset($_POST['submit'])) {
  // receive all input values from the form
  $firstname = mysqli_real_escape_string($conn, $_POST['text']);
  $lastname = mysqli_real_escape_string($conn, $_POST['text1']);
  $email = mysqli_real_escape_string($conn, $_POST['text2']);
  $password_1 = mysqli_real_escape_string($conn, $_POST['text3']);
  $password_2 = mysqli_real_escape_string($conn, $_POST['text4']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($email)) { array_push($errors, "Email is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if (empty($firstname)) { array_push($errors, "first name is required"); }
  if (empty($lastname)) { array_push($errors, "last name is required"); }
  if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
    alert("Passwords do not match");
  }

  $user_check_query = "SELECT * FROM users_tbl WHERE email='$email'";
  $result = mysqli_query($conn, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists{
    if ($user['email'] === $email) {
      array_push($errors, "Email already exists");
    }

  }
if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $query = "INSERT INTO `users_tbl` (`firstname`, `lastname`, `email`, `password`) 
              VALUES('$firstname', '$lastname', '$email', '$password')";
    mysqli_query($conn, $query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";
    header('location: registrationComplete.html');

  }
}




?>`

预期结果是要在C PANEL数据库表中输入数据的结果。 该页面被重定向到“ registrationComplete.html”,并且没有显示错误。

下面是适用于其他页面的代码

<?php

    $dbServername = "160.153.153.152";
     $dbUsername = "spartanfitnessG2";
     $dbPassword = "spguest2";
     $dbName = "spartanfitness";

     $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

 if(isset($_POST['submit'])){
     if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 

     $firstName = $_POST['text1'];
     $lastName = $_POST['text2'];
     $email = $_POST['text3'];
     $text = $_POST['text4'];
     $type = $_POST['checkbox'];

     $sql = "INSERT INTO contact_tbl (firstname, lastname, email, details, type) VALUES ('$firstName', '$lastName', '$email', '$text', '$type')";

     if (mysqli_query($conn, $sql)) {
    header('Location: thankyou.html');
        } 

     }
     mysqli_close($conn);


?>

1 个答案:

答案 0 :(得分:-1)

一个可能的问题是您连接数据库的配置错误。假设您在其中输入的用户名密码是正确的,也许您可​​以尝试将$dbServername的值从"160.153.153.152"更改为{{1 }}?