【问题标题】:Get statement not adding to database获取语​​句不添加到数据库
【发布时间】:2019-03-25 13:35:48
【问题描述】:

我正在尝试在变量中使用 get 语句将数据添加到数据库中,当我尝试执行此操作时,文件夹下不会添加任何内容,但是如果我添加纯文本,则会添加它。 (我正在尝试添加到文件夹部分) 下面提供了我的整个 html 文档:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="newfile.css">
    <title>Folder</title>
</head>

<body>
<?php
    include_once 'dbh.php';
echo $_GET["data"];
   if(isset($_GET["data"])) {
    $location = $_GET["data"];
    $sql = "SELECT * FROM posts WHERE folder LIKE '%$location%'";
    $result = mysqli_query($conn, $sql);
    $queryResult = mysqli_num_rows($result);
    
    if($queryResult > 0) {
        while ($row = mysqli_fetch_assoc($result))
        {
            echo $row['content'];
        }
    } else {
            echo "There are no results matching your search!";
    }
    }
?>
<?php

$uploadpath = 'postimages/';      // directory to store the uploaded files
$max_size = 3116718;          // maximum file size, in KiloBytes
$alwidth = 100000;            // maximum allowed width, in pixeli
$alheight = 100000;           // maximum allowed height, in pixeli
$allowtype = array('bmp', 'gif', 'jpeg', 'jpg', 'jpe', 'png', 'docx', 'psd', 'pdf', 'pptx', 'html', 'php', 'css', 'js', 'mp4', 'mp3');        // allowed extensions

if(isset($_FILES['fileup'])) {
  $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);
  $name = basename( $_FILES['fileup']['name']);
  $type = end(explode('.', strtolower($_FILES['fileup']['name'])));
  list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     // gets image width and height
  $err = '';

  // Checks if the file has allowed type, size, width and height (for images)
  if(!in_array($type, $allowtype)) $err .= 'The file <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
  if($_FILES['fileup']['size'] > $max_size*900000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB';
  if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x 
  '. $alheight;

  // If no errors, upload the image, else, output the errors
  if($err == '') {
    if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
        $content =  $name;
        $realfiledest = $uploadpath;
        $username = 'user';
        $folder = $_GET['data'];
        $sql = "INSERT INTO posts (content, img, folder, date) VALUES ('$content', '$realfiledest',  '$folder', NOW());";
        $result = mysqli_query($conn, $sql);
        if($result !== false) {
            header("Location: fold.php?data=Math");    

        }else{
            echo "fail";
        }

    }
    else echo '<b>Unable to upload the file.</b>';
  }
  else echo $err;
}

?> 
<div class="upform">
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
            <div class="image-upload">
                <label for="file-input">
                    <img src="add-file.png" />
                </label>
                <input id="file-input" type="file" name="fileup"/>            
                
            </div>       
            <input class="noshow" type="text" id="wow" placeholder="<?php echo $_GET['data']; ?>" name='name'>          
            <input type="submit" id="submit" name='submit'  value="U P L O A D" /> 
        </form>
</div>
<script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(
    function(){
        $('input:file').change(
            function(){
                if ($(this).val()) {
                    $("#submit").show();
                    // or, as has been pointed out elsewhere:
                    // $('input:submit').removeAttr('disabled'); 
                } 
            }
            )     
    });
    </script>
</body>
</html>

url=http://localhost/smart%20assist/fold.php?data=Math

任何帮助都会很棒,谢谢

【问题讨论】:

  • 请用 HTML 表单上传您的完整代码?
  • 完成,非常感谢
  • 这是单页码还是多页码。

标签: javascript php html css mysql


【解决方案1】:

所以,如果您进行文件上传,您是在表单中使用多部分的 post 方法,对吧。

现在,如果你想用你的 url 作为查询参数发送一些东西,那么你必须使用$_REQUEST。在你的情况下,

if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
        $content =  $name;
        $realfiledest = $uploadpath;
        $username = 'user';
        $folder = $_REQUEST['data'];
        $sql = "INSERT INTO posts (content, img, folder, date) VALUES ('$content', '$realfiledest',  '$folder', NOW());";
        $result = mysqli_query($conn, $sql);

如果您遇到同样的问题,我需要检查您的 HTML 表单,以便更好地帮助您。

【讨论】:

  • @AnthonyWaymouth 如果解决了您的问题,请投票作为答案
【解决方案2】:

我认为你犯了以下错误。

  1. 您没有在 HTML 中发布“数据”字段值。
  2. 您必须输入名称字段才能获取数据。
  3. 您正在发布使用 post 方法的表单。所以你应该使用$_POST[]$_REQUEST[]

请验证您的 HTML。

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多