0

/i want to insert data with the form and written simple code for it. Html part is working well.post table is created in database but this code unable to insert data in database. Please help me/ Inserting new posts

    </head>
<body>
    <form method="post" action="insert_post_1.php"        enctype="multipart/form-data">
    <table width="600" align="center" border="10">
        <tr>
            <td align="center" bgcolor="yellow" colspan="6">
                <h1>Insert New Post Here</h1>
            </td>
        </tr>

        <tr>
            <td align="right">Post Title:</td>
            <td><input type="text" name="title" size="30"></td>
        </tr>
        <tr>
            <td align="right">Post Author:</td>
            <td><input type="text" name="author" size="30"></td>
        </tr>
        <tr>
            <td align="right">Post Keywords:</td>
            <td><input type="text" name="keywords" size="30"></td>
        </tr>
        <tr>
            <td align="right">Post Image:</td>
            <td><input type="file" name="image" size="30"></td>
        </tr>
        <tr>
            <td align="right">Post Content:</td>
            <td><textarea name="content" cols="30" rows="25"></textarea></td>
        </tr>
        <tr>
            <td align="center" colspan="6"><input type="submit" name="submit" value="Publish Now"></td>
        </tr>

    </table>
    </form>
</body>
</html>

/html has no issue it works perfectly fine but with this php code i am unable to detect the problem. while clicking Publish button it is not inserting data to database/

    if($post_title == '' or $post_keywords == '' or $post_author == '' or $post_content == ''){
    echo "<script>alert('Any of the fields is empty')</script>";
    exit();
    }
    else{
    move_uploaded_file($post_temp, "images/$post_image");
/*this part of code is not running and hence unable to insert data to database*/    
    $insert_query = "INSERT INTO "
            . "posts (`post_id`, `post_title`, `post_date`, `post_author`, `post_image`, `post_keywords`, `post_content`)"
            . " VALUES (NULL, $post_title, $post_date, $post_author, $post_image, $post_keywords, $post_content)";

    if(mysql_query($insert_query)){
        echo "<center><h1>Post Published Successfully</h1></center>";
    }
    }
}
?>
sunilds
  • 1
  • 1
  • try to add a else statement to check whether query runs or not... – Ghostman Mar 06 '15 at 17:11
  • 1
    You are passing in several variables assumed to be strings, which are not single-quoted. See [When to use single quotes, double quotes, backticks](http://stackoverflow.com/a/11321508/541091) for examples on how to correctly quote these. However, it is vulnerable to SQL injection in this form, and that is best handled by adopting either PDO or MySQLi with `prepare()/execute()` instead of `mysql_connect()/mysql_query()`. – Michael Berkowski Mar 06 '15 at 17:18

2 Answers2

0

If you still want to use the mysql extension, comment this block

if(mysql_query($insert_query)){
    echo "<center><h1>Post Published Successfully</h1></center>";
}

and add this below

mysql_query($insert_query) or die(mysql_error());
echo "<center><h1>Post Published Successfully</h1></center>";

in order to see what's the error

Sledge Hammer
  • 196
  • 2
  • 11
  • error is Connected successfully Access denied for user 'www-data'@'localhost' (using password: NO) how to correct – sunilds Mar 07 '15 at 02:58
  • Most likely there's something wrong with the database credentials you've supplied (or forgot to) in the piece of code that makes the connection to MySQL. – Sledge Hammer Mar 07 '15 at 03:24
-1

First of all you need to look in to THIS documentation:

mysql_query is depricated. Use PDO or mysqli.

to check error in mysql:

try{
   // Run your query:
}
catch(Exception $e){
   echo $e->getMessage();
}
RNK
  • 5,582
  • 11
  • 65
  • 133
  • `mysql_query()`, despite being deprecated, will not throw an exception. You are essentially telling the OP to rewrite all the code to PDO, then check for errors. – Michael Berkowski Mar 06 '15 at 17:16
  • I am encourage OP not to use deprecated functions. Anyways, somebody will find this post useful. Thanks for down voting though.. – RNK Mar 06 '15 at 17:19