PHP在服务器上传两个文件,在mySQL db中上传信息

时间:2017-04-05 03:07:41

标签: php mysql upload insert crud

我发现将此上传到mySQL数据库几乎是不可能的。 " create_news"文件当前正在将我的两个文件(文件和图像)上传到我的服务器上,但是当它试图将信息插入我的mySQL数据库时我收到此错误:

错误:可见,主页,类型,publication_name,publication_url,月份,日期,年份,标题,作者,摘要,full_text,tag_1,tag_2,tag_3,tag_4,tag_5,tag_6,tag_7,image,image_type,image_size,file ,file_type,file_size)价值观(' N',' N',' N','','&#39 ;,''','',' t','',&#39 ;','','','','','' ,'','','',' 18068 - ','',&# 39; 0',' 8776 - ','',' 0')

在添加文件上传之前,上传已插入mySQL。这是我的表单页面:

 <?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
    <?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
    <?php session_start();
    if($_SESSION["login_user"] != true) {
    echo("Access denied!");
    exit();
}
?>
    <?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>
    <?php find_selected_news_page(); ?>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Washington Jesuit Adacemy: A high quality and comprehensive middle school for 5th through 8th grade boys from low-income communities in the DC metro area.">
  <meta name="keywords" content="">
  <meta name="author" content="Washington Jesuit Academy">
  <title>Washington Jesuit Academy: About</title>
  <link rel="stylesheet" type="text/css" href="https://cloud.typography.com/6113114/6497572/css/fonts.css"/>
  <link rel="stylesheet" type="text/css" href="https://cloud.typography.com/683054/7063952/css/fonts.css"/>
  <link href="/assets/css/wja.css" rel="stylesheet" type="text/css">
  <link href="/assets/css/wja-startup.css" rel="stylesheet">
</head>

<body>
  <?php include($_SERVER['DOCUMENT_ROOT']."/includes/navigation.php");?>
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script type="text/javascript">
  tinymce.init({
    selector: 'textarea',
    theme: 'modern',
    plugins: [
      'advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker',
      'searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking',
      'save table contextmenu directionality template paste textcolor'
    ],
    content_css: 'css/content.css',
    menubar: 'edit view format',
    toolbar: 'removeformat | bold italic | bullist numlist outdent indent | link anchor | nonbreaking charmap hr | print preview fullpage | code'
  });
  </script>
<div class="content">
<div class="block">
  <div class="container">
    <div id="col-md-12"> <?php echo message(); ?> <?php echo form_errors($errors); ?>
      <h2>New article or news item</h2>
      <form action="create_news.php" method="post" class="form-horizontal" enctype="multipart/form-data">
        <div class="form-group">
          <label for="visible" class="col-sm-2 control-label">Published</label>
          <div class="col-sm-10">
            <label class="radio-inline">
              <input type="radio" name="visible" id="visible1" value="N" checked>
              No </label>
            <label class="radio-inline">
              <input type="radio" name="visible" id="visible2" value="Y">
              Yes </label>
            <p class="small">If &lsquo;Yes&rsquo; is selected, the item will be publicly visible on the website as soon as it is saved.</p>
          </div>
        </div>

        <div class="form-group">
          <label for="homepage" class="col-sm-2 control-label">Featured on Homepage</label>
          <div class="col-sm-10">
            <label class="radio-inline">
              <input type="radio" name="homepage" id="homepage1" value="N" checked>
              No </label>
            <label class="radio-inline">
              <input type="radio" name="homepage" id="homepage2" value="Y">
              Yes </label>
            <p class="small">If &lsquo;Yes&rsquo; is selected, the item will also be featured on the <strong>homepage</strong>.</p>
          </div>
        </div>

        <div class="form-group">
          <label for="type" class="col-sm-2 control-label">Type</label>
          <div class="col-sm-10">
            <label class="radio-inline">
              <input type="radio" name="type" id="type1" value="N" checked>
              News </label>
            <label class="radio-inline">
              <input type="radio" name="type" id="type2" value="A">
              President's Blog </label>
          </div>
        </div>
        <div class="form-group">
          <label for="publication_name" class="col-sm-2 control-label">Publication Name</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="publication_name" value="" />
          </div>
        </div>
        <div class="form-group">
          <label for="publication_url" class="col-sm-2 control-label">Publication URL</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="publication_url" value="" />
          </div>
        </div>
        <div class="form-group">
          <label for="month" class="col-sm-2 control-label">Month</label>
          <div class="col-sm-10">
            <select class="form-control" name="month">
              <option value="">Select Month</option>
              <option value="January">January</option>
              <option value="February">February</option>
              <option value="March">March</option>
              <option value="April">April</option>
              <option value="May">May</option>
              <option value="June">June</option>
              <option value="July">July</option>
              <option value="August">August</option>
              <option value="September">September</option>
              <option value="October">October</option>
              <option value="November">November</option>
              <option value="December">December</option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <label for="date" class="col-sm-2 control-label">Date</label>
          <div class="col-sm-10">
            <select class="form-control" name="date">
              <option value="">Select Date</option>
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
              <option value="6">6</option>
              <option value="7">7</option>
              <option value="08">8</option>
              <option value="09">9</option>
              <option value="10">10</option>
              <option value="11">11</option>
              <option value="12">12</option>
              <option value="13">13</option>
              <option value="14">14</option>
              <option value="15">15</option>
              <option value="16">16</option>
              <option value="17">17</option>
              <option value="18">18</option>
              <option value="19">19</option>
              <option value="20">20</option>
              <option value="21">21</option>
              <option value="22">22</option>
              <option value="23">23</option>
              <option value="24">24</option>
              <option value="25">25</option>
              <option value="26">26</option>
              <option value="27">27</option>
              <option value="28">28</option>
              <option value="29">29</option>
              <option value="30">30</option>
              <option value="31">31</option>
 </select>
          </div>
        </div>
        <div class="form-group">
        <label for="year" class="col-sm-2 control-label">Year</label>
        <div class="col-sm-10">
          <select class="form-control" name="year">
            <option value="">Select Year</option>
            <option value="2000">2000</option>
            <option value="2001">2001</option>
            <option value="2002">2002</option>
            <option value="2003">2003</option>
            <option value="2004">2004</option>
            <option value="2005">2005</option>
            <option value="2006">2006</option>
            <option value="2007">2007</option>
            <option value="2008">2008</option>
            <option value="2009">2009</option>
            <option value="2010">2010</option>
            <option value="2011">2011</option>
            <option value="2012">2012</option>
            <option value="2013">2013</option>
            <option value="2014">2014</option>
            <option value="2015">2015</option>
            <option value="2016">2016</option>
            <option value="2017">2017</option>
            <option value="2018">2018</option>
            <option value="2019">2019</option>
            <option value="2020">2020</option>
          </select>
        </div>
        </div>
        <div class="form-group">
          <label for="title" class="col-sm-2 control-label">Title</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="title" value="" />
          </div>
        </div>
        <div class="form-group">
          <label for="author" class="col-sm-2 control-label">Author</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="author" value="" />
              <p class="small">If this is a President&rsquo;s Blog, type <strong>Bill Whitaker, Founding President</strong></p>

          </div>

        </div>
        <div class="form-group margin-t-25">
          <label for="summary" class="col-sm-2 control-label">Summary</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="summary" value="" />
          </div>
        </div>
        <div class="form-group margin-t-25">
          <label for="full_text" class="col-sm-2 control-label">Full Text</label>
          <div class="col-sm-10">
            <textarea name="full_text" rows="10" style="width:100%"></textarea>
          </div>
        </div>

    <div class="form-group">
          <label class="col-sm-2 control-label">Optional Tag(s)</label>
          <div class="col-sm-5">
            <label class="checkbox m-l">
              <input type="checkbox" name="tag_1" value="Y">
              Tag 1 </label>
            <label class="checkbox m-l">
              <input type="checkbox" name="tag_2" value="Y">
              Tag 2 </label>
            <label class="checkbox m-l">
              <input type="checkbox" name="tag_3" value="Y">
              Tag 3 </label>
            <label class="checkbox m-l">
              <input type="checkbox" name="tag_4" value="Y">
              Tag 4 </label>
          </div>
          <div class="col-sm-5">
            <label class="checkbox m-l">
              <input type="checkbox" name="tag_5" value="Y">
             Tag 5 </label>
            <label class="checkbox m-l">
              <input type="checkbox" name="tag_6" value="Y">
             Tag 6</label>
            <label class="checkbox m-l">
              <input type="checkbox" name="tag_7" value="Y">
             Tag 7</label>
          </div>
        </div>
            <div class="form-group"><hr />
   <label for="full_text" class="col-sm-2 control-label">Image Upload </label>
          <div class="col-sm-10">
        <input type="file" name="image" class="mt-3"/>
       </div>  </div>

        <div class="form-group"><hr />
   <label for="full_text" class="col-sm-2 control-label">File Upload </label>
          <div class="col-sm-10">
        <input type="file" name="file" class="mt-3"/>
       </div>  </div>
        <div class="text-right">
          <input type="submit" name="submit" class="btn btn-primary m-t-md m-r" value="Create item" />
          <a href="manage_content.php" class="btn btn-default m-t-md">Cancel</a></div>
      </form>
    </div>
  </div>
</div>
<?php include($_SERVER['DOCUMENT_ROOT']."/includes/footer.php");?>
<!-- JavaScript --> 
<script src="../assets/js/jquery.min.js"></script> 
<script src="../assets/js/toolkit.js"></script> 
<script src="../assets/js/application.js"></script>
</div>
</body></html><?php
  // Close database connection
  if (isset($connection)) {
  mysqli_close($connection);
  }
?>

这是我的#34;创造&#34;页:

<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php
session_start();
if($_SESSION["login_user"] != true) {
    echo("Access denied!");
    exit();
}
?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/validation_functions.php");?>
<?php
if (isset($_POST['submit'])) {
    // Process the form
        $visible = mysql_prep($_POST["visible"]);
        $homepage = mysql_prep($_POST["homepage"]);
        $type = mysql_prep($_POST["type"]);
        $publication_name = mysql_prep($_POST["publication_name"]);
        $publication_url = mysql_prep($_POST["publication_url"]);
        $month = mysql_prep($_POST["month"]);
        $date = mysql_prep($_POST["date"]);
        $year = mysql_prep($_POST["year"]);
        $title = mysql_prep($_POST["title"]);
        $author = mysql_prep($_POST["author"]);
        $summary = mysql_prep($_POST["summary"]);
        $full_text = mysql_prep($_POST["full_text"]);
        $tag_1 = mysql_prep($_POST["tag_1"]);
        $tag_2 = mysql_prep($_POST["tag_2"]);
        $tag_3 = mysql_prep($_POST["tag_3"]);
        $tag_4 = mysql_prep($_POST["tag_4"]);
        $tag_5 = mysql_prep($_POST["tag_5"]);
        $tag_6 = mysql_prep($_POST["tag_6"]);
        $tag_7 = mysql_prep($_POST["tag_7"]);
        $image = rand(1000,100000)."-".$_FILES['image']['name'];
        $image_loc = $_FILES['image']['tmp_name'];
        $image_size = $_FILES['image']['size'];
        $image_type = $_FILES['image']['type'];
        $image_folder="images/";
        $file = rand(1000,100000)."-".$_FILES['file']['name'];
        $file_loc = $_FILES['file']['tmp_name'];
        $file_size = $_FILES['file']['size'];
        $file_type = $_FILES['file']['type'];
        $file_folder="files/";

 $image_new_size = $image_size/1024;  
 $file_new_size = $file_size/1024;  
 $new_image_name = strtolower($image);
 $new_file_name = strtolower($file);


 $final_image=str_replace(' ','-',$new_image_name);
 $final_file=str_replace(' ','-',$new_file_name);

 if(move_uploaded_file($image_loc,$image_folder.$final_image))
 if(move_uploaded_file($file_loc,$file_folder.$final_file))

    $query  = "INSERT INTO `news` (";
    $query .= "visible, homepage, type, publication_name, publication_url, month, date, year, title, author, summary, full_text, tag_1, tag_2, tag_3, tag_4, tag_5, tag_6, tag_7, image, image_type, image_size, file, file_type, file_size ";
    $query .= ") VALUES (";
    $query .= "  '{$visible}', '{$homepage}', '{$type}', '{$publication_name}', '{$publication_url}', '{$month}', '{$date}', '{$year}', '{$title}', '{$author}', '{$summary}', '{$full_text}', '{$tag_1}', '{$tag_2}', '{$tag_3}', '{$tag_4}', '{$tag_5}', '{$tag_6}', '{$tag_7}', '{$final_image}','{$image_type}','{$image_new_size}', '{$final_file}','{$file_type}','{$file_new_size}'";
    $query .= ")";
    $result = mysqli_query($connection, $query);

    if ($result) {
        // Success
        $_SESSION["message"] = "Item created.";
        redirect_to("manage_content.php");
    } else {
        // Failure
        //$_SESSION["message"] = "Item creation failed.";
        //redirect_to("new_news.php");
        echo "Error: " . $query . "<br>" . $result->error;
    }



} else {
    // This is probably a GET request
    redirect_to("new_news.php");
}

?>

<?php
    if (isset($connection)) { mysqli_close($connection); }
?>

0 个答案:

没有答案