在POSTING大数组时,Mysql INSERT语句FAILING

时间:2015-01-30 11:56:35

标签: php mysql oop pdo

我一直在网上搜索,然后把头发拉出来"过了好几天。它在我的XAMPP localhost上工作正常,并且在我的在线测试服务器上工作正常,直到我更新了PHP版本,并且由于语法弃用而不得不重写代码。

基本上,我正在为摄影客户制作后端数据库。其中一个表用于存储图像信息。我还没有试图存储一个真实的图像(某种BLOB),我只想存储"什么和哪里"。

似乎正在发生的事情是,如果我尝试输入具有数百张图像的拍摄目录的内容,当我点击输入时屏幕发生变化,然后不是告诉我输入了多少图像,而是进入" 418未使用"页面说

  

服务器遇到内部错误或配置错误,无法完成您的请求。

我一直试图缩小哪些缓冲区要增加,或者变量类似于" max_allowed_pa​​cket"," max_input_vars" ...仍然没有运气。我甚至尝试比较两台服务器之间的phpinfo,找出为什么有效,另一台没有...

这是我正在做的......列表页

<?php
// set page headers
$page_title = "Enter Images into Database";
include_once 'auth.php';

// get database connection
include_once 'config/fpaddb.php';
include_once 'objects/clients.php';
include_once 'objects/photoshoots.php';
include_once 'objects/images.php';

$database = new Database();
$db = $database->getConnection();

$colname_chk_Images = "-1";
if (isset($_GET['ShootId'])) {
  $colname_chk_Images = $_GET['ShootId'];
}
$colname1_chk_Images = "NULL";
if (isset($_GET['ShootFolder'])) {
  $colname1_chk_Images = $_GET['ShootFolder'];
}

$colname_get_Images = "-1";
if (isset($_SESSION['cID'])) {
  $colname_get_Images = $_SESSION['cID'];
}
$entered=0; //check for already entered images

?>
<?php
$dirname=$_SESSION['cIFolder'];
$Clogin=$_SESSION['Clogin'];
$ClientID=$_SESSION['cID'];
$_SESSION['CURR_CLIENT_ID'] = $ClientID;
$maindir=$_GET['ShootFolder'];
$ShootId=$_GET['ShootId'];
$dir=$_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['PHP_SELF'])."protect/clientfolders/".$Clogin."/users/".$Clogin."/images/".$maindir;
$_SESSION['dir']=$dir;
$dir2="/protect/clientfolders/".$Clogin."/users/".$Clogin."/images/".$maindir;
$dirt= "/phpThumb-master/";
$dirn= dirname($_SERVER['PHP_SELF']);
$filesArray=array_map('basename', glob($dir."/*.jpg"));
$lightbox_data= "FPAD_Lightbox";
$thumb =    "$dir2/";
$notThumb =   "$dir2/";
$ic = count($filesArray);
$_SESSION['SESS_TOTNUM'] = $ic;
$_SESSION['sID'] = $ShootId;
$sID = $_SESSION['sID'];

include_once 'header_a.php';
?>
<div class="container">
<?php
echo $_SESSION['SESS_TOTNUM']." images found ";
echo "for Shoot ID#: ".$_SESSION['sID']."<br>";
echo "*Note* - if input boxes come up GREEN, then images are already loaded into the database";
?>
<p>
<?php

$images1 = new Image($db);

$images1->ShootId = $colname_chk_Images;
$images1->directory = $colname1_chk_Images;
$images1->ClientID = $colname_get_Images;
$chk_Images = $images1->checkImages();
$get_Images = $images1->getImages();

$Images = array();

while ($row_get_Images = $get_Images->fetch(PDO::FETCH_ASSOC))
    {
        $Images[] = $row_get_Images['image_name'];
    }
?></p>
<form method="POST" name="form1" id="form1" action="input.php">
    <table id="clientshoots" class="table table-condensed table-bordered table-small">
  <tr>
    <th>image_id</th>
    <th>image_name</th>
    <th>image_path</th>
    <th>image_path_root</th>
    <th>image_size</th>
    <th>directory</th>
    <th width="auto">ShootId</th>
    <th width="auto">ClientID</th>
    <th>ClientName</th>
    <th>login</th>
  </tr>
  <?php $ic=0;
   for($i=0;$i<count($filesArray);$i++) { 
        $fileinfo = $filesArray[$i];
        $fname=$dir."/".$fileinfo;
        $fname2=$dir2."/".$fileinfo;
        $size = filesize($fname);
        $atime = date("F d, Y H:i:s", fileatime($fname));
        $mtime= date("F d, Y H:i:s", filemtime($fname));
        $perms=decoct(fileperms($fname) & 0777);
        $type=filetype($fname);
        $pth=realpath($fname);
        $name=basename($fname);
        $dn=dirname($fname2);
        if (in_array($fileinfo, $Images)) {
        $entered=1;    
        echo "<style type=\"text/css\">\n";
        echo "input {\n";
        echo "background-color:#00FF33;\n";
        echo "}\n";
        echo "</style>";
        } 
    ?>
    <tr>
    <td>&nbsp;</td>
      <td><input type="text" name="image_name[]" value="<?php echo $fileinfo; ?>" readonly/></td>
      <td><input type="text" name="image_path[]" value="<?php echo $dir; ?>" readonly/></td>
      <td><input type="text" name="image_path_root[]" value="<?php echo $dir2; ?>" readonly/></td>
      <td><input type="number" name="image_size[]" value="<?php echo $size; ?>" readonly/></td>
      <td><input type="text" name="directory[]" value="<?php echo $maindir; ?>" readonly/></td>
      <td><input type="number" name="ShootId[]" value="<?php echo $ShootId; ?>" readonly/></td>
      <td><input type="number" name="ClientID[]" value="<?php echo $ClientID; ?>" readonly/></td>
      <td><input type="text" name="ClientName[]" value="<?php echo $_SESSION['cName']; ?>" readonly/></td>
      <td><input type="text" name="login[]" value="<?php echo $Clogin; ?>" readonly/></td>
    </tr>
    <?php next($filesArray);
    $ic=$ic+1;
    } 

    $_SESSION['SESS_IC'] = $ic;?>
</table>
<?php if ($entered == 1){
  echo  "<a href=\"viewClient.php?ClientID=".$ClientID."\" class=\"btn btn-primary active\">Return</a>";
} else { 
  echo  "<input class=\"btn-primary\" style=\"background-color:\" id=\"Insert records\" type=\"submit\" value=\"Insert records\">";
}?>
<input type="hidden" name="MM_insert" value="form1">
<input type="hidden" name="sID" value="<?php echo $sID; ?>">
</form>
</div>
<br>
  <!-- /container -->
 <?php include 'footer_b.php'; ?>

然后是input.php页面......

<?php
// set page headers
$page_title = "Enter Images into Database";
include_once 'auth.php';

// get database connection
include_once 'config/fpaddb.php';
include_once 'objects/clients.php';
include_once 'objects/photoshoots.php';
include_once 'objects/images.php';
include_once 'objects/ratings.php';

$database = new Database();
$db = $database->getConnection();

$sID = $_SESSION['sID'];
$ic = $_SESSION['SESS_IC'];
$ma = $_SESSION['SESS_CLIENT_MULTI'];
$gn = $_SESSION['SESS_CLIENT_GRPNO'];
$cID = $_SESSION['cID'];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

    //Function to sanitize values received from the form. Prevents SQL injection

        function clean($str) {
        $str = filter_var(($str), FILTER_SANITIZE_STRING);
        return ($str);
    }

$image1 = new Image($db);   

$count = count($_POST['image_name']);
$fileinfo = clean($_POST['image_name']);

    //Check for duplicates
    if($fileinfo != '') {
            for($i=0;$i<$count;$i++) { 
            $fileinfo = clean($_POST['image_name'][$i]);
            //echo $fileinfo;
            $image1->image_name = $fileinfo;
            $result = $image1->check4Dup(); 
            if($result) {
                if(count($result) > 0) {
                    $errmsg_arr[] = 'Image already entered into Database';
                    $errflag = true;
                }
                $result = NULL;
            }
            else {
                die($e->getMessage());
            }
            next($count);   
            }
        }
$image1->ic = $ic;
$num = $image1->create();   

$colname_newImages = "-1";
if (isset($sID)) {
  $colname_newImages = $sID;
}
$image1->ShootId = $sID;
$newImages = $image1->countOneShoot();
$row_newImages = $newImages->fetch(PDO::FETCH_ASSOC);
$totalRows_newImages = $newImages->rowCount();
$ic2 = $totalRows_newImages;
$_SESSION['SESS_TOTNUM_ENT'] = $ic2;
header("Location: rs_images.php"); 


include_once 'header_a.php';
?>
<div class="container">
<?php 
echo "Success! Number of images entered is ".$ic2; ?>
<br><br>
<p><input name="Verify" type="button" value="Verify Inputs" onclick="MM_goToURL('parent','rs_images.php');return document.MM_returnValue"/></p>
</div>
 <?php include 'footer_b.php'; ?>

和班级档案......

<?php

class Image{

        // database connection and table name
    private $dbh;
    private $table_name = "images";

    // object properties
    public $image_id;
    public $image_name;
    public $image_path;
    public $image_path_root;
    public $image_size;
    public $directory;
    public $ShootId;
    public $ClientID;
    public $ClientName;
    public $login;
    public $ic;

    public function __construct($db){
        $this->dbh = $db;
    }

    // Clean Function
    function clean($str){
        $str = filter_var(($str), FILTER_SANITIZE_STRING);
        return ($str);
    }

    // test function
    function test(){

        $ic = $this->ic;

        $i=1;
        $j=1;
        foreach ($_POST['image_name'] as $row=>$iname)
        {
            $image_name = clean($iname);
            $image_path = clean($_POST['image_path'][$row]);
            $image_path_root = clean($_POST['image_path_root'][$row]);
            $image_size = clean($_POST['image_size'][$row]);
            $directory = clean($_POST['directory'][$row]);
            $ShootId = clean($_POST['ShootId'][$row]);
            $ClientID = clean($_POST['ClientID'][$row]);
            $ClientName = clean($_POST['ClientName'][$row]);
            $login = clean($_POST['login'][$row]);
            $Clogin = $login."');";

            $i=$i+1;
            $j=$j+1;

            $qry1st = "INSERT INTO `images` (image_name, image_path, image_path_root, image_size, directory, ShootId, ClientID, ClientName, login) VALUES ";
            $sql_array = "('".$image_name."', '".$image_path."', '".$image_path_root."', ".$image_size.", '".$directory."', ".$ShootId.", ".$ClientID.", '".$ClientName."', '".$Clogin;
            //$stmt = $this->dbh->prepare($qry1st.$sql_array);
            //$stmt->execute();
            echo $qry1st.$sql_array;
        }

    }

     // create function
    function create(){

        $ic = $this->ic;
        $qry1st = "INSERT INTO `images` (image_name, image_path, image_path_root, image_size, directory, ShootId, ClientID, ClientName, login) VALUES ";

        $sql_array = array(); // This is where we'll queue up the rows

        $queue_num = 50; // How many rows should be queued at once?
        $i=1;       
        foreach ($_POST['image_name'] as $row=>$iname)
        {
        $image_name = clean($iname);
        $image_path = clean($_POST['image_path'][$row]);
        $image_path_root = clean($_POST['image_path_root'][$row]);
        $image_size = clean($_POST['image_size'][$row]);
        $directory = clean($_POST['directory'][$row]);
        $ShootId = clean($_POST['ShootId'][$row]);
        $ClientID = clean($_POST['ClientID'][$row]);
        $ClientName = clean($_POST['ClientName'][$row]);
        $login = clean($_POST['login'][$row]);
        if ($i==($_SESSION['SESS_TOTNUM'])) {
        $login_term = $login."');";
        }
        else
        {
        $login_term = $login."')";
        $i=$i+1;
        }

        $sql_array[] = "('".$image_name."', '".$image_path."', '".$image_path_root."', ".$image_size.", '".$directory."', ".$ShootId.", ".$ClientID.", '".$ClientName."', '".$login_term;

        // Add a new entry to the queue
        $c=0;
        if (count($sql_array) >= $queue_num)
        { // Reached the queue limit

        $addImages = $this->dbh->query($qry1st . implode(', ', $sql_array)); // Insert those that are queued up
        $addImages->execute();

        $sql_array = array(); // Erase the queue
        }//End if 

        }//end foreach

        if (count($sql_array) > 0) // There are rows left over
        {
        $addImages = $this->dbh->query($qry1st . implode(', ', $sql_array));
        $addImages->execute();
        }
    }

    function checkImages(){

    $query_chk_Images = "SELECT images.image_name FROM images WHERE ShootId = ? AND directory = ?";

    $chk_Images = $this->dbh->prepare ($query_chk_Images);
    $chk_Images->bindValue(1, $this->ShootId);
    $chk_Images->bindValue(2, $this->directory);
    $chk_Images->execute();
    return $chk_Images;
    }

    // create function
    function getImages(){

    $query_get_Images = "SELECT * FROM images WHERE ClientID = ? ORDER BY image_name ASC";

    $get_Images = $this->dbh->prepare ($query_get_Images);
    $get_Images->bindValue(1, $this->ClientID);
    $get_Images->execute();
    return $get_Images;

    }

     // create function
    function getImageID(){

        $query_rsImageID = "SELECT * FROM images WHERE ShootId = ? ORDER BY image_id ASC";

        $rsImageID = $this->dbh->prepare($query_rsImageID);
        $rsImageID->bindValue(1, $this->ShootId);
        $rsImageID->execute();

        return $rsImageID;
    }

     // create function
    function get_image_id(){

        $q = "SELECT image_id FROM images WHERE ShootId = ? ORDER BY image_id ASC";

        $stmt = $this->dbh->prepare($q);
        $stmt->bindValue(1, $this->ShootId);
        $stmt->execute();

        return $stmt;
    }

    // create function
    function countOneShoot(){

        $query_newImages = "SELECT * FROM images WHERE ShootId = ?";

        $newImages = $this->dbh->prepare($query_newImages);
        $newImages->bindValue(1, $this->ShootId);
        $newImages->execute();

        return $newImages;
    }

    // create function
    function check4Dup(){

        $qry = "SELECT * FROM `images` WHERE image_name = ?";

        $result = $this->dbh->prepare($qry);
        $result->bindValue(1, $this->image_name);
        $result->execute();

        return $result;
    }
}

我已经尝试了所有我尝试过的额外内容,比如一次输入一条记录信息,将值与冒号前缀字段名称绑定,而不是绑定?&#39; s。我试过不同的循环。我认为它归结为试图通过一个查询推得太多......但是为什么它可以在XAMPP上工作?为什么它在PHP 5.2下运行良好?

我很欣赏任何可以解决的问题。这是我关于PHP,MySQL或任何网站相关的第一篇文章,我一直在学习这些东西,并且已经完成并调试了90%,当我把它放到网上做一些真正的测试时实际的目录和客户端文件夹,当我发现在PHP 5.4和5.2之间时,已经发生了一些变化,我发现自己几乎每行重写一次,以升级到MySQLi或PDO / OOP。在互联网上进行了大量搜索后,我选择了OOP方法,仍然需要重写上面的更多代码来清理大量的东西,但是现在我正在排除INSERT故障,我在我无法自己解决或在我迄今为止所阅读的所有论坛,帖子和博客的帮助下解决。

0 个答案:

没有答案