0

抱歉,这是一个重复的问题,但由于某种原因,我无法对收到的答案发表任何评论,并且该问题尚未得到解答。

当我的表单被提交(通过 Ajax)时,我收到以下错误消息:

PHP 警告: mysqli_query() [function.mysqli-query]:第 32 行 /home1/xenongro/public_html/testing/enrolment/thanks.php 中的空查询

我怀疑这与 if/else 语句有关,但不确定实际问题是什么。作为测试,我删除了 if/else 语句并成功地从表单中提交了一些值。不幸的是,当我使它成为条件时,它出现了上述错误。

任何人都可以帮忙吗?

<?php

$firstname = htmlspecialchars(trim($_POST['fname']));
$lastname = htmlspecialchars(trim($_POST['lname']));
$worktel = htmlspecialchars(trim($_POST['worktel']));
$funding = htmlspecialchars(trim($_POST['funding']));
$level = htmlspecialchars(trim($_POST['level']));

$dbc = mysqli_connect('localhost', 'xxxxx', '<xxxx>', 'xxxx')
or die ('Could not connect to MySQL server.');

if ($level != "IOSH Managing Safely"){
    if ($funding == "Self Funding"){
        $query = "INSERT INTO enrolments (fname, lname, worktel)" . 
        "VALUES ('$firstname', '$lastname', '$worktel')";
    }
    else if ($funding == "Employer Funding"){
        $query = "INSERT INTO enrolments (fname, lname, worktel)" . 
        "VALUES ('$firstname', '$lastname', '$worktel')";
    }
}
else if ($level == "IOSH Managing Safely"){
    if ($funding == "Self Funding"){
        $query = "INSERT INTO enrolments (fname, lname, worktel)" . 
        "VALUES ('$firstname', '$lastname', '$worktel')";
    }
    else if ($funding == "Employer Funding"){
        $query = "INSERT INTO enrolments (fname, lname, worktel)" . 
        "VALUES ('$firstname', '$lastname', '$worktel')";
    }
}

$result = mysqli_query($dbc, $query)
or die ('error querying database');
mysqli_close($dbc);

?>
4

4 回答 4

0

当您使用 else if 在 else if 块内执行查询并在注释中恢复时

$result = mysqli_query($dbc, $query) //Inside the else if block
于 2012-10-17T11:38:38.327 回答
0

如果$funding不等于“Self Funding”和“Employer Funding”,$query将为空。
否则$query不能为空。

于 2012-10-17T11:45:51.030 回答
0

如果您通过 ajax 发布并使用 jquery,您应该首先像这样提交,否则它不会将任何 post 值带到 ajax url

$.ajax ({
        type:'post',
        url:"domainPath",
        success: function(response) {
            if(!response.trim()) { ....
于 2012-10-17T11:40:57.987 回答
0

试试这个解决方案(在 $ErrorsRow 中,您可以标记所有表单错误):

if(isset($_POST['submit_button'])){
    $Data = $_POST;
    foreach ($Data as $key => $value) {
        if(!empty($value)) {
            $DataRow[$key] = htmlspecialchars(trim($value));
        } else {
            $ErrorsRow[] = 'Empty field '. $key;
        }
    }

    if($DataRow && !isset($ErrorsRow)){
        $firstname = $DataRow['fname'];
        $lastname  = $DataRow['lname'];
        $worktel   = $DataRow['worktel'];
        $funding   = $DataRow['funding'];
        $level     = $DataRow['level'];

    $dbc = mysqli_connect('localhost', 'xxxxx', '<xxxx>', 'xxxx')
    or die ('Could not connect to MySQL server.');

    if ($level != "IOSH Managing Safely"){
     if ($funding == "Self Funding"){
      $query = "INSERT INTO enrolments (fname, lname, worktel)" . 
    "VALUES ('$firstname', '$lastname', '$worktel')";
    }
    else if ($funding == "Employer Funding"){
    $query = "INSERT INTO enrolments (fname, lname, worktel)" . 
    "VALUES ('$firstname', '$lastname', '$worktel')";
    }
    }
    else if ($level == "IOSH Managing Safely"){
    if ($funding == "Self Funding"){
    $query = "INSERT INTO enrolments (fname, lname, worktel)" . 
    "VALUES ('$firstname', '$lastname', '$worktel')";
    }
    else if ($funding == "Employer Funding"){
    $query = "INSERT INTO enrolments (fname, lname, worktel)" . 
    "VALUES ('$firstname', '$lastname', '$worktel')";
        }
        }
    if(isset($query)) {
        $result = mysqli_query($dbc, $query) or die ('error querying database');
        mysqli_close($dbc);
    }


    } else {
        if(isset($ErrorsRow) && !empty(implode($ErrorsRow))){
            $Errors = implode(',', $ErrorsRow);
            echo 'some error message'. $Errors; 
        }

    } 
}
于 2012-10-17T11:51:33.637 回答