1

I have a question about PHP arrays and inserting them as single records into a MySQL database. I have the array sorted and that is working as it should.

This is what I have for the array:

$files = array();
            foreach($_FILES['file']['tmp_name'] as $key => $tmp_name )
            {
                $files[$key] = array
                (
                    $file_name = $_FILES['file']['name'][$key],
                    $file_type=$_FILES['file']['type'][$key],
                    $file_size =$_FILES['file']['size'][$key],
                    $file_tmp =$_FILES['file']['tmp_name'][$key]                        
                );
            }

This is what I have for the array to insert them as separate rows in to the database:

$new = array();
            foreach($files as $key => $value)
            {
                $new[] = "'".implode("','", $value)."'";
            }
            $query = "(".implode("), (",$new).")";
            $sqlone = "INSERT INTO files (filename, filetype, filesize, filetempname) VALUES ".$query."";
            if (!mysql_query($sqlone, $conn))
            {
                die("Error: " . mysql_error().".");
            }

The issue I am running into is this: I want to add extra information to the query but I am not entirely sure how to do this.

I want to be able to add a reference to the email that the files were attached to. I basically want the query to be as follows:

$sqlone = "INSERT INTO files (filename, filetype, filesize, filetempname, mailid //this is the extra column in the database) VALUES ".$query.", '1'// this is the corresponding value";

The issue I am running in to is that I get an error when trying to add extra information to it.

Are there any pointers you guys could give me?

Thanks in advance

6
  • 1
    It looks correct. What error are you getting? I'd really suggest getting away from the mysql_* functions as they're deprecated and won't work in future releases. Check into PDO or MySQLi instead. Commented Jun 12, 2013 at 19:09
  • @aynber This is the error I am getting: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1'' at line 1. I will look into using mysqli, I am just trying to get this working for now Commented Jun 12, 2013 at 19:12
  • 1
    Echo out $sqlone before you run the mysql_query to make sure it looks correct. Commented Jun 12, 2013 at 19:13
  • @aynber, I have figured out that it is not adding the mailid value to the query in the right place. I have now added it to $query = "(".implode("), (",$new).", '1' //added here)"; and it now only adds it to the last item in the array. Any ideas why this might be? Commented Jun 12, 2013 at 19:18
  • You're adding it to the very end of the query. Put it in the new[] array inside of the foreach instead. Commented Jun 12, 2013 at 19:20

1 Answer 1

1

Just change where your parenthesis is added (and quote your inputs):

$query = "('".implode("'), ('",$new);
$sqlone = "INSERT INTO files (filename, filetype, filesize, filetempname, mailid) VALUES ".$query."', '1')";

Should result in the SQL:

INSERT INTO files (filename, filetype, filesize, filetempname, mailid) VALUES
('<file_name>', '<file_type>', '<file_size>', '<file_tmp_name>', '1')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.