PHP查询在表中插入2行

时间:2010-12-30 16:10:20

标签: php mysql

好的,这是交易。我有3个查询将数据放在不同的表中。其中2个是包含文件。我试着将它们放入主代码中,结果是一样的。前两个查询插入一行包含数据和一个空行。第三个工作正常。这是代码。

这在主页面中:

<? $p=0;
    if ($select2)
    {
        $event=$select2;
    }
    else
    {
        require_once("insert_gal_name.php5");
    }

    if ($select)
    {
        $folder=$select;
    }
    else
    {
        require_once("insert_folder.php5");
        if (file_exists("Connections/".$folder))
        {
        }
        else
        {
             mkdir("Connections/$folder",0777);
        }
    }

    while($p<$number)
    {
        $p++;

        $dir = 'Connections/'.$folder.'/'; // Директорията в която ще се записват файловете
        copy($_FILES['file']['tmp_name'][$p],"$dir".$_FILES['file']['name'][$p]); //Копиране на файла
        echo "Файлът бе качен успешно!<br>"; // Извеждане на съобщение показващо, че файла е качен
        if($_FILES['file']['name'][$p])
        {
            $real_file_name = $dif_of_files.$_FILES['file']['name'][$p];
            $nom_file = str_replace(" ", "", $real_file_name); }

            $query = "INSERT INTO gallery (name, title, day, month, year) VALUES ('$nom_file', '$event', '$day', '$month', '$year')";
            $result = mysql_query($query) or die(mysql_error());
        }
        if (!$result)
        {
            echo "Error";
        }
        else
        {
            echo "All data was uploaded successfuly.";
        }
?>

以下是按来源顺序包含的内容

<?  $query = "INSERT INTO gallery_names (name) VALUES ('$event')";
    $result = mysql_query($query) or die(mysql_error());
    if (!$result)
    {
        echo "Error";
    }
    else
    {
        echo "The gallery was created successfuly.";
    }
?>

<? $query = "INSERT INTO folders (name) VALUES ('$folder')";
    $result = mysql_query($query) or die(mysql_error());
    if (!$result)
    {
        echo "Error";
    }
    else
    {
        echo "The folder was created successfuly.";
    }
?>

2 个答案:

答案 0 :(得分:2)

<?
    if($event!='')
    {
        $query1 = "INSERT INTO gallery_names (name) VALUES ('$event')";
        $result1 = mysql_query($query1) or die(mysql_error());
        if (!$result1)
        {
            echo "Error";
        }
        else
        {
            echo "The gallery was created successfuly.";
        }
    }
?>

答案 1 :(得分:1)

如果$ select2为false,则此代码将执行两次插入。如果$ event为空,其中一个将是一个空行,这听起来像你遇到的问题 $ select2和$ event的值是多少?你试过调试它们吗?