Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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表单_Php_Mysql - Fatal编程技术网

带有文本输入和图像上传器的php表单

带有文本输入和图像上传器的php表单,php,mysql,Php,Mysql,大家好,我正在寻找一些关于如何构建包含文本和图像上传器的php表单的指导。我可以分别做这两种形式,但在结合起来有点困难 我的html表单: <form name="news-page" action="" method="POST" enctype="multipart/form-data"> <h1>News</h1> <span id="newstitle"> <p id="news

大家好,我正在寻找一些关于如何构建包含文本和图像上传器的php表单的指导。我可以分别做这两种形式,但在结合起来有点困难

我的html表单:

<form name="news-page" action="" method="POST" enctype="multipart/form-data">
    <h1>News</h1>
    <span id="newstitle">
                    <p id="newstitle">News Title</p>
                    <input id="title" type="text" name="newstitle" value="News Title"/>
                </span>
    <span id="newsdate">
                    <p>News Date</p>
                    <input id="news_date" type="text" name="newsdate" value="News Date"/>               
                </span>
    <span id="category">
                    <p>News Category</p>
                    <input id="newscategory" type="text" name="newscategory" value="News Category"/>
                </span>
    <p id="news_info">News Information</p>
    <textarea id="newsinfo" name="newstext">Bacon ipsum dolor amet turducken boudin sirloin ..</textarea>
    <div id="newsimage">
        <img src/>
        <p>Insert News Image</p>
        <label class="myLabel" id="news-image-upload">
            <input type="file" required name="newsuploader" id="fileToUpload" />
            <span>Select Image</span>
        </label>
        <button type="submit" name="add_news_btn">Add News</button>
    </div>
</form>
上传图片的代码

if (isset($_FILES['newsuploader'])) {
    if ($_FILES["newsuploader"]["error"] > 0) {
        echo "No file chosen</br>";
        echo "Database fail</br>";
    }
    else {
        move_uploaded_file($_FILES["newsuploader"]["tmp_name"], "../media/images/" . $_FILES["newsuploader"]["name"]);
        echo "Saved";
        $file = "media/images/" . $_FILES["newsuploader"]["name"];
        include 'connect.php';

        if (!mysqli_select_db($conn, "mostacms_db")) {
            echo "Error: " . mysql_error();
        }
        else echo "all good";
    }

    $stmt = $conn->prepare("INSERT INTO news(imageURL) VALUES(?)");
    $stmt->bind_param('s', $file);
    $stmt->execute();
    $conn->close();
}

试着改变这种说法

$stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory) VALUES(?, ?, ?, ?)");

$stmt->bind_param('ssss', $newsdate, $newstitle, $newstext, $newscatagory);

to

$stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory) VALUES(?, ?, ?, ?,?)");

$stmt->bind_param('sssss', $newsdate, $newstitle, $newstext, $newscatagory,$file);

尝试发布到这个php文件中

if (isset($_POST['add_news_btn'])) {
    include 'connect.php';
    $newsdate = (isset($_POST['newsdate']) ? $_POST['newsdate'] : null);
    $newstitle = (isset($_POST['newstitle']) ? $_POST['newstitle'] : null);
    $newscatagory = (isset($_POST['newscategory']) ? $_POST['newscategory'] : null);
    $newstext = (isset($_POST['newstext']) ? $_POST['newstext'] : null);

    // upload file
    if (isset($_FILES['newsuploader'])) {
      if(move_uploaded_file($_FILES["newsuploader"]["tmp_name"], "../media/images/" . $_FILES["newsuploader"]["name"]))
        echo "Saved";
      $imageURL = "media/images/" . $_FILES["newsuploader"]["name"];
    }
    else
      $imageURL='';

    // update details to DB
    $stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory,imageURL ) VALUES(?, ?, ?, ?,?)");
    $stmt->bind_param('sssss', $newsdate, $newstitle, $newstext, $newscatagory, $imageURL);
    $stmt->execute();
    $stmt->close();
}
注意:为避免重复文件插入,请在保存之前尝试将文件名更改为唯一的文件名。

美元POST的内容是什么-使用var\u dump美元POST时会发生什么?
if (isset($_POST['add_news_btn'])) {
    include 'connect.php';
    $newsdate = (isset($_POST['newsdate']) ? $_POST['newsdate'] : null);
    $newstitle = (isset($_POST['newstitle']) ? $_POST['newstitle'] : null);
    $newscatagory = (isset($_POST['newscategory']) ? $_POST['newscategory'] : null);
    $newstext = (isset($_POST['newstext']) ? $_POST['newstext'] : null);

    // upload file
    if (isset($_FILES['newsuploader'])) {
      if(move_uploaded_file($_FILES["newsuploader"]["tmp_name"], "../media/images/" . $_FILES["newsuploader"]["name"]))
        echo "Saved";
      $imageURL = "media/images/" . $_FILES["newsuploader"]["name"];
    }
    else
      $imageURL='';

    // update details to DB
    $stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory,imageURL ) VALUES(?, ?, ?, ?,?)");
    $stmt->bind_param('sssss', $newsdate, $newstitle, $newstext, $newscatagory, $imageURL);
    $stmt->execute();
    $stmt->close();
}