【问题标题】:save a link to mysql database php保存到mysql数据库php的链接
【发布时间】:2017-10-10 17:43:38
【问题描述】:

我有一个 html 表单(用于博客文章),我想将所有内容保存在数据库表中。它工作正常,但是当我为图像添加一列时,它也可以以相同的 html 表单上传,然后它没有保存任何东西。

代码如下:

$imagepath = 'https://myurl.com/uploads/' . $_FILES['image']['name'];

  $db = new PDO($dsn, $dbuser, $dbpass);
  $query = $db->prepare(
    "INSERT INTO posts (author, title, text, date, image)
  VALUES(:author, :title, :text, NOW()), '$imagepath'");
  $query->execute(array("author" => $author, "title" => $title, "text" => $text));
  $db = null;

【问题讨论】:

    标签: mysql sql hyperlink


    【解决方案1】:

    右括号放错了

      "INSERT INTO posts (author, title, text, date, image)
      VALUES(:author, :title, :text, NOW()), '$imagepath'");
    

    应该是

      "INSERT INTO posts (author, title, text, date, image)
      VALUES(:author, :title, :text, NOW(), '$imagepath' )");
    

    【讨论】:

      【解决方案2】:

      试试这个..

      <?php
      
      if(isset($_POST['Upload'])) // upload button press
      {
      
      $file_name = $_FILES['file']['name'];
      $file_size = $_FILES['file']['size'];
      $file_type = $_FILES['file']['type'];
      $file_temp = $_FILES['file']['tmp_name'];
      
      $folder = "uploads/";
      $file_url="http://localhost/../../uploads/$file_name"; //file location
      
      move_uploaded_file($file_temp,"Song_uploads/".$file_name);
      
      $sql="INSERT INTO imagepost(file_NAME,file_URL,file_SIZE) VALUES(:file_name,:file_url,:file_size)";
      
      
          $stmt= $conn->prepare($sql);
      
      
          $stmt->bindParam(':file_name',$file_name);
          $stmt->bindParam(':file_url',$file_url);
          $stmt->bindParam(':file_size',$file_size);
      
      
          if($stmt->execute())
            {
      
          $message="<br/><h1> ---> "."[".$file_name."]"." File Has Been Uploaded !! </h1>";
      
            }
            else
            {
          $message="<br/> <h1> ---> "."[".$file_name."]"." File Not Be Uploaded !!  Please Try Again !! </h1>";
            }
      }
      ?>
      

      【讨论】:

      • 我不知道你为什么删除你最近的帖子......但你可以试试这个..
      • w3schools.com/php/php_file_upload.asp ... 如果你想要更多的东西,你可以看到这个。 ...
      猜你喜欢
      • 2011-04-01
      • 2012-09-29
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      相关资源
      最近更新 更多