每次插入(int)$ _ GET ['id']时,我得到0值;在数据库中

时间:2019-02-24 09:55:43

标签: php html mysql

我有一个问题,当我尝试插入Get ['id']的值时,在打印ID时我总是得到0,甚至!我能看到它。 Upload.php: 你好, 我有一个问题,当我尝试插入Get ['id']的值时,在打印ID时我总是得到0,甚至!我能看到它。 Upload.php:

    <?php
    include '../include/connect.php';
    $id =(int)$_GET['id'];
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_img" />
<input type="submit" name="btn_upload" value="Upload">
</form>

<?php
if(isset($_POST['btn_upload']))
{
    $filetmp = $_FILES["file_img"]["tmp_name"];
    $filename = $_FILES["file_img"]["name"];
    $filetype = $_FILES["file_img"]["type"];
    $filepath = "photo/".$filename;

    move_uploaded_file($filetmp,$filepath);
    $sql = "INSERT INTO upload_img (img_name,img_path,img_type,im_id) VALUES ('$filename','$filepath','$filetype','.$id.')";
    mysqli_query($con, $sql);
}
?>

</body>
</html>

和我的SQL:

数据库图片

enter image description here

我尝试使用此链接:

upload.php?id=2

3 个答案:

答案 0 :(得分:1)

发布postup.php时,您失去了GET参数。之后,将$ _GET ['id'](空字符串)转换为(int),然后php将其设置为0。

<form action="upload.php?id=<?php echo $id ?>" method="post" enctype="multipart/form-data">

<form action="" method="post" enctype="multipart/form-data">

或在表单中创建一个隐藏的输入字段,然后通过$ _POST ['id']

访问它
<input type="hidden" name="id" value="<?php echo $id; ?>" />

答案 1 :(得分:0)

您正在尝试插入字符串而不是int值。您必须删除$id的引号,例如:

    move_uploaded_file($filetmp,$filepath);
    $sql = "INSERT INTO upload_img (img_name,img_path,img_type,im_id) VALUES ('$filename','$filepath','$filetype',$id)";
    mysqli_query($con, $sql);

答案 2 :(得分:0)

使用以下代码,您必须在提交时在表单中传递ID。

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Program {

    private static final int NUMBER_OF_TASKS = 40;
    private static final int NUMBER_OF_THREADS = 10;
    private static final long EVENT_LOOP_PAUSE_DURATION_IN_MS = 40L;

    class QueueSizePrinter extends Thread {

        private final LinkedBlockingQueue<Runnable> workQueue;

        public QueueSizePrinter(LinkedBlockingQueue<Runnable> workQueue) {
            this.workQueue = workQueue;
        }

        @Override
        public void run() {
            while (true) {
                int qSize = workQueue.size();
                if (qSize > NUMBER_OF_TASKS) {
                    System.out.println("ERROR: " + qSize);
                }

                try {
                    Thread.sleep(500L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class TaskProcessor implements Runnable {
        private final String currentTaskId;
        private final ConcurrentHashMap<String, Long> activeTasks;

        public TaskProcessor(String currentTaskId, ConcurrentHashMap<String, Long> activeTasks) {
            this.currentTaskId = currentTaskId;
            this.activeTasks = activeTasks;
        }

        @Override
        public void run() {
            try {
                // emulate of useful work
                Thread.sleep(300L);
            } catch (Exception e) {
                System.out.println("error: " + e.toString());
            } finally {
                activeTasks.remove(currentTaskId); // (1)
            }
        }
    }

    public void program() {

        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
        ExecutorService executor = new ThreadPoolExecutor(NUMBER_OF_THREADS, NUMBER_OF_THREADS, 0L, TimeUnit.MILLISECONDS, workQueue);

        Set<String> initialTasks = ConcurrentHashMap.newKeySet();
        for (int currentTaskIndex = 0; currentTaskIndex < NUMBER_OF_TASKS; currentTaskIndex++) {
            initialTasks.add(String.valueOf(currentTaskIndex));
        }

        new QueueSizePrinter(workQueue).start();

        ConcurrentHashMap<String, Long> activeTasks = new ConcurrentHashMap<>();

        while (true) {

            initialTasks.forEach((currentTaskId) -> {
                if (!activeTasks.containsKey(currentTaskId)) {
                    activeTasks.put(currentTaskId, System.currentTimeMillis());

                    executor.submit(new TaskProcessor(currentTaskId, activeTasks));
                }
            });

            // (2)
            activeTasks.entrySet().removeIf(entry -> {
                boolean hasDelete = System.currentTimeMillis() - entry.getValue() > 1000;
                if (hasDelete) {
                    //System.out.println("DELETE id=" + entry.getKey());
                }
                return hasDelete;
            });

            try {
                Thread.sleep(EVENT_LOOP_PAUSE_DURATION_IN_MS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Program main = new Program();
        main.program();
    }
}