使用php代码将文件上传到服务器

时间:2014-04-14 13:03:07

标签: php mysql

我想写一个PHP代码,它更新我的mysql表上的现有数据,并将一个文件上传到服务器......正常工作,只是文件上传没有。 如果有人可以帮我解决这个问题,我会非常感激! 我的代码:

<?php
// personal data assignment
if ($_SESSION['username'] == "zdraci"){
    require('config.inc');
    $target = 'galab-kepek/';
    $tablename = "data_laci";
}

if ($_SESSION['username'] == "styven"){
    require('config2.inc');
    $target = 'galab-kepek2/';
    $tablename = "data_timi";
}

//get the form data
$gyuruszam = $_REQUEST["gyuruszam"];
$new_gyuruszam = $_POST["gyuruszam-new"];
$apja = $_POST["apja"];
$anya = $_POST["anya"];
$baknyosteny = $_POST["baknyosteny"];
$megjegyzes = $_POST["megjegyzes"];
$pic=($_FILES['photo1']['name']);

if($pic == NULL || $pic == ""){ $pic = "Nincs k&#233p!"; }

//This is the directory where images will be saved
$targetfile = $target . basename( $_FILES['photo1']['name']);

if ($new_gyuruszam != NULL){
    $sql0 = mysql_query("UPDATE $tablename SET gyuruszam = '$new_gyuruszam' WHERE gyuruszam = '$gyuruszam'");
    $sql0_r = mysql_affected_rows();
    $sql1 = mysql_query("UPDATE $tablename SET apja = '$apja' WHERE gyuruszam = '$new_gyuruszam'");
    $sql1_r = mysql_affected_rows();
    $sql2 = mysql_query("UPDATE $tablename SET anya = '$anya' WHERE gyuruszam = '$new_gyuruszam'");
    $sql2_r = mysql_affected_rows();
    $sql3 = mysql_query("UPDATE $tablename SET baknyosteny = '$baknyosteny' WHERE gyuruszam = '$new_gyuruszam'");
    $sql3_r = mysql_affected_rows();
    if($pic == NULL || $pic == ""){
        $sql4 = mysql_query("UPDATE $tablename SET kepneve = '$pic' WHERE gyuruszam = '$new_gyuruszam'");
        $sql4_r = mysql_affected_rows();
    }
    $sql5 = mysql_query("UPDATE $tablename SET megjegyzes = '$megjegyzes' WHERE gyuruszam = '$new_gyuruszam'");
    $sql5_r = mysql_affected_rows();
}
else{
    $sql1 = mysql_query("UPDATE $tablename SET apja = '$apja' WHERE gyuruszam = '$gyuruszam'");
    $sql1_r = mysql_affected_rows();
    $sql2 = mysql_query("UPDATE $tablename SET anya = '$anya' WHERE gyuruszam = '$gyuruszam'");
    $sql2_r = mysql_affected_rows();
    $sql3 = mysql_query("UPDATE $tablename SET baknyosteny = '$baknyosteny' WHERE gyuruszam = '$gyuruszam'");
    $sql3_r = mysql_affected_rows();
    if($pic == NULL || $pic == ""){
        $sql4 = mysql_query("UPDATE $tablename SET kepneve = '$pic' WHERE gyuruszam = '$gyuruszam'");
        $sql4_r = mysql_affected_rows();
    }
    $sql5 = mysql_query("UPDATE $tablename SET megjegyzes = '$megjegyzes' WHERE gyuruszam = '$gyuruszam'");
    $sql5_r = mysql_affected_rows();
}
?>

和代码的第二部分:

<?php

            if ($sql0_r > 0 || $sql1_r > 0 || $sql2_r > 0 || $sql3_r > 0 || $sql4_r > 0 || $sql5_r > 0 ) {
                if ($new_gyuruszam == NULL)
                    {
                        echo '<center><h2><br/>Sikeres friss&#237;t&#233;s!</h2><br/><h3>A k&#246;vetkez&#337 gy&#369;r&#369;sz&#225;m "<b style="color:#C60">'.$gyuruszam.'</b>" fel&#252;l&#237;r&#243;dott az &#250;j adatokkal!</h3></center>';

                    }
                    else {
                        echo '<center><h2><br/>Sikeres friss&#237;t&#233;s!</h2><br/><h3>A k&#246;vetkez&#337 gy&#369;r&#369;sz&#225;m "<b style="color:#C60">'.$gyuruszam.'</b>" fel&#252;l&#237;r&#243;dott az &#250;j "<b style="color:#C60">'.$new_gyuruszam.'</b>" gy&#369;r&#369;sz&#225;mal!</h3></center>';

                    }
                }
            else {
                echo '<center><h2><br/>A friss&#237;t&#233;s nem lehets&#233;ges!</h2><br/><h3>Lehet, hogy nem l&#233;tezik ez a gy&#369;r&#369;sz&#225;m lementve,<p>vagy nem v&#225;ltoztatt&#225;l semmit az adatokon...</p></h3></center>';
            }
            //Writes the photo to the server 
            if(move_uploaded_file($_FILES['photo1']['tmp_name'], $targetfile)) 
            {
                //Tells you if its all ok
                echo "<center><br/><h3>A k&#233;p sikeresen lement&#246;d&#246;tt!</h3></center>";
            } 
            else {
                //Gives and error if its not 
                echo "<center><br/><h3>Sikertelen k&#233;p lement&#233;s, lehets&#233;ges ok:<br/>-nem v&#225;lasztott&#225;l ki k&#233;pet;</h3></center>";
            }
        ?>

如果我选择或不选择文件,它会从if(move_uploaded_file($_FILES['photo1']['tmp_name'], $targetfile))的第二个循环中给出错误 它告诉我没有选择文件(因为这是来自该循环的错误消息... 提前谢谢你!

0 个答案:

没有答案