值未插入数据库 - 没有警告或错误

时间:2013-03-30 15:32:28

标签: php sql function

页面上的输出很好,没有错误或警告。它似乎应该将值插入数据库,但它不会。 var_dump($ result)结果为“boolean false”。当我从命令行插入查询时,它可以工作。

功能:

    function handlePostForm(){
        date_default_timezone_set('UTC');
    if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(isset($_POST['formSumbit']) && !empty($_POST['newheader']) && !empty($_POST['newpost']))
    {
       $varHeader = $_POST['newheader'];
       $varContent = $_POST['newpost'];
       $varDate = date('Y-m-d'); 
       $order ="INSERT INTO posts (header,content,post_date) VALUES      ('$varHeader','$varContent','$varDate')";
        $result = mysql_query($order);

    }

    else 
    echo "error";
    }
    }

除了调用函数的html表单之外:

<?php include('post_func.php'); ?>
<title>new post</title>
</head>
    <body dir="rtl">
    <h1>new post</h1>
    <form method='post' action="<?php $_SERVER["PHP_SELF"]?>">
    header:
    <input type="text" name="newheader" maxlength="100" style="width:500px;">
    post:
    <textarea  <input type="text" name="newpost" maxlength="100" style="width:500px;  height:500px;"/></textarea>

    <input type="submit" name="formSumbit" value="sumbit" />
    </form>

    <?php

    handlePostForm();

    ?>

2 个答案:

答案 0 :(得分:0)

要获取详细信息错误,您可以尝试在mysql_query()函数中使用连接处理程序,并使用mysql_insert_id来确保您的插入。还要确保在mysql_connection之后使用mysql_select_db()函数来选择数据库。

以下是申请上述内容后的代码:

 function handlePostForm(){
 global $conn;//YOur connection
    date_default_timezone_set('UTC');
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST['formSumbit']) && !empty($_POST['newheader']) && !empty($_POST['newpost']))
{
   $varHeader = $_POST['newheader'];
   $varContent = $_POST['newpost'];
   $varDate = date('Y-m-d'); 
   $order ="INSERT INTO posts (header,content,post_date) VALUES      ('$varHeader','$varContent','$varDate')";
    $result = mysql_query($order ,$conn);

}

else 
echo "error";
}
}

答案 1 :(得分:0)

if($_SERVER["REQUEST_METHOD"] == "POST"){
mysql_select_db("application", $con);
    mysql_query("INSERT INTO    program_info(prog_title,location,count,p_date,p_start,p_end,advisor_name,advisor_email,advisor_phone,stud1_name,stud1_email,stud1_phone,stud2_name,st
ud2_email,stud2_phone,program)
    VALUES ('$_POST[event_title]','$_POST[event_location]','$_POST[estimated_count]','','$_POST[event_starttime]','$_POST[event_endtime]','$_POST[advisor]','$_POST[advisor_email]','$_POST[phone]','$_POST[student1]','$_POST[student1_email]','$_POST[studentphone1]','$_POST[student2]','$_POST[student2_email]','$_POST[studentphone2]','$_POST[program])");
    $temp=$_POST['event_date'];
    echo $temp;
}else`
{ echo "error";
}