Click here to Skip to main content
14,696,432 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.
thanks

What I have tried:

<?php
include("connect.php");
if (isset($_POST['submit'])){
	$title=$_POST['p_title'];
	$date=date('d-m-y');
	$name=$_POST['p_name'];
	$country=$_POST['p_country'];
	$contactno=$_POST['p_contactno'];
	$email=$_POST['p_email'];
	$image=$_FILES['p_image']['name'];
	$image_type=$_FILES['p_image']['type'];
	$image_size=$_FILES['p_image']['size'];
	$image_tmp=$_FILES['p_image']['tmp_name'];
	$content=$_POST['p_content'];
	
	if($title == '' or $name==''){
		echo " alert('All Field Required')";
		exit();
	}
	if($image_type == "image/jpeg" or $image_type == "image/png"
	or $image_type == "image/gif"){
		if($image_size<=500000){
			move_uploaded_file($image_tmp,"images/$image_name");
		}else{
			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);
	if($run){
		echo " alert ('Record Added') ";
		header ('refresh:3; url: blog.php');
	}else{
		echo " alert ('Data Not Sent') ";
		header ('refresh:3; location: localhost/insert_post.php');
	}
}
?>
Posted
Updated 2-Jul-18 6:21am
v2
Comments
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[^]
   
Comments
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.
TRY THIS CODE
<?php
$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', 'abc@example.com')";

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

$conn->close();
?>
   

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