Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php
if(isset($_POST['submit'])){
    $post_name=mysqli_real_escape_string($_POST['post_name']);
     $post_image=mysqli_real_escape_string($_FILES['image']['name']);
    $post_image_temp=mysqli_real_escape_string($_FILES['image']['tmp_name']);
    $post_desc=mysqli_real_escape_string($_POST['post_desc']);
    
          move_uploaded_file($post_image_temp,"/images/$post_image");
    
    $query ="INSERT INTO post(post_name,post_image,post_desc) ";
    $query .= "VALUES('".$post_name."','".$post_image."','".$post_desc."')";
    
    $result=mysqli_query($connection,$query);
    if(!$result){
           die("QUERY FAILED.".mysqli_error());
       }
   
}
?>


What I have tried:

php mysql insert not working and no error
Posted
Updated 16-May-18 10:04am
Comments
Bryian Tan 16-May-18 21:01pm    
How can you tell if the Insert get executed? By the way, what this line doing? move_uploaded_file? Try comment that out

PHP
$query ="INSERT INTO post(post_name,post_image,post_desc) ";
$query .= "VALUES('".$post_name."','".$post_image."','".$post_desc."')";

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
 
Have you (1) looked at your Query string to see if it's what you think it is?
Have you (2) run the string to see if it returns any records. If it returns no data, it is NOT an error (from the MySQL's point of view).

If the above are both working, I would look at the methods you use to display the data (if any).
 
Share this answer
 
Comments
aman fanan 16-May-18 12:02pm    
code is correct but they are not inserting the data.why?
W Balboos, GHB 16-May-18 12:05pm    
I asked two questions:
1 - Does it look the way you expect it to look?
2 - If you run (the result of 1) it directly on you MySQL server, does it work?

If it works than you need to check you connection string.
If it doesn't work when used directly - then it would not work through php, either. You need to fix it.

But first, make sure it works without php. If necessary, post the result of '1'.


If it
aman fanan 16-May-18 12:13pm    
1. yes it look the way i expect
2.it is running on mysql server
W Balboos, GHB 16-May-18 12:17pm    
So, since you say it looks the way you think it should and it works when run directly, you need to check your php connection.
$result=mysqli_query($connection,$query);

And I don't know you parameters, password, or system, so that's something you'll need to work out yourself.

ONE MORE THING: Does your code even get as far as the MySQL call? Check that, too.
aman fanan 16-May-18 12:52pm    
seriously men i m not getting the answer this is so frustating
help me

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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