Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 上传图片并添加链接到mysql_Php_Mysql_Image_Content Management System - Fatal编程技术网

Php 上传图片并添加链接到mysql

Php 上传图片并添加链接到mysql,php,mysql,image,content-management-system,Php,Mysql,Image,Content Management System,我正在为我的网站创建一个CMS,在我的管理页面中,我有一个添加页面,向我的网站添加新内容,并添加了一些表单字段 其中2项是: 图像URL(文本框)和上载图像(选择文件按钮) 当我填写所有文件并使用图像URL选择图像并点击add article时,它工作正常,我的表单保存到数据库中,然后显示在我的网站上 当我填写所有文件并使用UPLOAD image和点击add article选择一个图像时,它会将图像添加到我的cpanel中所选的文件夹中,但不会添加到数据库中 我的问题是:如何将其添加到数据库中

我正在为我的网站创建一个CMS,在我的管理页面中,我有一个添加页面,向我的网站添加新内容,并添加了一些表单字段

其中2项是:

图像URL(文本框)和上载图像(选择文件按钮)

当我填写所有文件并使用图像URL选择图像并点击add article时,它工作正常,我的表单保存到数据库中,然后显示在我的网站上

当我填写所有文件并使用UPLOAD image和点击add article选择一个图像时,它会将图像添加到我的cpanel中所选的文件夹中,但不会添加到数据库中

我的问题是:如何将其添加到数据库中?并将新图像位置保存到数据库的图像字段中?

在将“上载文件”按钮添加到我的页面时,我遵循了本教程

请不要给我看如何做到这一点的链接,因为我已经通读了它们,但当涉及到将其添加到我的代码时,我步履蹒跚

我的add.php代码如下所示

<?php

session_start();

include_once('../include/connection.php');

if (isset($_SESSION['logged_in'])){
  if (isset($_POST['title'], $_POST['content'])) {
        $title   = $_POST['title'];
        $content = nl2br($_POST['content']);
        if (!empty($_POST['image']))
        {
            $image = $_POST['image'];
        }
        else
        {
            $image = $_POST['imageupload'];

            if (isset($_FILES['imageupload']))
            {
              $errors = array();
              $allowed_ext = array('jpg', 'jpeg', 'png', 'gif');

              $file_name = $_FILES['imageupload'] ['name'];
              $file_ext = strtolower (end (explode ('.', $file_name)));
              $file_size = $_FILES['imageupload'] ['size'];
              $file_tmp = $_FILES['imageupload'] ['tmp_name'];

              if (in_array ($file_ext, $allowed_ext) === false) {
                     $errors[] = 'File extension not allowed';
              }

              if ($file_size > 2097152) {
                     $errors[] = 'File size must be under 2mb';
              }

              if (empty($errors)) {
                     if (move_uploaded_file($file_tmp, 'images/'.$file_name)) {
                           echo 'File uploaded';
                           $image = $file_name;
              }
              }else{
                    foreach ($errors as $error)
                    echo $error, '<br />';
              }

            }
        }
        $link     = $_POST['link'];
        $category = $_POST['category'];
        $brand    = $_POST['brand'];


if (empty($title) or empty($content)) {
         $error = 'All Fields Are Required!';
}else{
 $query = $pdo->prepare('INSERT INTO mobi (promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name) VALUES(?, ?, ?, ?, ?, ?)');
 $query->bindValue(1, $title);
 $query->bindValue(2, $content);
 $query->bindValue(3, $image);
 $query->bindValue(4, $link);
 $query->bindValue(5, $category);
 $query->bindValue(6, $brand);



     $query->execute();
    header('location: index.php');
}

}
          ?>
    <?php

if (isset($_FILES['Filedata']))
{
// And if it was ok
    if ($_FILES['Filedata']['error'] !== UPLOAD_ERR_OK)
    exit('Upload failed. Error code: ' . $_FILES['image']['error']);

    $filename = $_FILES['Filedata']['name'];
    $targetpath    = "../img/news/" . $filename; //target directory relative to script location

    $copy = copy($_FILES['Filedata']['tmp_name'], $targetpath);
}
?>

<html>
<head>
<title>Add Article</title>
<link rel="stylesheet" href="../other.css" />
</head>

<body>
<div class="container">
<a href="index.php" id="logo"><b>&larr; Back</b></a>

<br />

<div align="center">
<h4>Add Article</h4>

<?php if (isset($error)) { ?>
     <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>

<form action="add.php" method="post" autocomplete="off" enctype="multipart/form-data">

<input type="text" name="title" placeholder="Title" /><br /><br />
<textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br />
<input name="imageupload" type="file" id="image" placeholder="Imageupload" />
<input type="text" name="image" placeholder="Image" /><br /><br />
<input type="link" name="link" placeholder="Link" /><br /><br />
<input type="category" name="category" placeholder="Category" /><br /><br />
<input type="category" name="brand" placeholder="Brand" /><br /><br />
<input type="submit" value="Add Article" />

</form>
</div>
</div>
</body>
</html>


<?php
}else{
       header('location: index.php');
}

?>

附加条款

附加条款













请帮忙

if (move_uploaded_file($file_tmp, 'images/'.$file_name)) {
                       echo 'File uploaded';                          
           $image = '/images/'.$filename;//try updating the line like this