Click here to Skip to main content
13,792,957 members
Rate this:
 
Please Sign up or sign in to 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 16-May-18 6:45am
Updated 16-May-18 11: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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

$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[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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).
  Permalink  
Comments
aman fanan 16-May-18 12:02pm
   
code is correct but they are not inserting the data.why?
W Balboos 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 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
W Balboos 16-May-18 13:14pm
   
"Seriously" - from the small patch of code you include with your question and all the missing information (like how and where do you build $connection), that's all the information that I can offer.

Several different tests you can do. That's how you debug a problem. Break it down until you see the bit that's not working.

Try using a debugger to see where your code goes wrong.. If you don't have one and plan to continue writing code, you should get hold of one.
aman fanan 16-May-18 12:14pm
   
with php is not running

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


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.181207.3 | Last Updated 16 May 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100