Jump to content
Larry Ullman's Book Forums

Strange Problem Of Inserting Data Into Mysql Table.


Recommended Posts

Hi, I get a strange problem when I try to insert images data into media table. I decided to break down the problems section by sections. I rewrite the query using basic mysqli_query() function and insert one row at a time for testing purpose. Here is the strange thing, when I pass the value as variable into the VALUES, the insertion will fail and return an error: 'insertion failedUnknown column 'edvard' in 'field list''. However, If I just pass the string into VALUES(), the insertion goes without problem. I haven't run into this sort of problem before, can anyone explain the reason please. Thanks again!

 

Failed Code:

 

 

            $file_title = 'edvard-much-the-scream';
            $file_mime = '.jpg';
            $product_type = 'product';
            $file_url = 'edvard-much-the-scream.jpg';
            $fipi = 1;
 
 
    $query = "INSERT INTO media (file_title) VALUES ($file_title)"; // this fails 
    mysqli_query($dbc, $query);
    if(mysqli_affected_rows($dbc)==1){
        $iid = mysqli_insert_id($dbc);
        echo $iid;
    } else {
        echo 'insertion failed' . mysqli_error($dbc);
    }
 
Success Code:
 
            $file_title = 'edvard-much-the-scream';
            $file_mime = '.jpg';
            $product_type = 'product';
            $file_url = 'edvard-much-the-scream.jpg';
            $fipi = 1;
 
 
    $query = "INSERT INTO media (file_url) 
              VALUES ('edvard-much-the-scream')"; // this works
 
    mysqli_query($dbc, $query);
    if(mysqli_affected_rows($dbc)==1){
        $iid = mysqli_insert_id($dbc);
        echo $iid;
    } else {
        echo 'insertion failed' . mysqli_error($dbc);
    }
 
 

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...