Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i'm trying to insert data into database using php and mysql but my query isn't running. i have to insert data into comments table using php and mysql here is my code. if there is any errors please tell me.

PHP
<pre>if(isset($_POST['submit-comment']))
                {
                    $cs_name = $_POST['full-name'];
                    $cs_email = $_POST['email'];
                    $cs_website = $_POST['website'];
                    $cs_comment = $_POST['comment'];
                    $cs_date = time();
                    if(empty($cs_name) or empty($cs_email) or empty($cs_comment))
                    {
                        echo "<script type='text/javascript'>swal({   title: 'Opps!',   text: 'You Have To Fill All (*) Fields!',   imageUrl: 'images/info.png' });</script>";
                    }
                    else
                    {
                        $cs_query = "INSERT INTO `comments`(`id`, `date`, `name`, `username`, `post_id`, `email`, `website`, `image`, `comment`, `status`) VALUES (NULL,$cs_date,$cs_name,user,$post_id,$cs_email,$cs_website,NULL,$cs_comment,pending)";
                        $cs_run = mysql_query($cs_query);
                        if($cs_run)
                        {
                            echo "<script type='text/javascript>setTimeout(function () { swal('Good job!', 'Your Commnet Has Been Submited Waiting For Approvel!', 'success')}, 100);</script>";
                        }
                        else
                        {
                            echo "<script type='text/javascript'>swal({   title: 'Opps!',   text: 'Your Commnet Not has Been Submited!',   imageUrl: 'images/delete.png' });</script>";
                        }
                    }
                }
              ?>


What I have tried:

i have tried but not get the answer i want.
Posted
Updated 7-Sep-17 4:33am
Comments
omerkamran 7-Sep-17 10:20am    
I dont see any connection made to DB in your code. Make sure you are connecting your DB before performing any SQL Operation.

1 solution

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900