使用简单的PHP代码将带图像的新闻添加到数据库。但坚持结果

时间:2014-01-21 10:10:54

标签: php mysql

我想存储带有文字和图片的新闻,但没有任何内容存储在数据库中,我也没有收到任何错误。 感谢

SQL:

 CREATE TABLE IF NOT EXISTS `news` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `topic` varchar(70) NOT NULL,
  `datetime` datetime NOT NULL,
  `name` varchar(70) NOT NULL,
  `image` blob NOT NULL,
  `news` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

添加帖子php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add News</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <p>
    <label for="textfield">Topic:</label>
    <input type="text" name="topic" id="textfield">
  </p>
  <p>
    <label for="datetime">DateTime:</label>
    <input type="datetime" name="datetime" id="datetime">
  </p>
  <p>
    <label for="textarea">News:</label>
    <textarea name="news" id="textarea"></textarea>
  </p>
  <p>
    <label for="fileField">Upload Image:</label>
    <input type="file" name="image" id="fileField">
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Upload">
  </p>
</form>
</body>
</html>

<?php
// connect to database
mysql_connect ("localhost", "root", "") or die (mysql_error());
mysql_select_db ("webnews") or die (mysql_error());
// file properties
$topic = $_POST["topic"];
$datetime = $_POST["datetime"];
$news = $_POST["news"];
$file = $_FILES['image']['tmp_name'];

if (!isset ($topic, $datetime, $news, $file))
echo "Please fill up your data.";
else
{
    $image = file_get_contents ($_FILES['image']['tmp_name']);
    $image_name = addslashes ($_FILES['image']['name']);
    $image_size = getimagesize ($_FILES['image']['tmp_name']);

if ($image_size==FALSE)
    echo "That's not an image.";
else
{

    if (!$insert = mysql_query ("INSERT INTO news VALUES 
    ('','$image_name','$image','$topic','$datetime', '$news')"))
    echo "Problem uploading image.";
else
    {
    $lastid = mysql_insert_id ();
    echo " Image Uploaded. <p /> Your image: <p />
    <img src'get.php?id=$lastid>";
    }
}
}

?>

获取帖子php

<?php
// connect to database
mysql_connect ("localhost", "root", "") or die (mysql_error());
mysql_select_db ("webnews") or die (mysql_error());

$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * from news WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpeg");

echo $image;

?>

在php中同时添加一些带有图像的文本值是否存在问题。我试图这么简单,而不是做一个复杂的代码。

0 个答案:

没有答案