Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

adding multiple images into a MySQL database with PHP

max_file_size mysql

  • Please log in to reply
17 replies to this topic

#13 whitestar

whitestar

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 07 March 2011 - 02:32 AM

Hello Alexander

I tried your code but am getting this error
Fatal error: Call to undefined function is_empty()
...........on line 540

refering to this line
else if (!is_empty($_FILES['image']['image_name'][$i])  && 

  • 0

#14 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 07 March 2011 - 10:16 PM

Had not used that one in awhile, try replacing that with empty()
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#15 whitestar

whitestar

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 08 March 2011 - 12:15 AM

which part should I replace the empty()
should I do it like this
 else if (!is_empty())

or do it like this
else if ()

  • 0

#16 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 08 March 2011 - 12:32 AM

!is_empty() should be !empty()
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#17 whitestar

whitestar

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 09 March 2011 - 03:07 AM

Hello Alexander

I tried that and it no longer gives me any error messages it simple displays the page but when I try to upload the images it doesnt say that they are uploaded or not and when I check the database the images are still not loaded into there.

I did however have an idea on a different way to achieve the same result and tested it and it works.
So Thank you for all your advice.
This is how I got it to work just incase anyone else is intersted in trying the same thing!

I used a single form to browse and choose all of the images that I want to upload naming each one(image_main, image_1, image_2 etc) I also name the form elements that allo you to name each image(image_name_main, image_name_1, image_name2 etc) there is a single element that sends the subscribers id.
here is what the forms script looks like.

add.php
<form action="insert.php" method="post" enctype="multipart/form-data" name="browse" id="brows_form">

<div align="left">
<p <span class="style10">Main Image Max File Size 330KB</span>
<input type="hidden" name="MAX_FILE_SIZE" value="852400" >
<input name="image_main" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Main Image</span>
<input type="text" name="image_name_main" value="" size="32" />

<p <span class="style10">Image #1 Max File Size 330KB</span>
<input name="image_1" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Image #1</span>
<input type="text" name="image_name_1" value="" size="32" />

<p <span class="style10">Image #2 Max File Size 330KB</span>
<input name="image_2" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Image #2</span>
<input type="text" name="image_name_2" value="" size="32" />

<p <span class="style10">Image #3 Max File Size 330KB</span>
<input name="image_3" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Image #3</span>
<input type="text" name="image_name_3" value="" size="32" />

<input type="hidden" name="subscriber" value="<?php echo $row_subscriber_username['suubscriber_id']; ?>" /></p>
<input type="submit" value="Upload" >
<span class="style10">Choose & Name Your Picture</span></div>
</form>



I then just repeated the php code on the insert.php page which inserts the data into the database except for changing the name of the elements that are fetched($column_retrieve) and ($name_retrieve) on each piece here is the script on the insert.php page

insert.php
<?php

$username = "_";
$password = "_";
$host = "_";
$database = "_";
$table = "images";
$column = "image";
$column_retrieve = "image_main";
$id = "subscriber";
$name = "image_name";
$name_retieve = "image_name_main";

$link = mysql_connect($host, $username, $password); if (!$link) {
die("Can not connect to database: ".mysql_error());
}

mysql_select_db ($database);

if (isset($_FILES[$column_retrieve]) && $_FILES[$column_retrieve]['size'] > 0) {

$tmpName = $_FILES[$column_retrieve]['tmp_name'];

$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

$query = "INSERT INTO $table ";
$query .= "($column, $name, $id) VALUES ('$data', '" . mysql_real_escape_string($_POST[$name_retieve]). "', " . mysql_real_escape_string($_POST[$id]) . ")";
$results = mysql_query($query, $link);

print "Thank you, Your Main Image has been uploaded. DONT FORGET to add the image into your Spot! ";

}
else {
print "No Main image selected/uploaded. Make sure that you have not exceeded the max file size!";
}

mysql_close($link);
?></p>
<p align="center" class="style13"><?php

$username = "_";
$password = "_";
$host = "_";
$database = "_";
$table = "images";
$column = "image";
$column_retrieve = "image_1";
$id = "subscriber";
$name = "image_name";
$name_retieve = "image_name_1";

$link = mysql_connect($host, $username, $password); if (!$link) {
die("Can not connect to database: ".mysql_error());
}

mysql_select_db ($database);

if (isset($_FILES[$column_retrieve]) && $_FILES[$column_retrieve]['size'] > 0) {

$tmpName = $_FILES[$column_retrieve]['tmp_name'];

$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

$query = "INSERT INTO $table ";
$query .= "($column, $name, $id) VALUES ('$data', '" . mysql_real_escape_string($_POST[$name_retieve]). "', " . mysql_real_escape_string($_POST[$id]) . ")";
$results = mysql_query($query, $link);

print "Thank you, Your # 1 Image has been uploaded. DONT FORGET to add the image into your Spot! ";

}
else {
print "No # 1 image selected/uploaded. Make sure that you have not exceeded the max file size!";
}

mysql_close($link);
?> </p>
<p align="center" class="style13"><?php

$username = "_";
$password = "_";
$host = "_";
$database = "_";
$table = "images";
$column = "image";
$column_retrieve = "image_2";
$id = "subscriber";
$name = "image_name";
$name_retieve = "image_name_2";

$link = mysql_connect($host, $username, $password); if (!$link) {
die("Can not connect to database: ".mysql_error());
}

mysql_select_db ($database);

if (isset($_FILES[$column_retrieve]) && $_FILES[$column_retrieve]['size'] > 0) {

$tmpName = $_FILES[$column_retrieve]['tmp_name'];

$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

$query = "INSERT INTO $table ";
$query .= "($column, $name, $id) VALUES ('$data', '" . mysql_real_escape_string($_POST[$name_retieve]). "', " . mysql_real_escape_string($_POST[$id]) . ")";
$results = mysql_query($query, $link);

print "Thank you, Your # 2 Image has been uploaded. DONT FORGET to add the image into your Spot! ";

}
else {
print "No # 2 image selected/uploaded. Make sure that you have not exceeded the max file size!";
}

mysql_close($link);
?>
</p>
<p align="center" class="style13"><?php

$username = "_";
$password = "_";
$host = "_";
$database = "_";
$table = "images";
$column = "image";
$column_retrieve = "image_3";
$id = "subscriber";
$name = "image_name";
$name_retieve = "image_name_3";

$link = mysql_connect($host, $username, $password); if (!$link) {
die("Can not connect to database: ".mysql_error());
}

mysql_select_db ($database);

if (isset($_FILES[$column_retrieve]) && $_FILES[$column_retrieve]['size'] > 0) {

$tmpName = $_FILES[$column_retrieve]['tmp_name'];

$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

$query = "INSERT INTO $table ";
$query .= "($column, $name, $id) VALUES ('$data', '" . mysql_real_escape_string($_POST[$name_retieve]). "', " . mysql_real_escape_string($_POST[$id]) . ")";
$results = mysql_query($query, $link);

print "Thank you, Your # 3 Image has been uploaded. DONT FORGET to add the image into your Spot! ";

}
else {
print "No # 3 image selected/uploaded. Make sure that you have not exceeded the max file size!";
}

mysql_close($link);
?>


I understand that it is possibly not the best way to do it but it is simple and it works!
  • 0

#18 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 09 March 2011 - 05:41 PM

Good job! Glad you could get it working at least before you move on to the more fun stuff (making it customized!)

You could probably loop the code fairly easily ("Your # $i image has been uploaded" for example) which you may want to try if you are working with this script later on to make it clearer, but it looks alright.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.






Also tagged with one or more of these keywords: max_file_size, mysql

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download