2012-01-04 4 views
0

ファイルをデータベースに簡単にアップロードできますが、ファイルを編集したいときは未定義エラーが発生しています。コードは次のとおりです。

product_update.phpこれは、データベース内の特定のレコードのすべての既存情報を変更できる場所です。

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="purchase_try"; // Database name 
$tbl_name="product_list"; // Table name 

// Connect to server and select database. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 


// get value of id that sent from address bar 
//$id = $_GET['AutoID']; 

if(isset($_GET['AutoID'])){ 
$id= $_GET['AutoID']; 
} 
// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE AutoID = ".$id; 

$result=mysql_query($sql); 

$rows=mysql_fetch_array($result, 1); 

?> 


      <form name="form1" method="post" action="update_post.php"/> 
      <table width="600" border="0" cellspacing="1" cellpadding="0"> 

      <tr> 
      <td> 
      <table width="100%" height="87" border="1" cellpadding="3" cellspacing="0"> 
      <tr> 
      <td colspan="3"><table width="100%" height="87" border="1" cellpadding="3" cellspacing="0"> 

      <tr> 
      <td align="center"><strong>Title</strong></td> 
      <td width="144" align="center"> : </td> 
      <td > <input name="title_product" type="text" id="title_product" value="<?php echo $rows['title_product']; ?>" size="90" /></td> 
      </tr> 

      <tr> 
      <td align="center"><p><strong>Description</strong></p></td> 
      <td align="center"> : </td> 
      <td><input name="description_product" type="text" id="description_product" value="<?php echo $rows['description_product']; ?>" /> 
      </td> 
      </tr> 

      <tr> 
      <td width="169" align="center"><p><strong>Start Date</strong></p></td> 
      <td align="center"> : </td> 
      <td><input name="start_date" type="text" id="start_date" value="<?php echo $rows['start_date']; ?>" size="40" /> </td> 
      </tr> 

      <tr> 
      <td width="169" align="center"><p><strong>End Date</strong></p></td> 
      <td align="center"> : </td> 
      <td><input name="end_date" type="text" id="end_date" value="<?php echo $rows['end_date']; ?>" size="40" /></td> 
      </tr> 

      <tr> 
      <td width="169" align="center"><strong>Price Before</strong></td> 
      <td align="center"> : </td> 
      <td> <input name="price_before" type="text" id="price_before" value="<?php echo $rows['price_before']; ?>" size="40" /> </td> 
      </tr> 

      <tr> 
      <td width="169" align="center"><strong>Price After </strong></td> 
      <td align="center"> : </td> 
      <td> <input name="price_after" type="text" id="price_after" value="<?php echo $rows['price_after']; ?>" size="40" /> </td> 
      </tr> 

      <tr> 
      <td width="169" align="center"><strong>Percentage </strong></td> 
      <td align="center"> : </td> 
      <td><input name="percentage" type="text" id="percentage" value="<?php echo $rows['percentage']; ?>" size="40" /></td> 
      </tr> 

      <tr> 
      <td width="169" align="center"><strong>Edit Uploaded Images?<!-- <a href="image_edit.php?AutoID=<?PHP //echo $rows['AutoID'];?>">Click here.</a> --></strong></td> 
      < <td align="center"> : </td> 
      <td><input name="ufile1" type="file" id="ufile1" size="50" /></td> 
      </tr> 

      <tr> 
      <td width="169" align="center"><strong>Image 2 </strong></td> 
      <td align="center"> : </td> 
      <td><input name="ufile2" type="file" id="ufile2" size="50" /></td> 
      </tr> 

      <tr> 
      <td width="169" align="center"><strong>Image 3 </strong></td> 
      <td align="center"> : </td> 
      <td><input name="ufile3" type="file" id="ufile3" size="50" /></td> 
      </tr> 

      <tr> 
      <td width="169" align="center"><strong>Date Created</strong></td> 
      <td align="center"> : </td> 
      <td><input name="date_created" type="text" id="date_created" value="<?php echo $rows['date_created']; ?>" /> </td> 
      </tr> 

      <tr> 
      <td width="169" align="center"><strong>Time Created</strong></td> 
      <td align="center"> : </td> 
      <td><input name="time_created" type="text" id="time_created" value="<?php echo $rows['time_created']; ?>" /></td> 
      </tr> 

     <td colspan="3"><p align="right"> 
     <input name="AutoID" type="hidden" id="AutoID" value="<?php echo $rows['AutoID']; ?>" /> 
     <input type="submit" name="Submit" value="Submit" /> 
     </p></td> 
     </tr> 
     </table></td> 
</tr> 
</table> 
</td> 

</tr> 
</td> 
</form> 

<?php 

// close connection 
mysql_close(); 

?> 

、その後、Update_Post.phpこのファイルは、FORMのすべての値をとり、データベースを更新します。他のフィールドは更新されますが、FILE UPLOADは機能しません。それはエラーを与える。

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="purchase_try"; // Database name 
$tbl_name="product_list"; // Table name 

// Connect to server and select database. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 
// update data in mysql database 

?> 
<?PHP 
if(isset($_POST['Submit'])) 
    { 



    $title   = $_POST['title_product']; 
    $description = $_POST['description_product']; 

    $start_date  = $_POST['start_date']; 
    $end_date  = $_POST['end_date']; 
    $price_before = $_POST['price_before']; 
    $price_after = $_POST['price_after']; 
    $percentage  = $_POST['percentage']; 

    $image_1  = $_FILES['ufile1']['name']; 
    $image_2  = $_FILES['ufile2']['name']; 
    $image_3  = $_FILES['ufile3']['name']; 

    $date_created = $_POST['date_created']; 
    $time_created = $_POST['time_created']; 

    $id     = $_POST['AutoID']; 

    $sql="UPDATE $tbl_name SET title_product='".$title."', 
            description_product='".$description."', 
            start_date='".$start_date."', 
            end_date ='".$end_date."', 
            price_before='".$price_before."', 
            price_after='".$price_after."', 
            percentage='".$percentage."', 

            date_created='".$date_created."', 
            time_created='".$time_created."' WHERE AutoID=".$id; 

    $result=mysql_query($sql); 
// image_1 = '$image_1', 
    //        image_2 = '$image_2', 
    //        image_3 = '$image_3', 

    //$sql_insert = "INSERT INTO $tbl_name(image_1, image_2, image_3)VALUES ('$image_1', 'image_2', 'image_3') WHERE AutoID= ".$id; 

    //$sql_result = mysql_query($sql_insert); 
    // if successfully updated. 
    if($result){ 

        $path1= "upload/".$_FILES['ufile1']['name']; 
        $path2= "upload/".$_FILES['ufile2']['name']; 
        $path3= "upload/".$_FILES['ufile3']['name']; 

        //copy file to where you want to store file 
        copy($_FILES['ufile1']['tmp_name'], $path1); 
        copy($_FILES['ufile2']['tmp_name'], $path2); 
        copy($_FILES['ufile3']['tmp_name'], $path3); 
        //$_FILES['ufile']['name'] = file name 
        //$_FILES['ufile']['size'] = file size 
        //$_FILES['ufile']['type'] = type of file 

        // Use this code to display the error or success. 

        $filesize1=$_FILES['ufile1']['size']; 
        $filesize2=$_FILES['ufile2']['size']; 
        $filesize3=$_FILES['ufile3']['size']; 

        //all 3 must be selected to upload. 
        if($filesize1 || $filesize2 || $filesize3 != 0) 
        { 
        $sql_insert = "INSERT INTO $tbl_name(image_1, image_2, image_3)VALUES ('$image_1', 'image_2', 'image_3') WHERE AutoID= ".$id; 

        $sql_result = mysql_query($sql_insert); 
        echo "We have recieved your files"; 
        } 

        else { 
        echo "ERROR....."; 
        } 
        echo "Successful"; 

        //header("location:user_history2.php"); 
        } 
    } 

    else 
    { 
    echo "Update Unsuccessful."; 
    } 

?> 

The is Undefined index : ufile 
+0

あなたはmove_uploaded_file()の代わりに、コピーを()したことがありますか? www.php.net/move_uploaded_file。 – stefandoorn

+0

いいえ、まだ試していませんでした。今すぐ試してみてください –

+0

var_dump($ FILES)とvar_dump($ _ POST)の出力を確認する必要がありますか? – rkosegi

答えて

2

あなたのフォームには、ファイルをアップロードできるようにenctype="multipart/form-data"属性が必要です。

詳しい情報はhere

+0

私はこれを行っています。しかし、別の問題は、私はそれを変更しなかった場合、私はレコードにアップロードされたファイルを保持する方法です? 現在、3つのファイルをアップロードするオプションがあるためです。 MOdifyingフォームの場合、私はimage_1のみをアップロードし、残りの部分は手を触れずに送信します。アップロードされたファイルの名前が表示され、編集しなかった前のレコードが消えます。 –

2

ファイルをアップロードするときは、フォームにenctype="multipart/form-data"を指定する必要があります。

<form name="form1" method="post" action="update_post.php" enctype="multipart/form-data"/> 
... 
</form> 
+0

ああ、私はそれを挿入し、ファイルを実行させてください。ありがとうございます –

+0

あなたのトリックは機能しました!それは実行されています!しかし、今、別の問題があります。 1つのファイルだけをアップロードすると、1つのファイルの名前だけが表示されます。どちらが正しい。その後、別の2つのファイルをアップロードすると、私がタッチしなかった以前のレコードが消えます。なぜですか? - –

+0

@MiffRoseおそらくあなたの奇妙な 'INSERT ... WHERE' SQL構文のためです。しかしそれは別の質問です。一度に1つの質問だけを聞くので、SOはフォーラムではありません。 –