Jump to content

INSERTing Arrays into database


cdoggg94

Recommended Posts

Before I was having trouble uploading multiple files at once, and I have successfully done it now.

 

Now my issue is putting the names of those files in the column I want in the data base.

 

I think its something like this but i dont know how to display the correct name of the file.

 

<?php
set_time_limit(0);
require_once("Connections/myConnect.php");
mysql_select_db("phpmy1_norfolkartscentre_ca");
$preName = $_POST['name'];
$preText = $_POST['text'];
$preLink = $_POST['links'];


if(isset($_FILES['picture'])){
foreach($_FILES['picture']['tmp_name'] as $key => $tmp_name){
	move_uploaded_file($tmp_name, "data/{$_FILES['picture']['name'][$key]}");
}
//print_r($_FILES);
}

$querystring = "INSERT INTO presentation_tbl(pre_id,pre_name,pre_banner,pre_text,pre_link,pre_logo1,pre_logo2,pre_logo3) VALUES(NULL,'".$preName."','".$[0]."','".$preText."','".$preLink."','".$[1]."','".$[2]."','".$[3]."')";
$doquery = mysql_query($querystring);
echo "";
} else{
    echo "There was an error uploading the file, please try again!";
}

 

The ".$[1]."','".$[2]."','".$[3] part is where i want to display the specific names of the array contents.

 

I just dont know how to display it.

Link to comment
https://forums.phpfreaks.com/topic/263000-inserting-arrays-into-database/
Share on other sites

Specific names of what array?  The $_FILES array?  Please be more specific as to what values you're looking to derive if that's the case.

 

Also, and perhaps it's just because this is a work in progress, but that final ELSE statement will return an error:

 

} else{
    echo "There was an error uploading the file, please try again!";
}

 

as there is no opening IF for it.  Maybe you meant to place query within the:

 

if(isset($_FILES['picture'])){

 

block?  Then it should look like this:

 

<?php
set_time_limit(0);
require_once("Connections/myConnect.php");
mysql_select_db("phpmy1_norfolkartscentre_ca");
$preName = $_POST['name'];
$preText = $_POST['text'];
$preLink = $_POST['links'];


if(isset($_FILES['picture'])){
foreach($_FILES['picture']['tmp_name'] as $key => $tmp_name){
	move_uploaded_file($tmp_name, "data/{$_FILES['picture']['name'][$key]}");
}
//print_r($_FILES);
$querystring = "INSERT INTO presentation_tbl(pre_id,pre_name,pre_banner,pre_text,pre_link,pre_logo1,pre_logo2,pre_logo3) VALUES(NULL,'".$preName."','".$[0]."','".$preText."','".$preLink."','".$[1]."','".$[2]."','".$[3]."')";
$doquery = mysql_query($querystring);
echo "";
} else {
    echo "There was an error uploading the file, please try again!";
}

There are 4 file inputs in the form, one for banner, one for logo1, one for logo2, and the last for logo3.

 

I would assume that since they are in that order, they would be : [0], [1], [2], [3].

 

i just don't know what to put in front of them in the INSERT query to make it display the correct content.

 

As for if I was trying to put the query inside the $_FIELDS block, no i wasn't.  I'm not 100% sure if it should go inside or out.  I thought out was the correct place.

There are 4 file inputs in the form, one for banner, one for logo1, one for logo2, and the last for logo3.

 

I would assume that since they are in that order, they would be : [0], [1], [2], [3].

 

i just don't know what to put in front of them in the INSERT query to make it display the correct content.

 

As for if I was trying to put the query inside the $_FIELDS block, no i wasn't.  I'm not 100% sure if it should go inside or out.  I thought out was the correct place.

 

Well, it would make sense to have the query execute only if a file has been uploaded, correct?  Having the INSERT query run without a file being uploaded wouldn't make any sense.

 

You need to post the form code as well, please.

Very true...

 

Here is what I have changed it to:

 

<?php
set_time_limit(0);
require_once("Connections/myConnect.php");
mysql_select_db("phpmy1_norfolkartscentre_ca");
$preName = $_POST['name'];
$preText = $_POST['text'];
$preLink = $_POST['links'];


if(isset($_FILES['picture'])){
foreach($_FILES['picture']['tmp_name'] as $key => $tmp_name){
	move_uploaded_file($tmp_name, "data/{$_FILES['picture']['name'][$key]}");
	}
	$querystring = "INSERT INTO presentation_tbl(pre_name,pre_banner,pre_text,pre_link,pre_logo1,pre_logo2,pre_logo3) VALUES(".$preName."','".$[0]."','".$preText."','".$preLink."','".$[1]."','".$[2]."','".$[3]."')";
$doquery = mysql_query($querystring);
echo "Upload was a sucess";
}else{
	echo "there was a problem uploading, please try again!";
}

?>

 

and the form code is right here:

 

<form action="admin_presentation2.php" method="post" enctype="multipart/form-data">
<table border="0">
<tr>
<td align="right">Name: </td>
    <td colspan="2"><input name="name" type="text" /></td>
</tr>
<tr>
<td align="right">Text: </td>
    <td colspan="2"><input name="text" type="text" /></td>
</tr>
<tr>
<td align="right">Links: </td>
    <td colspan="2"><input name="links" type="text" /></td>
</tr>
<tr>
<td align="right">Banner: </td>
    <td colspan="2"><input name="picture[]" type="file" /></td>
    <td>(800px X 152px)</td>
</tr>
<tr>
<td align="right">Logo1: </td>
    <td colspan="2"><input name="picture[]" type="file" /></td>
</tr>
<tr>
<td align="right">Logo2: </td>
    <td colspan="2"><input name="picture[]" type="file" /></td>
</tr>
<tr>
<td align="right">Logo3: </td>
    <td colspan="2"><input name="picture[]" type="file" /></td>
</tr>

<tr>
<td><input name="id" type="hidden" value="" /></td>
    <td colspan="2"><input name="submit" type="submit" value="Add!" /></td>
</tr>

</table>
</form>

It's quite simple, actually.  And you had the concept within your foreach() loop by using the $key to differentiate between files:

 

if (isset($_POST['submit'])) {
if (isset($_FILES['picture'])) {
	foreach($_FILES['picture']['tmp_name'] as $key => $tmp_name){
		move_uploaded_file($tmp_name, "data/{$_FILES['picture']['name'][$key]}");
	}

	$pre_banner = $_FILES['picture']['name'][0];
	$pre_logo1  = $_FILES['picture']['name'][1];
	$pre_logo2  = $_FILES['picture']['name'][2];
	$pre_logo3  = $_FILES['picture']['name'][3];

	$querystring = "INSERT INTO presentation_tbl(pre_name,pre_banner,pre_text,pre_link,pre_logo1,pre_logo2,pre_logo3) VALUES(".$preName."','".$pre_banner."','".$preText."','".$preLink."','".$pre_logo1."','".$pre_logo2."','".$pre_logo3."')";
	$doquery = mysql_query($querystring);
	echo "Upload was a sucess";
}
else {
	echo "there was a problem uploading, please try again!";
}
}

Oh, and don't forget to sanitize incoming (form) data before placing within a query.  Look at mysql_real_escape_string.

 

Also, if you are allowing files to be uploaded by the public, you will want to check the MIME types upon upload and not allow for executables or system files (file.exe; file.ini, etc).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.