试图上传图像并让它更新mysql

时间:2013-06-04 03:47:11

标签: php mysql image insert

每当我尝试使用表单上传文件并将文件插入mysql时,我都会收到这些错误 -

  

注意:未定义索引:文件输入   第300行/home/sgas/public_html/x/agentpage.php

     

注意:未定义索引:文件输入   第305行/home/sgas/public_html/x/agentpage.php

     

注意:未定义索引:文件输入   第310行/home/sgas/public_html/x/agentpage.php

我查看了这些名字,他们似乎都是对的所以我现在不知道什么是错的:/。

以下是从您上传图片的表单开始的代码。

<td align="right" valign="top" class="bodymaintextalt">applicable document</td>
<td>
    <input name="file" type="file" id="file" size="35">
    </tr>
    <tr>
        <td align="right" valign="top" class="bodymaintextalt">received</td>
        <td>
            <input name="received" type="text" id="received" value="$">
        </td>
    </tr>
    <tr>
        <td align="right" valign="top" class="bodymaintextalt">paid</td>
        <td>
            <input name="paid" type="text" id="paid" value="$">
        </td>
    </tr>
    <tr>
        <td align="right" valign="top">&nbsp;</td>
        <td>
            <div align="right">
                <input type="submit" name="Submit32" value="Submit">


<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
if ('POST' === $_SERVER['REQUEST_METHOD']){
    $con = mysql_connect("localhost","xxx","xxxxx");

    if (!$con){
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("xxxxx", $con);

    $target = "clientdoc/";
    $target = $target . basename( $_FILES['file']['agentclient']);

    $date = $_POST['date'];
    $propertydescription = $_POST['propertydescription'];
    $transactiontype = $_POST['transactiontype'];
    $applicabledocument = ($_FILES['file']['agentclient']); 
    $received = $_POST['received'];
    $paid = $_POST['paid'];

    //Writes the to the server 
    if(move_uploaded_file($_FILES['file']['tmp_name'], $target)){ 

        //Tells you if its all ok 
        echo "The file ". basename( $_FILES['uploadedfile']['agentclient']). " has been uploaded, and your information has been added to the directory"; 
    } 
    else { 
        //Gives and error if its not 
        echo "Sorry, there was a problem uploading your file."; 
    } 

    $sql = mysql_query("INSERT INTO `transactions` (`date`, `agentclient`, `propertydescription`, `transactiontype`, `applicabledocument`, `received`, `paid`) 
    VALUES ('$date', '$agentclient', '$propertydescription', '$transactiontype', '$applicabledocument', '$received', '$paid')") or die(mysql_error()); 

    $query = mysql_query($sql);
    if ($_SERVER['REQUEST_METHOD'] === 'POST'){
        echo "Succesfully added transaction. Updating table...";
        echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"48\">";
        mysql_close($con);
    }
}

1 个答案:

答案 0 :(得分:0)

您的文件名是文件,因此将其更改为agentclient

<input type="file" name="agentclient" id="file"/>

$target = $target . basename( $_FILES['agentclient']['name']);