简体   繁体   English

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

[英]php form with text inputs and image uploader

Hi all I'm looking for some guidance as to how can I build a php form which includes both text and an image uploader. 大家好,我正在寻找有关如何构建包含文本和图像上传器的php表单的指导。 I am able to do the two forms separately but am having a bit of difficulty in joining things together. 我能够分别完成这两种形式,但是在将事物连接在一起时有点困难。

My html form: 我的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>

Code to insert text 插入文字的代码

if (isset($_POST['add_news_btn'])) {
    $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);
    include 'connect.php';

    $stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory) VALUES(?, ?, ?, ?)");
    $stmt->bind_param('ssss', $newsdate, $newstitle, $newstext, $newscatagory);
    $stmt->execute();
    $stmt->close();
    echo "done";
}

code to upload image 代码上传图片

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();
}

try to change this statement 尝试更改此声明

$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);

Try posting into this php 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();
}

Note : To avoid duplicate file insertion try changing the filename to something unique before saving it. 注意 :为避免重复文件插入,请在保存前尝试将文件名更改为唯一的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM