Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组插入表失败,没有错误_Php_Pdo - Fatal编程技术网

Php 数组插入表失败,没有错误

Php 数组插入表失败,没有错误,php,pdo,Php,Pdo,我从一个post表单中成功地获得了这两个数组,并且我能够打印这两个数组,现在是插入它的时候了。这是我想要的顺序,但它不会更新我的数据库,上传文件,也不会返回任何错误 我的PDO有什么问题 Adding_a_car.php <form action="" method="post" enctype="multipart/form-data"> <ul> <li> Year:<br>

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

我的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);
打印结果($vehicleinfo)打印结果($Path)

我不能让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());

$dbh->pdo->setAttribute(pdo::ATTR_ERRMODE,pdo::ERRMODE_WARNING);
$sth=$dbh->prepare($sql);
$sth->execute($final);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常);
打印($sth->errorInfo());

由于您正在对每个以表结构命名的字段硬编码一个
,您可以这样做:

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());
}
以下是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();
}

lol我修复了它对不起,您是否验证了
$final[0]
准确地包含了您的语句所期望的元素数?使用pdo错误处理程序来知道它是否抛出了任何错误,请参阅我更改了
$path[12]
$info[21]
=33个参数=期望的语句,但仍然没有结果,我怎么能马上测试pdo插入?-1用于表结构,这显然是错误的。哇,非常感谢@Prix,但我修改了这段代码,为每个表提供了完整的键列表,因此最终我得到了33个键,车辆信息先是车辆图片就像在我的sql中看到我的更新,pdo仍然不工作我现在要尝试错误,但是阵列问题已经解决了,非常感谢各位,我非常感谢大家的支持heart@sdfasdfad更新后给我另一条评论,我会来检查。现在:)我希望我能最终开始将此发送到mysql,我准备好了一个完整的字符串,32个键,现在要将其转换为msql插入,我需要你,你必须完成我的代码,只要告诉我要做什么:)上帝保佑u@sdfasdfad好的,您的代码现在有点混乱,似乎您已经在一个文件上启动了数据库连接,并试图在第二个文件上重用它,我不知道它们是类还是直接调用,但我可以告诉你,更新的问题告诉我你的函数不知道什么是
$dbh
。简单地说,您的
$dbh=newpdo
行在哪里?在什么档案里?是在教室里吗?你在哪里使用它?您是否使用include在添加_a_car.php文件时包含您的文件?@sdfasdfad您还可以删除
$info[21]正如您在
foreach
中的
$info[]
一样。
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());
}
$final = array_merge(array_values($vehicleinfo),array_values($images));
<?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();
}