在mysql查询中奇怪的额外等号。我该如何删除它?

时间:2012-03-17 20:15:44

标签: php mysql

我试图找出如何分配$updateNames变量以接受4个不同的值,因此允许我一次重新上传一个文件。现在我上传的任何文件都会被放入name1

这是php:

    <?php

require_once('storescripts/connect.php');
mysql_select_db($database_phpimage,$phpimage);
error_reporting(E_ALL);
$uploadDir = 'upload/';
if(isset($_POST['upload']))
{


foreach ($_FILES as $file)
{

    $fileName = $file['name'];
    $tmpName = $file['tmp_name'];
    $fileSize = $file['size'];
    $fileType = $file['type'];

    if ($fileName != ""){
        $filePath = $uploadDir;
        $fileName = str_replace(" ", "_", $fileName);

        //Split the name into the base name and extension
        $pathInfo = pathinfo($fileName);
        $fileName_base = $pathInfo['fileName'];
        $fileName_ext = $pathInfo['extension'];

        //now we re-assemble the file name, sticking the output of uniqid into it
        //and keep doing this in a loop until we generate a name that 
        //does not already exist (most likely we will get that first try)
        do {
           $fileName = $fileName_base . uniqid() . '.' . $fileName_ext;
        } while (file_exists($filePath.$fileName));

        $file_names [] = $fileName;

        $result = move_uploaded_file($tmpName, $filePath.$fileName);
    }


if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$fileinsert[] = $filePath;
}
}

$mid   = mysql_real_escape_string(trim($_POST['mid']));
$cat   = mysql_real_escape_string(trim($_POST['cat']));
$item  = mysql_real_escape_string(trim($_POST['item']));
$price = mysql_real_escape_string(trim($_POST['price']));
$about = mysql_real_escape_string(trim($_POST['about']));

for($i = 0; $i < 4; $i++)
{
    $values[$i] = isset($file_names[$i]) ? mysql_real_escape_string($file_names[$i]) : '';

    if($values[$i] != '')
    {
        $updateVals[] = "name".($i+1)." = '{$values[$i]}'";
    }

}
$updateNames = '';
if(count($updateVals))
{
    $updateNames = ", " . implode(', ', $updateVals);
}

$update = "INSERT INTO image
               (mid, cid, item, price, about, name1, name2, name3, name4)
           VALUES
               ('$mid', '$cat', '$item', '$price', '$about', '$values[0]', '$values[1]', '$values[2]', '$values[3]')
           ON DUPLICATE KEY UPDATE
                cid = '$cat', item = '$item', price = '$price', about = '$about' $updateNames";
$result = mysql_query($update) or die (mysql_error());

3 个答案:

答案 0 :(得分:1)

当您填充$ values数组时,$ fields数组仍为空。因此引用返回一个空字符串,导致赋值表达式不完整。

答案 1 :(得分:1)

非常基本的错误。

INSERT INTO image (mid, cid, item, price, about, name1, name2, name3, name4) VALUES ('167', 'hats', 'zzz', 'zz', 'zz', '4f64105aad275.jpg', '', '', '') ON DUPLICATE KEY UPDATE cid = 'hats', item = 'zzz', price = 'zz', about = 'zz' , = '4f64105aad275.jpg';

在上述声明的最后,您已, = '4f64105aad275.jpg'。请将其设置为:, name1 = '4f64105aad275.jpg';

答案 2 :(得分:0)

在您的代码中,您不会在for循环中为$fields[]数组分配任何值。

您不需要数组来执行您想要的操作。

这应该有效:

$updateVals[$i] = "name".($i+1)." = '{$values[$i]}'";

您需要使用i + 1作为名称变量以1开头。