Jump to content

Insert image name to database problem


zazu
Go to solution Solved by benanamen,

Recommended Posts

Hi there guys,

I've a little problem with inserting a file name into a database table. I can't see wich is the problem. The code is bellow and i think the problem is at INSERT INTO part.

    <?php
  $path = "./cv/";

$valid_formats = array("doc", "pdf");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
  $name = $_FILES['photoimg']['name'];
  $size = $_FILES['photoimg']['size'];
  if(strlen($name))
  {
    list($txt, $ext) = explode(".", $name);
    if(in_array($ext,$valid_formats))
    {
      if($size<(20480*20480)) // Image size max 20 MB
      {
        $actual_image_name = time().$id.".".$ext;
        $tmp = $_FILES['photoimg']['tmp_name'];
        if(move_uploaded_file($tmp, $path.$actual_image_name))
        {
          mysqli_query($mysqli,"INSERT INTO formular_client (client_cv = '$actual_image_name')");
        }
        else
          echo "failed";
      } 
      else
        echo "Image file size max 20 MB"; 
    }
    else
      echo "Invalid file format.."; 
  }
}
?>
<input type="file" name="photoimg" id="photoimg" />
Edited by zazu
Link to comment
Share on other sites

 

Hi there guys,

I've a little problem with inserting a file name into a database table. I can't see wich is the problem. The code is bellow and i think the problem is at INSERT INTO part.

    <?php
  $path = "./cv/";

$valid_formats = array("doc", "pdf");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
  $name = $_FILES['photoimg']['name'];
  $size = $_FILES['photoimg']['size'];
  if(strlen($name))
  {
    list($txt, $ext) = explode(".", $name);
    if(in_array($ext,$valid_formats))
    {
      if($size<(20480*20480)) // Image size max 20 MB
      {
        $actual_image_name = time().$id.".".$ext;
        $tmp = $_FILES['photoimg']['tmp_name'];
        if(move_uploaded_file($tmp, $path.$actual_image_name))
        {
          mysqli_query($mysqli,"INSERT INTO formular_client (client_cv = '$actual_image_name')");
        }
        else
          echo "failed";
      } 
      else
        echo "Image file size max 20 MB"; 
    }
    else
      echo "Invalid file format.."; 
  }
}
?>
<input type="file" name="photoimg" id="photoimg" />

Check my topic

 

I had similar situation regarding Mime checking I think You should use mime types as well and not explode to get file ext.

 

 

I'm not an expert with mysqli but try 

mysqli_query($mysqli,"INSERT INTO `formular_client` (`client_cv`) = (`$actual_image_name`)");
Link to comment
Share on other sites

 

The syntax is

INSERT INTO `formular_client` (`client_cv`) VALUES ('$actual_image_name')

or

INSERT INTO `formular_client` SET `client_cv` = '$actual_image_name'

 

Doesn't work, and i don't know why because the code seems to be correct. :(

Link to comment
Share on other sites

You need to be more specific than "doesn't work".

 

When i execute the script the file i choose is sent to the specified folder, but the file name is not stored to the database! To be more specific the INSERT function doesn't work well.

Link to comment
Share on other sites

you must ALWAYS check for and handle query errors. there are multiple possible reasons that any query can fail.

 

for some quick debugging, just echo mysqli_error($mysqli); on the next line after your mysqli_query() statement, assuming you have a valid database connection in $mysqli.

 

do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects?

Link to comment
Share on other sites

 

Have you checked to see if there is any data in $actual_image_name?

 

In the following line you have $id, but I dont see it anwhere in your code gettting a value.

$actual_image_name = time().$id.".".$ext;   

 

Ok mate! Thanks for help i've manage to solve the problem. The working code is listed bellow:

mysqli_query($mysqli,"UPDATE evaluatori_users SET client_cv='$actual_image_name' WHERE id='$id'");
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.