Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法从网站表单填充wampserver数据库?_Php_Html_Sql_Mysqli - Fatal编程技术网

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

Php 无法从网站表单填充wampserver数据库?,php,html,sql,mysqli,Php,Html,Sql,Mysqli,每当我在我的网站中填写表单并按下submit按钮时,它不会将数据从这里推送到我创建的wampserver/phpmyadmin数据库中吗 这是我第一次这样做,我认为它几乎是工作,因为它没有给我任何错误,而运行。但我不能指出我错在哪里,我猜这很容易,但我忽略了 这是php代码: <?php function renderform($firstname, $lastname, $emailaddress, $contactnumber, $query, $error) { ?>&

每当我在我的网站中填写表单并按下submit按钮时,它不会将数据从这里推送到我创建的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('', '', '', '', '', '');
    }
}

?>

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

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

确保可以回显变量,以确保表单提交值。

对于插入到
语句中的
语句,
SET
是否有效?如果不尝试使用
将值(value1,value2,value3,…)插入表名(column1,column2,column3,…)
您在那里有各种
if
语句,您如何确认逻辑遵循您期望的路径?如果您可以使用一个理想的调试器,但失败的话,一个简单的方法是在代码中添加各种
echo
语句来指示逻辑的整体流程。就我们所知,
if(isset($\u POST['Submit'])
可能是
false
,代码正在按设计工作。是的,@Frankich。你真的有带有
Submit
name属性的按钮吗?或者它是小写的
Submit
?谢谢你的回复,@David我明天会试试你的建议。
->
(带空格)会引发语法错误。我正在编辑你的答案,以便更好地格式化它。早些时候尝试过,无法使其工作,但明天将再次尝试