Click here to Skip to main content
14,734,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,
I am new to PHP and sqli, i write this code to insert data into my database.

Connection establish successful but record are not able to insert.
Kindly help to find error in the code.

What I have tried:

if (isset($_POST['submit'])){
	if($title == '' or $name==''){
		echo " alert('All Field Required')";
	if($image_type == "image/jpeg" or $image_type == "image/png"
	or $image_type == "image/gif"){
			echo " alert ('Image size is larger')";
}else {
	echo "alert ('Image type is invalid')";
	$query = "INSERT INTO testimonial (post_title, post_date, post_author, post_image, post_content, post_country, post_phno, post_email) 
	valuse ('$title','$date','$name','$image','$content','$country','$contactno','$email')";
	$run = mysqli_query($con, $query);
		echo " alert ('Record Added') ";
		header ('refresh:3; url: blog.php');
		echo " alert ('Data Not Sent') ";
		header ('refresh:3; location: localhost/insert_post.php');
Updated 2-Jul-18 6:21am
Richard MacCutchan 28-Jun-18 8:03am
What happens when you run your code?

$query = "INSERT INTO testimonial (post_title, post_date, post_author, post_image, post_content, post_country, post_phno, post_email)
valuse ('$title','$date','$name','$image','$content','$country','$contactno','$email')";

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[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
Member 13890780 28-Jun-18 7:38am
thanks for your advice, I knew it and later will secure it but in initially i am stuck to insert data into database.
Richard Deeming 28-Jun-18 12:29pm
"I'll secure it later" is a bad idea. Not only is there a good chance that you won't have time to secure it properly, or that you'll miss something; you're also going to be battling to get the concatenated SQL queries in just the right format to work, which would be a non-issue if you were using parameters.
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('aaa', 'bbb', '')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;


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