使用php将图像上传到数据库

时间:2010-10-06 06:02:50

标签: php database image file upload

我正在尝试使用php将图像上传到数据库,我收到一条错误消息,提示未定义索引:horse_image

这是表单代码:

<form method="post" enctype="multipart/form-data"action="newhorse.php"
OnSubmit="return VerifyDataEntry(this)">  
<p>Please type the horse details below 
<p>Horse Id: <input type="text" name="horse_id">  
<p>Horse name: <input type="text" name="horse_name">
<p>Horse Gender: <span id="spryradio1">
<label>
  <input type="radio" name="horse_gender" value="m" id="horse_gender_0" />
  Male</label>

<label>
  <input type="radio" name="horse_gender" value="f" id="horse_gender_1" />
  Female</label>
<br />
<span class="radioRequiredMsg">Please make a selection.</span></span>

<p>Horse Height: <input type="text" name="horse_height">
<?php
  if (!isset($_FILES["horse_image"]["tmp_name"]))
  {
 ?>

        <p>Select an image to upload:
          <input type="file" size="50" name="horse_image">


<?php
  }
  else
  {
    $upfile = "horse_images/".$_FILES["horse_image"]["name"];

    $imgsize=getimagesize($_FILES["horse_image"]["tmp_name"]);

    if(!move_uploaded_file($_FILES["horse_image"]
      ["tmp_name"],$upfile))
    {
      echo "ERROR: Could Not Move File into Directory";
    }

  }
 ?>
<p><span id="spryselect1">
<label for="horse_breed">Horse Breed: </label>
<select name="horse_breed" id="horse_breed">
  <option value="0" selected="selected">please select</option>
  <option value="13">american</option>
  <option value="14">asian</option>
  <option value="11">australian</option>
  <option value="12">brazilian</option>
  <option value="15">europian</option>
</select>
<?php
  include("connection.php"); 
     $conn = oci_connect($UName,$PWord,$DB);
  $query = "Select * FROM skill ORDER BY SKILL_DESC";
    $stmt = oci_parse($conn,$query);
  oci_execute($stmt);
?>
<p>Select horse skill(s)
<p>
<table border="2" cellpadding="4">
  <tr>
    <th>Skill Id</th>
    <th>Skill Name</th>
    <th>Add?</th>
  </tr>
  <?php 
  while ($skills = oci_fetch_array ($stmt))
  {
?>
    <tr>
      <td><?php echo $skills["SKILL_ID"]; ?></td>
      <td><?php echo $skills["SKILL_DESC"]; ?></td>
      <td align="center">

<input type="checkbox" name="check[]" value="<?php echo $skills["SKILL_ID"]; ?>">

      </td>
    </tr>
<?php
  }
?>

</table>
<p><input type="Submit" Value="Submit">  <input type="Reset" Value="Clear Form Fields">  
</form>

这是newhorse.php中的代码

<?php 
$horse_id = $_POST["horse_id"]; 
$horse_name = $_POST["horse_name"]; 
$horse_gender = $_POST["horse_gender"];
$horse_height = $_POST["horse_height"];
$horse_image = $_POST["horse_image"];
$horse_breed = $_POST["horse_breed"];
?> 
    <table border="0">  
    <tr>  
    <td>Horse Id: <?php echo $_POST["horse_id"]; ?></td>  
    </tr>  
    <tr>  
    <td>Horse Name: <?php echo $_POST["horse_name"]?> </td> 
    </tr>  
    <tr>  
    <td>Horse Gender: <?php echo $_POST["horse_gender"] ?></td>  
    </tr>  
    <tr>  
    <td>Horse Height: <?php echo $_POST["horse_height"] ?></td>  
    </tr>
    <tr>  
    <td>Horse Image: <?php echo $_POST["horse_image"]
     ?></td>  
    </tr>
    <tr>  
    <td>Horse Breed: <?php echo $_POST["horse_breed"] ?></td>  
    </tr>
    </table> 




    <?php

          include("connection.php");
          $conn = oci_connect($UName,$PWord,$DB);

          $query="INSERT INTO horse (horse_id, horse_name,horse_gender,horse_height,horse_image,horse_breed) VALUES('$_POST[horse_id]','$_POST[horse_name]','$_POST[horse_gender]','$_POST[horse_height]','$_POST[horse_image]','$_POST[horse_breed]')";

          $stmt = oci_parse($conn,$query);
          oci_execute($stmt); 


    ?>

3 个答案:

答案 0 :(得分:1)

你没有$ _POST ['horse_image']

使用$ _FILE ['horse_image']

http://www.w3schools.com/PHP/php_file_upload.asp

答案 1 :(得分:0)

上传的图片未放在$ _POST数组中。阅读file uploads

上的文档

答案 2 :(得分:0)

你去了

表单页面:

<form enctype="multipart/form-data" action="insert_image.php" method="post" name="changer">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>

插入数据库页面:

<?php

  include 'conf.php';

  if ($_FILES["image"]["error"] > 0)
  {
     echo "<font size = '5'><font color=\"#e31919\">Error: NO CHOSEN FILE <br />";
     echo"<p><font size = '5'><font color=\"#e31919\">INSERT TO DATABASE FAILED";
   }
   else
   {
     move_uploaded_file($_FILES["image"]["tmp_name"],"images/" . $_FILES["image"]["name"]);
     echo"<font size = '5'><font color=\"#0CF44A\">SAVED<br>";

     $file="images/".$_FILES["image"]["name"];
     $sql="INSERT INTO eikones (auxon, path) VALUES ('','$file')";

     if (!mysql_query($sql))
     {
        die('Error: ' . mysql_error());
     }
     echo "<font size = '5'><font color=\"#0CF44A\">SAVED TO DATABASE";

   }

   mysql_close();

?>
相关问题