无法将数据从html表单发送到mysql

时间:2018-12-09 07:05:30

标签: php mysqli

我无法将信息从传送到mysql。我收到以下错误: 致命错误:调用未定义函数mysqli_connect()

我知道数据库连接正在工作,因为我能够将其他数据发送到数据库中。我已经在signupcontact.php上添加了连接信息,但没有执行任何操作。

由于数据库已连接,我不确定问题出在哪里。

谢谢您的帮助。

表格:

<?php
   echo "<div class='wrapper'>";
   require ('header.php');
   include ('signupcontact.php');
   include ('db_connect2.php');



?>
   <head>
   <title>Contac Information</title>
   <meta charset="utf-8"/>
   <link rel="stylesheet" href="style.css"/>

    </style>
    <script>
    function validateForm() {
    if (document.forms[0].userName.value == "") {
        alert("Name field cannot be empty.");
        return false;
    } //end if
    if (document.forms[0].userLastName.value == "") {
        alert("Last Name field cannot be empty.");
        return false;
    } // end if
    if (document.forms[0].userEmail.value == "") {
        alert("Email field cannot be empty.");
        return false;
    } // end if
    alert ("Successful!");
    return true;
    } //end function validateForm
    </script>
    </head>
    <body>


    <?php
    echo '<form method="POST"
          action="db_connect2.php"
          onsubmit="return validateForm();">
           <fieldset style="width:900px; margin:auto;"> 
           <legend class="pcenter">Subscribe for updates</legend>
<label for="userName">Name: </label><br />
<input type="text" name="userName" id="userName"/><br /><br />
<label for="Last_Name">Last Name: </label><br />
<input type="text" name="userLastName" id="userLastName"/><br /><br />
<label for="userEmail">Email: </label><br />
<input type="text" name="userEmail" id="userEmail"/>
<br /><br />
<input type="submit" value="submit" id="submit"/><br />
</fieldset> 
</form>

dbconnect:

<?php

$conn = mysqli_connect('localhost', 'root', '', 'happy_blog');

if(!$conn) {
    die("connection failed: ".mysqli_connect_error());
}

signupcontact(我在此处再次添加了连接以查看是否有帮助,什么都没有)

<?php
$dBServername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "happy_blog";

// Create connection
$conn = mysqli_connect($dBServername, $dBUsername, $dBPassword, $dBName);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
    if (isset($_POST['submit'])){
        $Name = $_POST['userName'];
        $LName = $_POST['userLastName'];
        $Email = $_POST['userEmail'];

        $Name = mysqli_real_escape_string($conn,$_POST['userName']);
        $LName = mysqli_real_escape_string($conn,$_POST['userLastName']);
        $Email = mysqli_real_escape_string($conn,$_POST['userEmail']);

        //$sql = "INSERT INTO contact (userName, userLastName, userEmail) VALUES ('".$_POST["userName"]."', '".$_POST["userLastName"]."', '".$_POST["userEmail"]."')";
        $sql = "INSERT INTO contact (userName, userLastName, userEmail) VALUES ($Name, $LName, $Email)";
        $result = mysqli_query($conn, $sql);
if ($conn->query($sql) === TRUE) {
    echo "Form submitted successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

}

0 个答案:

没有答案