PHP MySQL使用$ _POST插入查询

时间:2017-05-13 17:01:42

标签: php mysql

变量正在发挥作用;因为我正在回应他们到屏幕上。但它没有在db / table中输入任何内容。它没有告诉我任何错误代码,但它失败了。如果您需要db_conn等其他信息,请告诉我。我已经尝试了几个类似问题的答案,但他们并没有为我修复它。表格在www.mommy-info.com/contact处。页面上的第二个表单是我现在正在使用的表单。

我的表单代码:

<form action="/cq.php" method="post">
    <fieldset>
        <legend>Username/Name</legend>
        <input
                placeholder="Insert your name"
                type="text" name="user_name" id="user_name"/>
    </fieldset>
    <br>
    <fieldset>
        <legend>Email</legend>
        <input id="email"
                type="email" name="email"
               placeholder="Insert your email address"/>
    </fieldset>
    <br>
    <fieldset>
        <legend>Question</legend>
        <textarea
                placeholder="Insert your question here."
                name="question"
                rows="5"
                cols="100">

        </textarea>
    </fieldset>
    <br>
    <input type="submit" value="Submit" name="submit" id="submit"/>
</form>

cq文件

<?php
$ptitle = "Question Submitted";?>
<?php require_once $_SERVER["DOCUMENT_ROOT"] . '/head.php'; ?>
<?php require_once $_SERVER["DOCUMENT_ROOT"] . '/navigation.php'; ?>
<?php
$user_name = $_POST['user_name'];
$email = $_POST['email'];
$question = $_POST['question'];

$query = "INSERT INTO cq (user_name, email, question) VALUES ('$user_name','$email','$question')";
?>
<section class="content">
    <?php

    echo ($user_name);
    echo ($email);
    echo ($question);

    $result= mysqli_query($db_conn, $query);
    if($result){
        echo ('Your question was successfully submitted. You should receive an email
        response in 2-3 days');
    }
    else{
        echo  mysqli_error($db_conn) . '<br> Sorry, your question was not submitted. Please try again.';
    }
    mysqli_close($db_conn);
    ?>
</section>
<?php include ('footer.php');?>

1 个答案:

答案 0 :(得分:2)

我的项目中遇到了类似的问题。 尝试直接在cq.php中调用连接文件 然后让你连接变量全局。

请在此处提供您的连接文件以供审核。

<?php
$ptitle = "Question Submitted";?>
 <?php require_once $_SERVER["DOCUMENT_ROOT"] . '/head.php'; ?>
<?php require_once $_SERVER["DOCUMENT_ROOT"] . '/navigation.php'; ?>
<?php
$user_name = $_POST['user_name'];
$email = $_POST['email'];
$question = $_POST['question'];

$query = "INSERT INTO cq (user_name,email,question) VALUES ('".$user_name."','".$email."','".$question."')";


require_once('your/path/connection_file_name.php');
global $db_conn;
echo '<section class="content">';
$result= mysqli_query($db_conn, $query);
    if($result){
        echo ('Your question was successfully submitted. You should receive an email
        response in 2-3 days');
    }
    else{
        echo  mysqli_error($db_conn) . '<br> Sorry, your question was not submitted. Please try again.';
    }
    mysqli_close($db_conn);
    echo '</section>';
    ?>
   <?php include ('footer.php');?>

尝试其他仅用于调试

<?php if($result){
        echo ('Your question was successfully submitted. You should receive an email
        response in 2-3 days');
    }
    else{
        echo 'MySql Error:'.mysqli_error($connect);
    } ?>