数组无法在没有错误的情况下插入表中

时间:2013-06-26 03:09:07

标签: php pdo

我从一个帖子表单成功获得了这两个数组,我能够print_r这两个,现在是时候插入它了。这是我想要的顺序,但它不会更新我的数据库,它会上传文件,并且不会返回任何错误。

我的PDO出了什么问题?

Adding_a_car.php

<form action="" method="post" enctype="multipart/form-data">

    <ul>

            <li>
                Year:<br>
                <input type="text" name="year">
            </li>
            <li>
                Make:<br>
                <input type="text" name="make">
            </li>
            <li>
                Model:<br>
                <input type="text" name="model">
            </li>
            <li>
                Engine:<br>
                <input type="text" name="engine">
            </li>
            <li>
                Sound System:<br>
                <input type="text" name="sound_system">
            </li>           
            <li>
                Mileage:<br>
                <input type="text" name="mileage">
            </li>           
            <li>
                Attribute 1:<br>
                <input type="text" name="att1">
            </li>           
            <li>
                Attribute 2:<br>
                <input type="text" name="att2">
            </li>           
            <li>
                Attribute 3:<br>
                <input type="text" name="att3">
            </li>           
            <li>
                Attribute  4:<br>
                <input type="text" name="att4">
            </li>           
            <li>
                Attribute 5:<br>
                <input type="text" name="att5">
            </li>           
            <li>
                Attribute 6:<br>
                <input type="text" name="att6">
            </li>   
            <li>
                Attribute 7:<br>
                <input type="text" name="att7">
            </li>           
            <li>
                Attribute 8:<br>
                <input type="text" name="att8">
            </li>           
            <li>
                Attribute 9:<br>
                <input type="text" name="att9">
            </li>
            <li>
                Attribute 10:<br>
                <input type="text" name="att10">
            </li>           
            <li>
                Attribute 11:<br>
                <input type="text" name="att11">
            </li>           
            <li>
                Attribute 12:<br>
                <input type="text" name="att12">
            </li>   
            <li>
                Attribute 13:<br>
                <input type="text" name="att13">
            </li>   
            <li>
                Attribute 14:<br>
                <input type="text" name="att14">
            </li>   
            <li>
                Attribute 15:<br>
                <input type="text" name="att15">
            </li>

                <li>Image1: <input name="image[]" type="file" /><br /></li>
                <li>Image2: <input name="image[]" type="file" /><br /></li>
                <li>Image3: <input name="image[]" type="file" /><br /></li>
                <li>Image4: <input name="image[]" type="file" /><br /></li>
                <li>Image5: <input name="image[]" type="file" /><br /></li>
                <li>Image6: <input name="image[]" type="file" /><br /></li>
                <li>Image7: <input name="image[]" type="file" /><br /></li>
                <li>Image8: <input name="image[]" type="file" /><br /></li>
                <li>Image9: <input name="image[]" type="file" /><br /></li>
                <li>Image10: <input name="image[]" type="file" /><br /></li>
                <li>Image11: <input name="image[]" type="file" /><br /></li>
                <li>Image12: <input name="image[]" type="file" /><br /></li>


                <li><input type="submit" value="Upload" /></li>



    </ul>

</form>
<?php 
if (isset($_POST))
{

                $vehicleinfo = array
                (
                'year'          => $_POST['year'],
                'make'          => $_POST['make'],
                'model'         => $_POST['model'],
                'engine'        => $_POST['engine'],
                'sound_system'  => $_POST['sound_system'],
                'mileage'       => $_POST['mileage'],
                'att1'          => $_POST['att1'],
                'att2'          => $_POST['att2'],
                'att3'          => $_POST['att3'],
                'att4'          => $_POST['att4'],
                'att5'          => $_POST['att5'],
                'att6'          => $_POST['att6'],
                'att7'          => $_POST['att7'],
                'att8'          => $_POST['att8'],
                'att9'          => $_POST['att9'],
                'att10'         => $_POST['att10'],
                'att11'         => $_POST['att11'],
                'att12'         => $_POST['att12'],
                'att13'         => $_POST['att13'],
                'att14'         => $_POST['att14'],
                'att15'         => $_POST['att15']


                );



    error_reporting(E_ALL);

    $success = 0;
    $picturepaths[12];
    $ii = 0;
    $fail = 0;
    $pictureid = 1;
    $i = 1;
    $uploads_dir = 'images';
    $count = rand(5, 70000);
    foreach ($_FILES["image"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {

            $tmp_name = $_FILES["image"]["tmp_name"][$key];
            $name = $_FILES["image"]["name"][$key];
            $uploadfile = "$uploads_dir/$name";
            $ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
            if (preg_match("/(jpg|gif|png|bmp)/",$ext)){

                $newfile = "$uploads_dir/picture".str_pad($count++,2,'0',STR_PAD_LEFT).".".$ext;


                if(move_uploaded_file($tmp_name, $newfile)){
                    $success++;
                    $picturepaths[] = $newfile;
                    $ii++;


                }else{
                    echo "Couldn't move file: Error Uploading the file. Retry after sometime.\n";
                    $fail++;
                }
            }else{
                echo "Invalid Extension.\n";
                $fail++;
            }
        }
    }
}
echo "<br> Number of files Uploaded:".$success;
echo "<br> Number of files Failed:".$fail;
//print_r($picturepaths);
//print_r($vehicleinfo);

add_a_car($picturepaths, $vehicleinfo);

function add_a_car();

function add_a_car($picturepaths, $vehicleinfo)
{
    //$paths[12];
    //$info[21];
    $iii = 1;
    $paths = array // here we import the picture unorganized and organize them
    (
        'picture1'  => '',
        'picture2'  => '',
        'picture3'  => '',
        'picture4'  => '',
        'picture5'  => '',
        'picture6'  => '',
        'picture7'  => '',
        'picture8'  => '',
        'picture9'  => '',
        'picture10' => '',
        'picture11' => '',
        'picture12' => ''
    );
    foreach($picturepaths as $field=>$data)
    {

        $paths['picture'.$iii] = '\''.$data.'\', ';

        $iii++;
    }
    foreach($vehicleinfo as $field=>$data)
    {

        $info[] = '\''.$data.'\', ';


    }

    //i have both arrays working fully according and not missing info total of 33 

    $sql = "INSERT INTO auto(year, make, model, engine, sound_system, mileage, att1, att2,
    att3, att4, att5, att6, att7, att8, att9, att10, att11, att12, att13, att14, att15, picture1,
    picture2, picture3, picture4, picture5, picture6, picture7, picture8, picture9,
    picture10, picture11, picture12) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    print_r($paths);
    print_r($vehicleinfo);


    $sth = $dbh->prepare($sql);
    $sth->execute($final);

}

print_r($ vehicleinfo)print_r($ paths)

的结果
Notice: Undefined variable: info in C:\xampp\htdocs\cardealerwins\core\functions\users.php on line 5
Array ( [year] => 19154 [make] => cehedcad [model] => asdfasd [engine] => 454 [sound_system] => [mileage] => [att1] => [att2] => fas [att3] => [att4] => [att5] => [att6] => asdfa [att7] => sdfasdfas [att8] => [att9] => [att10] => [att11] => dfasdfa [att12] => dfas [att13] => [att14] => [att15] => ) Array ( [picture1] => 'images/picture26351.png', [picture2] => 'images/picture26352.png', [picture3] => 'images/picture26353.png', [picture4] => [picture5] => [picture6] => [picture7] => [picture8] => [picture9] => [picture10] => [picture11] => [picture12] => ) 
Notice: Undefined variable: dbh in C:\xampp\htdocs\cardealerwins\core\functions\users.php on line 48

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\cardealerwins\core\functions\users.php on line 48

我不能让pdo在尝试这个时给我错误

$dsn = 'mysql:dbname=cardealerwins;host=127.0.0.1';
    $user = 'root';
    $password = '';
    $dbh = new PDO($dsn, $user, $password);

    $sth = $dbh->prepare($sql);
    $sth->execute($final);
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

connection.php和pdo的新更新

<?php
$connect_error = 'sorry we\'re expierencing connection problems.';
mysql_connect('localhost', 'root', '') or die($connect_error);
mysql_select_db('cardealerwins') or die($connect_error);


$dsn = 'mysql:dbname=cardealerwins;host=127.0.0.1';
$user = 'root';
$password = '';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}



?>


    $dbh->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
    $sth = $dbh->prepare($sql);
    $sth->execute($final);
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    print_r($sth->errorInfo());

1 个答案:

答案 0 :(得分:0)

由于您正在为表格结构命名的每个字段对KEY进行硬编码,因此您可以这样做:

function add_a_car($picturepaths, $vehicleinfo)
{
    // we do this so we can inherit the connection 
    // from the connect.php
    global $dbh;

    // count the total needed of placeholders for the query
    $total = count($vehicleinfo) + count($images);

    // create all the placeholders
    $input_list = substr(str_repeat(',?', $total), 1);

    // create the fields list in the same order of your array
    $fields = implode(", ", array_keys($vehicleinfo));

    // since for the pictures you don't have a key we need 
    // to create the field names for it based on the amount of pictures
    for ($i = 1; $i <= count($images); $i++)
    {
        $fields .= ',picture' . $i;
    }

    // now we merge the 2 arrays into 1    
    $final = array_merge($vehicleinfo,$images);

    // here is your new SQL query
    $sql = "INSERT INTO auto ($fields) VALUES ($input_list)";

    $sth = $dbh->prepare($sql);
    $sth->execute($final);
    print_r($sth->errorInfo());
}

我不完全确定PDO是否只采用数组值,或者它必须是仅包含值的数组,但如果稍后您可以将$final更改为:

$final = array_merge(array_values($vehicleinfo),array_values($images));

这就是你的connect.php应该是这样的:

<?php
$connect_error = 'sorry we\'re expierencing connection problems.';
$dsn = 'mysql:dbname=cardealerwins;host=127.0.0.1';
$user = 'root';
$password = '';

try
{
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch (PDOException $e)
{
    echo 'Connection failed: ' . $e->getMessage();
}