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
Share on other sites

No. Insert them each into its own row in the table. You probably want to add a page ID so you know what page those images should be displayed in. And if pre_id is a PRIMARY_KEY, exclude it from the SQL. It will increment itself so you don't have to set it to NULL.

Link to comment
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!";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!";
}
}

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.