PDO

时间:2017-12-14 01:25:48

标签: php jquery mysql pdo autosave

我正在尝试在PHPPDO创建自动保存表单。一切正常,但在完成后现在无法正常工作。
P.S:我在这个POST的帮助下创建了我的脚本:{Click to see}
HTML

<form method="POST" action="ePHP/addNewStory.php">
            <input type="text" name="storyDate" value="<?php echo date("j-n-Y"); ?>" placeholder="<?php echo date("j-n-Y"); ?>"/>
            <input type="file" name="storyImage" id="input-file-now image"  class="dropify" accept='image/*'/>
            <input type="text" name="storyImageTitle"/>
            <input type="checkbox" name="storyComments" checked class="js-switch col-lg-6" data-color="#86c34a"  />                            
            <button type="submit" class="btn btn-success">Publish</button>
            <button type="reset" class="btn btn-inverse model_img" id="sa-warning">Cancel</button>
            <input type="text" name="storyPublisher" hidden value="1">
            <input type="text" name="storyTitle" >
            <input type="text" name="storyExcerpt">
            <textarea name="editor1" class="Detail"></textarea>
            <input type="text" name="storyAuthor"/>
            <select  name="storyCategory[]" class="select2 select2-multiple" multiple="multiple">
                <optgroup label="Home">
                    <option value="AK">Alaska</option>
                    <option value="HI">Hawaii</option>
                </optgroup>
                <optgroup label="Pacific Time Zone">
                    <option value="CA">California</option>
                    <option value="NV">Nevada</option>
                    <option value="OR">Oregon</option>
                    <option value="WA">Washington</option>
                </optgroup>
            </select>
        </form>

JS

<script>  
$(function () {
    $.post("ePHP/addNewStory.php", function (data) {
        $("[name='storyPublisher']").val(data.storyPublisher);
        $("[name='storyTitle']").val(data.storyTitle);
        $("[name='storyExcerpt']").val(data.storyExcerpt);
        $("[name='editor1']").val(data.editor1);
        $("[name='storyCategory[]']").val(data.storyCategory);
        $("[name='storyDate']").val(data.storyDate);
        $("[name='storyImageTitle']").val(data.storyImageTitle);
    }, "json");
    setInterval(function () {
        $.post("ePHP/addNewStory.php", $("form").serialize());
    }, 2000);
}); 
</script>

PHP (addNewStory.php)

<?php
session_start();
try {
$dc = new PDO("mysql:host=localhost;dbname=UPmain", 'root', 'root');
$dc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$storyTitle=$_POST['storyTitle'];
$storyExcerpt=$_POST['storyExcerpt'];
$editor1=$_POST['editor1'];
$storyCategory=$_POST['storyCategory'];
$storyDate=$_POST['storyDate'];
$storyImageTitle=$_POST['storyImageTitle'];
$storyCategory=implode(',', $storyCategory);
$storyPublisher=1;  


$stmt = $dc->query("SELECT * FROM storyAutoSave WHERE storyPublisher='$storyPublisher'");
$return_count = $stmt->rowCount();
if($return_count > 0){     

if(isset($storyTitle)){
    $update_qry = $dc->prepare("UPDATE storyAutoSave SET storyTitle=:storyTitle, storyExcerpt=:storyExcerpt, storyDetail=:editor1, storyCategory=:storyCategory, storyDate=:storyDate, storyImageTitle=:storyImageTitle WHERE storyPublisher='$storyPublisher'");
    $update_qry -> bindValue(':storyTitle', $storyTitle, PDO::PARAM_STR);
    $update_qry -> bindValue(':storyExcerpt', $storyExcerpt, PDO::PARAM_STR);
    $update_qry -> bindValue(':editor1', $editor1, PDO::PARAM_STR);
    $update_qry -> bindValue(':storyCategory', $storyCategory, PDO::PARAM_STR);
    $update_qry -> bindValue(':storyDate', $storyDate, PDO::PARAM_STR);
    $update_qry -> bindValue(':storyImageTitle', $storyImageTitle, PDO::PARAM_STR);
    $update_qry -> execute(); 
     } else {
    $get_autosave = $dc->prepare("SELECT * FROM storyAutoSave WHERE storyPublisher='$storyPublisher'");
    $get_autosave->execute();
    while ($gt_v = $get_autosave->fetch(PDO::FETCH_ASSOC)) {
        $storyTitle=$gt_v['storyTitle'];
        $storyExcerpt=$gt_v['storyExcerpt']; 
        $storyDetail=$gt_v['storyDetail'];
        $storyCategory=$gt_v['storyCategory'];
        $storyDate=$gt_v['storyDate'];
        $storyImageTitle=$gt_v['storyImageTitle'];
        echo json_encode(array('storyTitle' => $storyTitle, 'storyExcerpt' => $storyExcerpt, 'editor1' => $storyDetail, 'storyCategory' => $storyCategory, 'storyDate' => $storyDate, 'storyImageTitle' => $storyImageTitle));
    }           
}
} else { 
$insert_qry = $dc->prepare("INSERT INTO storyAutoSave (storyPublisher, storyTitle, storyExcerpt, storyDetail, storyCategory, storyDate, storyImageTitle) VALUES (storyPublisher=:storyPublisher, storyTitle=:storyTitle, storyExcerpt=:storyExcerpt, storyDetail=:editor1, storyCategory=:storyCategory, storyDate=:storyDate, storyImageTitle=:storyImageTitle)");
$insert_qry -> bindValue(':storyTitle', $storyTitle, PDO::PARAM_STR);
$insert_qry -> bindValue(':storyExcerpt', $storyExcerpt, PDO::PARAM_STR);
$insert_qry -> bindValue(':editor1', $editor1, PDO::PARAM_STR);
$insert_qry -> bindValue(':storyCategory', $storyCategory, PDO::PARAM_STR);
$insert_qry -> bindValue(':storyDate', $storyDate, PDO::PARAM_STR);
$insert_qry -> bindValue(':storyImageTitle', $storyImageTitle, PDO::PARAM_STR);
$insert_qry -> bindValue(':storyPublisher', $storyPublisher, PDO::PARAM_STR);
$insert_qry->execute();  
}
} catch(PDOException $e) {
echo $e->getMessage();
}
?>

我也试过这个:

$insert_qry = $dc->prepare("INSERT INTO storyAutoSave (storyPublisher, storyTitle, storyExcerpt, storyDetail, storyCategory, storyDate, storyImageTitle) VALUES (?, ?, ?, ?, ?, ?, ?)");
$insert_qry->bindParam(1, $storyPublisher);
$insert_qry->bindParam(2, $storyTitle, PDO::PARAM_STR);
$insert_qry->bindParam(3, $storyExcerpt, PDO::PARAM_STR);
$insert_qry->bindParam(4, $editor1, PDO::PARAM_STR);
$insert_qry->bindParam(5, $storyCategory, PDO::PARAM_STR);
$insert_qry->bindParam(6, $storyDate, PDO::PARAM_STR);
$insert_qry->bindParam(7, $storyImageTitle, PDO::PARAM_STR);
$insert_qry->execute();  

addNewStory.php给了我这个错误:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'storyTitle' cannot be null

当我在数据库中的“storyPublisher”列中插入值时,一切都开始工作

0 个答案:

没有答案