为什么不设置我的变量? PHP

时间:2015-10-07 12:34:05

标签: php mysql variables post

所以$ bays $wheelnumber$lengthhours在表单中设置,并在表单发布到此处。但无论我做什么,变量都不会写入数据库。我已经尝试直接在代码中设置它们(即$bays = '5'),但仍然无法正常工作。但是当我设置':number' => $_POST['wheelnumber']时,它可以工作!!!!当我回显变量显示时,我知道它们已被设置。有人请帮忙。它正在努力!

    $bays = $_POST['bays'];
    $wheelnumber = $_POST['wheels'];
    $lengthhours = $_POST['length'];     

    //create user id
    $id = rand(1000000,99999990);

        if ($bays = ""){
        $bays = "1";}

        $length = $lengthours / $bays;

        if ($wheelnumber = ""){
        $wheelnumber = "1";}

    //create wheel number
    $name = "Wheel " . $wheelnumber;

    try {
        $connect = mysql_connect("localhost","root", "toor");
        if (!$connect) {
            die(mysql_error());
        }
        //insert into database with a prepared statement
        $stmt = $db->prepare('INSERT INTO wheels (name,number,id,stackid,length,farmid) VALUES (:name, :number, :id, :stackid, :length, :farmid)');
        $stmt->execute(array(
            ':name' => $name,
            ':number' => $wheelnumber,
            ':id' => $id,
            ':stackid' => $stack1,
            ':length' => $lengthhours,
            ':farmid' => $farmid,
        ));
    while ($bays > 0) {
                $bayname = "Bay " . $bays;
                $bid = rand(1000000,99999990);
        //insert into database with a prepared statement
        $stmt = $db->prepare('INSERT INTO bays (name,id,wheelid,length,farmid) VALUES (:name, :id, :wheelid, :length, :farmid)');
        $stmt->execute(array(
            ':name' => $bayname,
            ':id' => $bid,
            ':wheelid' => $id,
            ':length' => $length,
            ':farmid' => $farmid,
        ));
        $bays = $bays - 1;
}

2 个答案:

答案 0 :(得分:2)

您说$bays $wheelnumber$lengthhours是在表单中设置的,但您的代码从不同名称的变量中收集数据,即

$bays = $_POST['bays'];
$wheelnumber = $_POST['wheels'];
$lengthhours = $_POST['length'];     

应该是

$bays = $_POST['bays'];
$wheelnumber = $_POST['wheelnumber'];
$lengthhours = $_POST['lengthhours'];     

这些测试是错误的

if ($bays = ""){
    $bays = "1";
}

$length = $lengthours / $bays;

if ($wheelnumber = ""){
    $wheelnumber = "1";
}

等于的测试是==,因此将其更改为

if ($bays == ""){
    $bays = "1";
}

$length = $lengthours / $bays;

if ($wheelnumber == ""){
    $wheelnumber = "1";
}

您似乎也在使用mysql_数据库扩展连接,但之后尝试使用mysqli_PDO数据库扩展语法。 3个MySQL数据库扩展不能像那样互换,选择一个并坚持下去。

即。

$connect = mysql_connect("localhost","root", "toor");

正在使用mysql_数据库扩展名和

$stmt = $db->prepare('INSERT INTO wheels (name,number,id,stackid,length,farmid) VALUES (:name, :number, :id, :stackid, :length, :farmid)');

正在使用mysqli_PDO数据库扩展名。

$db来自????

此外,您似乎编写了一个Try / Catch块,但我不确定您是否编码了CATCH部分,代码不完整所以我可能错了

答案 1 :(得分:-2)

你的行

template <>
struct is_box_containter <std::vector<Box>> {
    enum { value = true };
};

template <>
struct is_box_containter <std::vector<std::reference_wrapper<Box>>> {
    enum { value = true };
};

应该是

$wheelnumber = $_POST['wheels'];