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

时间:2015-07-02 16:16:46

标签: php mysql

大家好我正在寻找一些指导,以便我如何构建包含文本和图像上传器的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($_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";
}

上传图片的代码

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

2 个答案:

答案 0 :(得分:0)

尝试更改此声明

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

答案 1 :(得分:0)

尝试发帖到这个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();
}

注意:为避免重复插入文件,请尝试在保存之前将文件名更改为唯一文件。