使用变量插入查询

时间:2013-04-10 13:01:18

标签: php insert user-profile

我有以下查询

function Add_Oddjob ($Add_Oddjob){
            //global $MemberID;
            //$update = array();
            array_walk($Add_Oddjob, 'array_sanitize');

            //foreach($Add_Oddjob as $field=>$data){ //loop through update data in Add_Oddjob.php
                //$update[] = '`' . $field . '` = \'' . $data . '\''; 
            }
            //mysql_query("INSERT INTO `oddjob`($field) VALUES ($data)") or die (mysql_error());
            echo "INSERT INTO `oddjob`(`MemberID`, `OddJobName`, `Description`,`DaysAvailable`) VALUES ('$MemberID','".$_POST['OddJobName']."', '".$_POST['Description']."','$daysavailable')";
            die ();

echo输出以下内容:

Notice: Undefined variable: MemberID in \oddjobexchange\core\functions\user.php on line 69

Notice: Undefined variable: daysavailable in \oddjobexchange\core\functions\user.php on line 69
INSERT INTO `oddjob`(`MemberID`, `OddJobName`, `Description`,`DaysAvailable`) VALUES ('','Painting', 'Test ','')

这是我在其他页面中使用的数组:

        if (isset($_POST['OddJobName']) && isset($_POST['Description']) && isset($_POST['DaysAvailable']) && empty($errors) === true){//if (empty($_POST) === false && empty($errors) === true) { //if (isset(empty($_POST['OddJobName'])) && isset(empty($_POST['Description'])) && isset(empty($_POST['DaysAvailable'])) === false && empty($errors) === true)
        $daysavailable='';
        foreach ($_POST['DaysAvailable'] as $value)
        {

        $daysavailable .=$value." ";
        }

        $Add_Oddjob = array (
            'MemberID'      => $MemberID,
            'OddJobName'    => $_POST['OddJobName'],
            'Description'   => $_POST['Description'],
            'DaysAvailable' => $daysavailable, 

            );

            Add_Oddjob ($Add_Oddjob);

            if(success){
             header('Location: member.php?username='.$username);
             exit ();
            }
        } else if (empty($errors) === false){
            //otherwise output errors
            echo output_errors($errors);
        }

如何才能使$MemberID$daysavailable在查询中发挥作用?

提前致谢。

1 个答案:

答案 0 :(得分:2)

您不能像这样使用数组变量。使用喜欢,

$Add_Oddjob['MemberID']

此外,它不是$daysavailable。它应该是$Add_Oddjob['DaysAvailable']