使用PHP上传图像并将数据存储在MSSQL中 [英] Using PHP to upload an image and store data in MSSQL

查看:156
本文介绍了使用PHP上传图像并将数据存储在MSSQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传图片以及添加详细信息标题,描述和文件路径到数据库表中。



我使用下面的代码,但是没有向数据库添加任何数据;

(该session.php包含了包含数据库连接的内容)

 <?php包括( '包括/ session.php文件'); 

$ uploadDir ='已提交/图片/';

if(isset($ _ POST ['submit']))
{
$ fileName = $ _FILES ['file'] ['name'];
$ tmpName = $ _FILES ['file'] ['tmp_name'];
$ fileSize = $ _FILES ['file'] ['size'];
$ fileType = $ _FILES ['file'] ['type'];

$ filePath = $ uploadDir。 $文件名;

$ result = move_uploaded_file($ tmpName,$ filePath);
if(!$ result){
echo错误上传< strong>文件< / strong>;
出口; ($!


if(!get_magic_quotes_gpc())
{
$ fileName = addslashes($ fileName);
$ filePath = addslashes($ filePath);
}

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

$ query =INSERT INTO $ user_pictures(file,title,description)VALUES('$ filePath','$ title','$ description');

mssql_query($ query);

}

?>

表单代码;

 < form name =Imageenctype =multipart / form-dataaction =upload-pics2.phpmethod =POST> 
标题< input type =textname =titlemaxlength =100class =textboxvalue =<?php echo $ form-> value(title);?> ; />
描述< textarea name =descriptionrows =8cols =40class =textboxvalue =<?php echo $ form-> value(description);?> ;>< / textarea的>
File< input type =filename =fileaccept =image / gif,image / jpeg,image / x-ms-bmp,image / x-pngsize =26class = textbox/>
< input type =submitname =submitvalue =Uploadclass =button/>
< / form>

我想知道是否有人可以告诉我可能会出错?



谢谢。

解决方案

这段代码因为几个问题而不起作用。



首先,当您检查上传时,您应该重命名一个html字段或更改字段名称:

 < input type =submitname =Uploadvalue =Uploadclass =button/> 

  if(isset($ _ POST ['submit']))

脚本不会将任何数据存储到数据库中。
您应该获得,清理和写入数据到相应的字段,例如:

$ $ p $ $ $ $ $ $ title = mysql_real_escape_string($ _ POST ['标题']);
$ description = mysql_real_escape_string($ _ POST ['description']);
$ query =INSERT INTO $ user_pictures(file,title,description)VALUES('$ filePath','$ title','$ description');

您应该确保这些字段在DB中存在,否则应该创建它们:

  ALTER table user_pictures ADD column description text,add column title varchar(255); 


I'm attempting to upload an image as well as add details such as; title, description and filepath into a database table.

I'm using the following code, but it isn't adding any data to the database;

(The session.php include contains the database connectivity.)

<?php include('includes/session.php');

$uploadDir = 'submitted/pictures/';

if(isset($_POST['submit']))
{
$fileName = $_FILES['file']['name'];
$tmpName  = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading <strong>file</strong>";
exit;
}

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
    $filePath = addslashes($filePath);
}

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

$query = "INSERT INTO $user_pictures (file, title, description) VALUES ('$filePath', '$title', '$description')";

mssql_query($query); 

}

?>

The form code;

<form name="Image" enctype="multipart/form-data" action="upload-pics2.php" method="POST">
 Title <input type="text" name="title" maxlength="100" class="textbox" value="<?php echo $form->value("title"); ?>" />
 Description <textarea name="description" rows="8" cols="40" class="textbox" value="<?php echo $form->value("description"); ?>"></textarea>
 File <input type="file" name="file" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26" class="textbox" />
 <input type="submit" name="submit" value="Upload" class="button" />
</form>

I was wondering if someone could tell me what might be going wrong?

Thank you.

解决方案

This code do not work because of several problems.

First, you should rename one of html fields or change field name when you are checking for upload:

<input type="submit" name="Upload" value="Upload" class="button" />

or

if(isset($_POST['submit']))

Second one, this script will not store any data into DB. You should get, sanitize and write data into according fields, for example:

$title = mysql_real_escape_string($_POST['title']);
$description = mysql_real_escape_string($_POST['description']);
$query = "INSERT INTO $user_pictures (file, title, description) VALUES ('$filePath', '$title', '$description')";

You should make sure these fields present in DB, if not - you should create them:

ALTER table user_pictures ADD column description text, add column title varchar(255);

这篇关于使用PHP上传图像并将数据存储在MSSQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆