Quantcast
Viewing all articles
Browse latest Browse all 3

PHP mysql insert post data

This PHP mysql insert post data is written on Website Developer

For PHP mysql insert post data, The page dedicated to the inclusion of the post is the core of the application blog engine, can write articles that will be published on the website and that possibly will be commented by users who frequent the blog.

It is a private page, in fact it can access only authenticated users through the login procedure described previously, translating technical what we just said, to gain access to the page will post writing must be an active session, otherwise the browser will be redirected to the first page of a front-end server.

Here is the code needed for the submissions of articles:

<?php
//session initialization
session_start();
//control session value
if(!isset($_SESSION['login'])) {
    //redirect to homepage when login failure
    header("Location: index.php");
}
 
//value of variables with the parameters from the form
if(isset($_POST['submit']) && ($_POST['submit'] == "Post")) {
 
    if(isset($_POST['author'])) {
        $author = addslashes(filter_var($_POST['author'], FILTER_SANITIZE_STRING));
    }
    if(isset($_POST['title'])) {
        $title = addslashes(filter_var($_POST['title'], FILTER_SANITIZE_STRING));
    }
    if(isset($_POST['text'])) {
        $text = addslashes(filter_var($_POST['text'], FILTER_SANITIZE_STRING));
    }
 
    //the class file include
    include "function_mysql.php";
    //instance of the class
    $data = new MysqlClass();
    //connection function
    $data->Connect();
 
    $t = "post"; # table name
    $v = array($title, $text, $author, date("Y-m-d")); # Insert values
    $r = "title_post, text_post, author_post, data_post"; # fields to populate
    //calling the function for data entry
    $data->insert($t, $v, $r);
    echo "Article posted successfully.";
    //disconnect
    $data->disconnect();
} else {
//form for inserting
    ?>
    Insert <h1> post: </h1>
    <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post" >
        Title: <br>
        <input name = "title" type = "text" ><br/>
        Text: <br>
        <textarea name = "text" cols = "30" rows = "10" > </textarea> <br/>
        Author: <br>
        <input name = "author" type = "text" ><br/>
        <input name = "submit" type = "submit" value = "Post" >
    </form>
    <?
}
?>

 

Image may be NSFW.
Clik here to view.
The mechanism that allows the publication of the post is very simple:

  • If you have sent the input parameters (title, text, and author of the post) phase is launched by entering values in the table, otherwise displays the form needed for article writing;
  • The data sent are transformed into variables that contain the values to insert into the table;
  • In addition to the fields title, author and text you must also populate the field relative to the date of writing, the latter is characterized by the type of given DATE that provides a format for dates “year-month-day”, and then to create the value to be inserted is called the function date() in PHP that records the current date based on the parameters passed as an argument, in this case are passed as parameter “Y” (year in four digits)”m” (month number preceded by “0″ if formed by a single digit, for example “09″ for September) and “d” (day of the month, preceded by “0″ if formed by a single digit, for example “01″ for the first day of the month).
  • The variables relating to the parameters sent are passed to the function Inserisci() for the population of the table, after performing the INSERT query close the connection to the DBMS in order to free system resources

Viewing all articles
Browse latest Browse all 3

Trending Articles