如何在用户提交图像时将图像添加到mysql数据库

时间:2013-06-15 10:34:15

标签: php javascript mysql

我想使用表单将多个图像添加到mysql数据库中。每当用户提交该表单时,它就会捕获所有数据并将其存储到mysql数据库中。 (这是正确的方式,但我需要)

我的数据库表看起来像这样。

CREATE TABLE `Owner_detail` (
  `id` int(10) NOT NULL auto_increment,
  `fullname` varchar(30) NOT NULL,
  `List1` varchar(20) NOT NULL,
  `List2` varchar(20) NOT NULL,
  `List3` varchar(20) NOT NULL,
  `area` varchar(30) NOT NULL,
  `ownermobile` varchar(20) NOT NULL,
  `email` varchar(20) NOT NULL,
  `image_one` blob NOT NULL,
  `image_two` blob NOT NULL,
  `image_three` blob NOT NULL,
  `otherdetail` varchar(300) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=49 ;

我的html表单看起来像这样。

<table width="60%"><form action="process.php" method="post" enctype="multipart/form-data" name="tripleplay" onsubmit="MM_validateForm('fullname','','R','area','','R','ownermobile','','RisNum','email','','RisEmail','otherdetail','','R');return document.MM_returnValue">
  <tr>
    <td width="44%">Full Name : </td>
    <td width="56%"><input size="25" type="text" name="fullname" id="fullname" /></td>
  </tr>
  <tr>
    <td valign="top">Rental Type:</td>


    <td>

    <select name='List1' id="List1"     onchange="fillSelect(this.value,this.form['List2'])">
<option selected>Make a Selection</option>
</select>
<br /><br />
<select name='List2' id="List2" onchange="fillSelect(this.value,this.form['List3'])">
  <option selected>Make a Selection</option>
</select><br /><br />
<select name='List3' id="List3" onchange="getValue(this.value,     this.form['List2'].value, 
this.form['List1'].value)">
      <option selected>Make a Selection</option>
    </select>
</td>
  </tr>
  <tr>
    <td>Area : </td>
    <td><input size="25" type="text" name="area" id="area" />
  sq.ft.</td>
    </tr>
  <tr>
    <td>Owner Mobile / Landline</td>
    <td><input size="25" type="text" name="ownermobile" id="ownermobile" /></td>
  </tr>
  <tr>
    <td><p>E-mail</p></td>
    <td><input size="25" type="text" name="email" id="email" /></td>
  </tr>
  <tr>
    <td valign="top">Pictures of Property</td>
     <td>

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


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


        <input type="file" name="image_three" id="image_three" />
    </td>
  </tr>


  <tr>
    <td valign="top">Other details you would like to share about your property:</td>
    <td><textarea size="25" name="otherdetail" id="otherdetail" cols="45" rows="5">    </textarea></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" id="submit" value="Submit" /></td>
  </tr>
</form>
</table>

我在PHP中的处理表格看起来像这样

<?
if( $_POST )
{
  $con =     mysql_connect("","","");

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

  mysql_select_db("vkrental", $con);

  $users_fullname = $_POST['fullname'];
  $users_area = $_POST['area'];
  $users_List1 = $_POST['List1'];
  $users_List2 = $_POST['List2'];
  $users_List3 = $_POST['List3'];
  $users_ownermobile = $_POST['ownermobile'];
  $users_email = $_POST['email'];
  $users_image_one = $_POST['image_one'];
  $users_image_two = $_POST['image_two'];
  $users_image_three = $_POST['image_three'];
  $users_otherdetail = $_POST['otherdetail'];


  $users_fullname = htmlspecialchars($users_fullname);
  $users_area = htmlspecialchars($users_area);
  $users_List1 = htmlspecialchars($users_List1);
  $users_List2 = htmlspecialchars($users_List2);
  $users_List3 = htmlspecialchars($users_List3);
  $users_ownermobile = htmlspecialchars($users_ownermobile);
  $users_email = htmlspecialchars($users_email);
  $users_image_one = htmlspecialchars($users_image_one);
  $users_image_two = htmlspecialchars($users_image_two);
  $users_image_three = htmlspecialchars($users_image_three);
  $users_otherdetail = htmlspecialchars($users_otherdetail);




  $query = "
  INSERT INTO `vkrental`.`Owner_detail` (

`fullname` ,
`area` ,
`List1` ,
`List2` ,
`List3` ,

`ownermobile` ,
`email` ,
`image_one` ,
`image_two` ,
`image_three` ,
`otherdetail`
)
VALUES ( '$users_fullname',
        '$users_area', '$users_List1','$users_List2','$users_List3',     '$users_ownermobile', '$users_email',     '$users_image_one','$users_image_two','$users_image_three', '$users_otherdetail'
        );";

  mysql_query($query);

  echo "<h2>Success.</h2>";

   mysql_close($con);
}
 ?>

我的问题是,当我从mysql数据库插入图像时,它成功存储。 但是当我从我的html表单中执行此操作时,它不会存储图像。它只存储数据(文本)。

我不知道表单中的实际错误是什么。

2 个答案:

答案 0 :(得分:0)

我想知道使用htmlspecialchars()二进制数据会做什么。

$users_image_one = htmlspecialchars($users_image_one);
$users_image_two = htmlspecialchars($users_image_two);

[编辑:]

htmlspecialchars()用于保存文本数据的特殊字符。但是,如果你将它用于二进制数据,它会搞砸所有内容。

自行排查问题:

首先只上传图片,您可以尝试找出问题。

另请查看此链接:File upload using POST method

答案 1 :(得分:-1)

听起来不像是有错误。 。 。 。

您不会将实际图像存储在数据库中,而只是存储它们的名称或对图像的引用

e.g。 product_one.jpg

然后,当你想要显示它时,你会做类似

的事情
<img src=" <?php $echo \images\$users_image_one ?> ">