php变量不保存查询

时间:2016-12-15 13:27:54

标签: php

我是PHP的新手,并且一直试图在下面的陈述中找出我出错的地方:

if(empty($_POST['datepickere']))
    {
        $purchaseorderdate = mysql_query("SELECT purchaseorderDMY FROM purchaseorders WHERE Servicetag = '$_POST[stage]'");

    }
else
    {
        $purchaseorderdate = "$_POST[datepickere]";
    }

else语句执行得很好但是if语句没有将查询的输出保存到"$purchaseorderdate"

有人可以给我一个关于我哪里出错的指针吗?对不起,如果有语法错误或类似的东西。

  

datepickere =我的HTML表单中的一个字段。

     

purchaseorderDMY = DB中保存datepickere的字段   何时添加新内容

1 个答案:

答案 0 :(得分:0)

首先建立一个mysqli连接(this is right out the docs)

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

现在使用更多exact copies from the documentation更新您的代码段:

if (empty($_POST['datepickere'])) {

    $myvar = mysqli_real_escape_string($link, $_POST['stage']);

    $result = mysqli_query("SELECT purchaseorderDMY FROM purchaseorders WHERE Servicetag = '$myvar'");
    $row = mysqli_fetch_assoc($result);
    $purchaseorderdate = $row['purchaseorderDMY'];

} else {

    // you don't need to quote a variable to use it
    // but it must be quoted starting at PHP4 (a decade ago)
    $purchaseorderdate = $_POST['datepickere'];
}