如何使用php在mysql的longblob数据类型中存储图像

时间:2014-05-03 06:03:26

标签: php html mysql

我正在使用PHP开发Web应用程序。我有以下HTML表单:

<form action="addHotelDetails.php?action=<?php echo md5("check"); ?>" method="post" name="myform" id="myform" enctype="multipart/form-data"> 
<table cellspacing="5" cellpadding="5" border="0">
<tr>
<td align="right" width="35%" valign="top">Hotel Name</td>
<td><input type="text" name="hotel_name"  style="border:#666633 1px solid;background:none;" value="<?php if(isset($_POST['hotel_name'])) print $_POST['hotel_name']; ?>"  size="20" maxlength="40"/>
</td>
</tr>
<tr>
<tr>
<td align="right" valign="top">City</td>
<td><input type="text" name="city" style="border:#666633 1px solid;background:none;" size="15" value="<?php if(isset($_POST['city'])) print $_POST['city']; ?>" maxlength="95"/>
</td>
</tr>

<tr>
<td align="right" valign="top">State</td>
<td><input type="text" name="state" style="border:#666633 1px solid;background:none;" size="15" value="<?php if(isset($_POST['state'])) print $_POST['state']; ?>" maxlength="95"/>
</td>
</tr>

<tr>
<td align="right" valign="top">Phone Number</td>
<td><input type="text" name="phone_no" style="border:#666633 1px solid;background:none;" size="12" value="<?php if(isset($_POST['phone_no'])) print $_POST['phone_no']; ?>" maxlength="95"/>
</td>
</tr>

<tr>
<td align="right" valign="top">Email ID</td>
<td><input type="text" name="email_id" style="border:#666633 1px solid;background:none;" size="50" value="<?php if(isset($_POST['email_id'])) print $_POST['email_id']; ?>" maxlength="50" />
</td>
</tr>
<tr>
<td align="right" valign="top">Image</td>
<form action="addHotelDetails.php"
method="POST" enctype="multipart/form-data">
<td><input type="file" name="image" style="border:#666633 1px solid;background:none;" size="20" value="<?php if(isset($_POST['image'])) print $_POST['image']; ?>" maxlength="50" />
</td>
</tr>
</form>
<tr>
<td align="right"></td>
<td>
<input type="hidden" name="frmname" value="add_new_hotel" />
<input type="submit" name="submit "value="Submit" style="width:100px; height:35px; font-size:16px;"/>
<input type="button" name="back" value="Back" onclick="parent.location='hotelList.php'" style="width:100px; height:35px; font-size:16px;"/>
<input type="reset" name="reset" value="Reset" style="width:100px; height:35px; font-size:16px;"/>
</td>
</tr>
</table>
</form>     

现在我可以将所有数据发送到我的数据库表但无法发送图像。我有一个longblob数据类型的图像字段。我想从计算机浏览图像并将链接存储在我的表中。我在php中编写了以下代码来存储数据。请有人在php中编辑我的代码,以便将图像链接存储在我的表格中。

<?php
include_once "../dbconn.php";

if(isset($_REQUEST['action']) && $_REQUEST['action']==md5("check"))
{
    if(isset($_POST['frmname']) && $_POST['frmname']=='add_new_hotel')
{

      if(strlen(trim($_POST['hotel_name']))==0)
  print '<font style="color:#990000; padding-left: 120px">Please enter the Hotel name!!!</font>';
    else if(strlen(trim($_POST['city']))==0)
        print '<font style="color:#990000; padding-left: 120px">Please enter the city!!!</font>';
    else if(strlen(trim($_POST['state']))==0)
        print '<font style="color:#990000; padding-left: 120px">Please enter the state!!!</font>';
    else if(strlen(trim($_POST['phone_no']))==0)
        print '<font style="color:#990000; padding-left: 120px">Please enter the phone Number!!!</font>';
    else if(strlen(trim($_POST['email_id']))==0)
        print '<font style="color:#990000; padding-left: 120px">Please enter the email id!!!</font>';
    //else if(strlen(trim($_POST['image']))==0)
    //  print '<font style="color:#990000; padding-left: 120px">Please select an image!!!</font>';

    else    
    {   

        $hotel_name = $_POST['hotel_name']; 
        $city = $_POST['city'];
        $state = $_POST['state'];
        $phone_no = $_POST['phone_no']; 
        $email_id = $_POST['email_id']; 
        $image = $_POST['image']; 

        date_default_timezone_set('Asia/Calcutta');


        $sql = "INSERT INTO `hotel_details` (`hotel_name`, `city`, `state`, `phone_no`, `email_id`,`image`)
         VALUES ('$hotel_name', '$city','$state','$phone_no', '$email_id', $image) ";
        $result = mysql_query($sql, $conn);

        if(!$result) {
            print '<font style="color:#990000">Your query not saved. Please try again!!!</font>';
        }
        header("location: hotelList.php?");

    }
}

}

1 个答案:

答案 0 :(得分:0)

标准方式是将图像存储在服务器文件夹中并将链接保存到数据库。因为在这种情况下我们可以对图像进行进一步的操作。并且在longblob中保存图像,这是一个漫长的过程,需要更多时间才能正常保存。