【问题标题】:mysql php - data not entering fully into databasemysql php - 数据未完全进入数据库
【发布时间】:2015-01-20 14:18:52
【问题描述】:

我目前正在建立一个网站,该网站有一个通过 php 和 mysql 访问的画廊。当您在网站上查看时,图库会从 mysql 获取代码,从我的文件夹(在机器上)获取图像。

我遇到一个问题,当我上传图像(使用 php)时,会出现 image_pathname,image_description(使用 image_gallery 数据库)也会出现,但作者没有 - 而是出现了一个数字。

我认为这可能是我可以轻松解决的问题,但我已经在网站上工作了一段时间,我的大脑被炸了 - 假设这是不可能的事情。

这是我的表格语法:

CREATE TABLE images(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
image_pathname VARCHAR( 50 ) ,
image_author VARCHAR( 50 ) ,
image_description VARCHAR( 50 ) ,
genreID VARCHAR( 100 )
)

这是我的 HTML:

<form method="post" action="upload_file.php" enctype="multipart/form-data">

            <p>
              Image Author:
            </p>
            <input type="text" name="image_author"/>

            <p>
              Please enter a decscription:
            </p>
            <input type="text" name="image_description"/>
            <p>
             Please upload an image.
            </p>
            <p>
              Photo:
            </p>
            <input type="hidden" name="size" value="350000">
            <input type="file" name="photo"> 


            <input TYPE="submit" name="upload" title="Add image/data to the Database" value="Add Image"/>
          </form>

这是我的 php:

<?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$name= (isset($_POST['image_author']));
$description= ($_POST['image_description']);
$pic=($_FILES['photo']['name']);


// Connects to your Database
mysql_connect("localhost", "root", "root") or die(mysql_error()) ;
mysql_select_db("image_gallery") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO images (image_author, image_description, image_pathname)
VALUES ('$name', '$description', '$pic')") ;

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

//Tells you if its all ok
echo "The file has been uploaded, and your information has been added to the directory <p> <a href='upload.php'> Go back</a>";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

【问题讨论】:

标签: php mysql sql sql-insert


【解决方案1】:

将值分配给变量时,您错误地调用了isset()。结果是您得到一个布尔值,即isset() retuens) 而不是$_POST['image_author'] 的实际值。

 $name= (isset($_POST['image_author']));

应该是

$name= $_POST['image_author'];

仅供参考,您对SQL injections 持开放态度并使用obsolete API

【讨论】:

    【解决方案2】:

    您可以像这样分配表单变量,并查看 Jay Blanchard 关于 MYSQL API 的评论,改用 MYSQLIPDO 这将帮助您编写 SQL INJECTION 免费代码。

    //This gets all the other information from the form
    $name= (isset($_POST['image_author']))? $_POST['image_author']:'';
    $description= (isset($_POST['image_description']))? $_POST['image_description']:'';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 2011-12-11
      • 1970-01-01
      • 2018-09-13
      • 2015-03-27
      • 2012-10-19
      相关资源
      最近更新 更多