Tardz Bert Tardz Bert - 7 months ago 13
SQL Question

Need help, cannot save my data in db I use query and the server side I use php oop

I use jquery saving my data in db but the problem of my code is when i click the submit button the page was loading and it back to the main page and also can't save the data in db.. i can't figure it out what was the problem of my code..

here is my code:

<?php
session_start();
include_once '../../include/CsppoAdmin.php';
$csppoAdmin = new CsppoAdmin();
$msg = '';

if (isset($_REQUEST['submit'])){
extract($_REQUEST);
$register = $csppoAdmin->add_news_release($date_story,$writer,$headline,$source,$story,$keywords);
if ($register) {
$msg = '<p style="color:green">New lesson was successful!</p>';
} else {
$msg = '<p style="color:green">Failed. Lesson inputted already exits please try again.</p>';
}

}
?>
<script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script>
<script>tinymce.init({selector:'textarea'});</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$("#submit").click(function(e) {
e.preventDefault();
var date_story = $("#date_story").val();
var writer = $("#writer").val();
var headline = $("#headline").val();
var source = $("#source").val();
var story = $("#story").val();
var keywords = $("#keywords").val();
var dataString = 'date_story='+date_story+'&writer='+writer+'&headline='+headline+'&source='+source+'&story='+story+'&keywords='+keywords;
if(date_story=='' || source=='' || story=='' || keywords=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "dataentry/add_nr.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
});
</script>
<form role="form" action="" method="post">
<div class="col-lg-6">
<div class="form-group">
<label>Date of story</label>
<input type="date" name="date_story" id="date_story" class="form-control" placeholder="Enter date">
</div>
<div class="form-group">
<label>Writer/Author</label>
<input type="text" name="writer" id="writer" class="form-control" placeholder="Enter Name">
</div>
<div class="form-group">
<label>Headline</label>
<input type="text" name="headline" id="headline" class="form-control" placeholder="Enter headline">
</div>
<div class="form-group">
<label>Source</label>
<input type="text" name="source" id="source" class="form-control" placeholder="Enter source">
</div>
</div>
<div class="col-lg-10">
<div class="form-group">
<label>Story</label>
<textarea name="story" id="story" class="form-control" rows="10"></textarea>
</div>
<div class="form-group">
<label>Keywords</label>
<input type="text" name="keywords" id="keywords" class="form-control" placeholder="Enter keywords separated by comma(,)">
</div>
<button name="submit" id="submit"class="btn btn-primary">Submit</button>
</div>
</form>


and here is my php oop where the data will save in db:

public function add_news_release($date_story,$writer,$headline,$source,$story,$keywords){
$query = "SELECT `news_id` FROM `news_release` WHERE `headline` = ?";
if($stmt = $this->db->prepare($query)){
$stmt->bind_param('s', $headline);
$stmt->execute();

$stmt->bind_result($id);
while ($stmt->fetch()) {
printf("%s\n", $id);
}
if ($id == 0){
$stmt = $this->db->prepare("INSERT INTO `news_release` (`date_story`,`writer`,`headline`,`source`,`story`,`keywords`,`post_date`,`poi_id`)
VALUES (?,?,?,?,?,?,?,?)");
$post_date = date('Y-m-d');
$poi_id = $_SESSION['poi_id'];
$stmt->bind_param('sssssssi', $date_story,$writer,$headline,$source,$story,$keywords,$post_date,$poi_id);
$stmt->execute();
return true;
}else {return false;}
$stmt->close();
}

mysqli_close($db);
$this->db = null;
}

Answer

The answer to my problem it should be like this:

jQuery:

<script src="js/jquery-1.11.2.js"></script>
<script type="text/javascript">
$("#submit").click(function() {
    $.post( $("#nrForm").attr("action"), $("#nrForm :input").serializeArray(), function(save){ $("#result").html(save); } );
    clearInput();
});

$("#nrForm").submit(function(){
    return false;
});

function clearInput(){
    $('input[type="text"], textarea').val('');
}
</script> 

my form:

<form id="nrForm" action="dataentry/save_nr.php" method="post">
                <div class="col-lg-6">
                    <div class="form-group">
                        <label>Date of story</label>
                        <input type="date" name="date_story" id="date_story" class="form-control"  placeholder="Enter date">
                    </div>
                    <div class="form-group">
                        <label>Writer/Author</label>
                        <input type="text" name="writer" id="writer" class="form-control"  placeholder="Enter Name">
                    </div>
                    <div class="form-group">
                        <label>Headline</label>
                        <input type="text" name="headline" id="headline" class="form-control"  placeholder="Enter headline">
                    </div>
                    <div class="form-group">        
                        <label>Source</label>
                        <input type="text" name="source" id="source" class="form-control"  placeholder="Enter source">
                    </div>
                </div>
                <div class="col-lg-10">
                    <div class="form-group">
                        <label>Story</label>
                        <textarea name="story" id="story" class="form-control" rows="10"></textarea>
                    </div>
                    <div class="form-group">        
                        <label>Keywords</label>
                        <input type="text" name="keywords" id="keywords" class="form-control"  placeholder="Enter keywords separated by comma(,)">
                    </div>
                    <button id="submit" name="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>

checking if it is inserted or not:

<?php
session_start();
include_once '../../include/CsppoAdmin.php';
$csppoAdmin = new CsppoAdmin();


    extract($_REQUEST);
    $register = $csppoAdmin->add_news_release($date_story,$writer,$headline,$source,$story,$keywords);
    if ($register) {
        echo '<p style="color:green">News release was successful!</p>';
    } else {
        echo '<p style="color:green">Failed. Inputted already exits please try again.  </p>';
    }
?>

and then the funtion that are insertin the data to the db:

public function add_news_release($date_story,$writer,$headline,$source,$story,$keywords){
            $query = "SELECT `news_id` FROM `news_release` WHERE `headline` = ?";
            if($stmt = $this->db->prepare($query)){
                $stmt->bind_param('s', $headline);
                $stmt->execute();

                $stmt->bind_result($id);
                while ($stmt->fetch()) {
                    printf("%s\n", $id);
                 } 
                if ($id == 0){
                    $stmt = $this->db->prepare("INSERT INTO `news_release` (`date_story`,`writer`,`headline`,`source`,`story`,`keywords`,`post_date`,`poi_id`)
                    VALUES (?,?,?,?,?,?,?,?)");
                    $post_date = date('Y-m-d');
                    $poi_id = $_SESSION['poi_id']; 
                    $stmt->bind_param('sssssssi', $date_story,$writer,$headline,$source,$story,$keywords,$post_date,$poi_id);
                    $stmt->execute();
                    return true;
                }else {return false;}
                $stmt->close();
            }           

            mysqli_close($db);
            $this->db = null;
    }
Comments