无法从网站表单填充wampserver数据库?

时间:2019-03-25 11:50:27

标签: php html sql mysqli

每当我在网站上填写表格并按下提交按钮时,它是不是将数据从这里推送到我创建的wampserver / phpmyadmin数据库中?

这是我第一次这样做,我认为它几乎可以正常运行,因为它在运行时不会给我任何错误。但是我无法指出我要去哪里错了,我猜这是我所忽略的简单事情

这是php代码:

<?php
function renderform($firstname, $lastname, $emailaddress, $contactnumber, $query, $error)
{
    ?><?php
    // connect to the database
    include('connect.php');
    // check if the form has been submitted. If it has, start to process the form and save it to the database
    if (isset($_POST['Submit'])) {
        // get form data, making sure it is valid
        $firstname = mysqli_real_escape_string($connection, htmlspecialchars($_POST['FirstName']));
        $lastname = mysqli_real_escape_string($connection, htmlspecialchars($_POST['LastName']));
        $emailaddress = mysqli_real_escape_string($connection, htmlspecialchars($_POST['EmailAddress']));
        $contactnumber = mysqli_real_escape_string($connection, htmlspecialchars($_POST['ContactNumber']));
        $query = mysqli_real_escape_string($connection, htmlspecialchars($_POST['Query']));
        // check to make sure both fields are entered
        if ($firstname == '' || $lastname == '' || $emailaddress == '' || $contactnumber == '' || $query == '') {
            // generate error message
            $error = 'ERROR: Please fill in all required fields!';
            // if either field is blank, display the form again
            renderform($firstname, $lastname, $emailaddress, $contactnumber, $query, $error);
        } else {
            // save the data to the database
            $query = ("INSERT INTO contact SET FirstName='$firstname', LastName='$lastname', EmailAddress='$emailaddress', ContactNumber='$contactnumber', Query='$query' ");
            $data = mysqli_query($connection, $query) or die(mysqli_error($connection));
            // once saved, redirect back to the view page
            header("Location: contact.php");
        }
    } else {// if the form hasn't been submitted, display the form
            renderForm('', '', '', '', '', '');
    }
}

?>

收集的结果应显示在wampserver / phpmyadmin数据库中,我最终将其放入云主机中。

1 个答案:

答案 0 :(得分:1)

如果变量不为空,则可以使用以下参数传递值:

$query = $this->dbConnect->prepare("INSERT INTO contact SET FirstName=?, LastName=?, EmailAddress=?, ContactNumber=?");
$query->bind_param('ssss', $firstname, $lastname, $email, $contact_no);
$query->execute();

确保您可以回显变量,以确保您的表单正在提交值。