Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8161343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:28:46+00:00 2026-06-06T18:28:46+00:00

I have an old mysql code where it successfully inserts the name of an

  • 0

I have an old mysql code where it successfully inserts the name of an image file into the database. But since old mysql is being decapitated, I have attempted doing the same in mysqli (can’t use PDO because of version of PHP I have). The problem is that I cannot get the image file name to be inserted into the database in mysqli. What am I doing wrong?

Below is the mysqli code:

    <?php

    session_start();


    $username="xxx";
    $password="xxx";
    $database="mobile_app";

      $mysqli = new mysqli("localhost", $username, $password, $database);

      /* check connection */
      if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        die();
      }

    $result = 0;

if(getimagesize($_FILES['fileImage']['tmp_name'])){

    if( is_file("ImageFiles/".$_FILES['fileImage']['name'])) {
        $parts = explode(".",$_FILES['fileImage']['name']);
        $ext = array_pop($parts);
        $base = implode(".",$parts);
        $n = 2;

        while( is_file("ImageFiles/".$base."_".$n.".".$ext)) $n++;
        $_FILES['fileImage']['name'] = $base."_".$n.".".$ext;

        move_uploaded_file($_FILES["fileImage"]["tmp_name"],
        "ImageFiles/" . $_FILES["fileImage"]["name"]);
        $result = 1;

        $imagesql = "INSERT INTO Image (ImageFile) 
        VALUES (?)";

                if (!$insert = $mysqli->prepare($imagesql)) {
          // Handle errors with prepare operation here
        }

        $insert->bind_param("s",'ImageFiles/' . $_FILES['fileImage']['name']);

     $insert->execute();

            if ($insert->errno) {
              // Handle query error here
            }

            $insert->close();


    }
        else
          {
          move_uploaded_file($_FILES["fileImage"]["tmp_name"],
          "ImageFiles/" . $_FILES["fileImage"]["name"]);
          $result = 1;

        $imagesql = "INSERT INTO Image (ImageFile) 
        VALUES (?)";

                if (!$insert = $mysqli->prepare($imagesql)) {
          // Handle errors with prepare operation here
        }

        $insert->bind_param("s",'ImageFiles/' . $_FILES['fileImage']['name']);

     $insert->execute();

            if ($insert->errno) {
              // Handle query error here
            }

            $insert->close();


          }
    }
    ?>

Below is the old mysql code where the insert did work:

    <?php

    session_start();


    $username="xxx";
    $password="xxx";
    $database="mobile_app";

    mysql_connect('localhost',$username,$password);

    mysql_select_db($database) or die( "Unable to select database");

    $result = 0;

if(getimagesize($_FILES['fileImage']['tmp_name'])){

    if( is_file("ImageFiles/".$_FILES['fileImage']['name'])) {
        $parts = explode(".",$_FILES['fileImage']['name']);
        $ext = array_pop($parts);
        $base = implode(".",$parts);
        $n = 2;

        while( is_file("ImageFiles/".$base."_".$n.".".$ext)) $n++;
        $_FILES['fileImage']['name'] = $base."_".$n.".".$ext;

        move_uploaded_file($_FILES["fileImage"]["tmp_name"],
        "ImageFiles/" . $_FILES["fileImage"]["name"]);
        $result = 1;

        $imagesql = "INSERT INTO Image (ImageFile) 
        VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";

        mysql_query($imagesql);

    }
        else
          {
          move_uploaded_file($_FILES["fileImage"]["tmp_name"],
          "ImageFiles/" . $_FILES["fileImage"]["name"]);
          $result = 1;

            $imagesql = "INSERT INTO Image (ImageFile) 
            VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";

    mysql_query($imagesql);

          }

}
          mysql_close();

    ?>

      <script language="javascript" type="text/javascript">
      window.top.stopImageUpload(<?php echo $result ? 'true' : 'false'; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');
      </script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:28:47+00:00Added an answer on June 6, 2026 at 6:28 pm

    Try this, assign the value to a variable first and then bind it, dont build the data within the bind_param:

        <?php 
        $imagesql = "INSERT 
                      INTO Image (ImageFile)
                      VALUES (?)";
    
        $insert = $mysqli->prepare($imagesql);
    
        //Dont pass data directly to bind_param store it in a variable
        $insert->bind_param("s",$img);
    
        //Assign the variable
        $img = 'ImageFiles/'.$_FILES['fileImage']['name'];
    
        $insert->execute();
        ?>
    

    Also you should check if the file is uploaded and if any errors have occurred with the upload before doing any db stuff:

    <?php 
    //If POST
    if($_SERVER['REQUEST_METHOD']=='POST'){
        //Check no errors on upload
        if($_FILES['fileImage']['error']==0){
    
            if(isset($_FILES['fileImage']['tmp_name'])){
                $imgSize = getimagesize($_FILES['fileImage']['tmp_name']);
                ...
                ...
            }
    
        }else{
            //get error code and display error
        }
    
    }else{
        //No POST
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an old mysql code which I want to convert into PHP data
I'm translating an old database SyBase to MySQL and I have this DDL Query:
I'm migrating an old Sybase database to MySQL and I have to create foreign
We currently have a 10 year old nasty, spaghetti-code-style SQL Server database that we
I have a sql file that creates a database in mysql: SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
Possible Duplicate: How to successfully rewrite old mysql-php code with deprecated mysql_* functions? I
I have this old-style non-PDO MySQL query (the code isn't tightened up, just to
I have an old mysql code which worked correctly in the application: $query =
I currently have a news website setup in PHP/MYSQL that's a bit old and
I have old code that uses size_t which IIRC comes from cstring.h. On OS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.