Jump to content

Using PHP to Uploading an Image and other info into mysql database at the same time?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Hello

I need a little help I have followed Tutorial: Storing Images in MySQL with PHP / Part II / Display your images and I want to change the table and insert.php script a little bit. I’ve changed the table (images) that holds the images by adding a column called subscriber I have another table called subscriber_info that all the subscriber_info is on but what I want to do is when I add the image I want to also record the subscriber_id into the subscriber column on the images table at the same time so that I know who added the picture and also so that I can create a page where all the subscribers can view all of the images that they have uploaded.
I have this script on my add.html page
<form action="insert.php" method="post" enctype="multipart/form-data" name="browse" id="brows_form">

      

        <div align="left">

          <input type="hidden" name="MAX_FILE_SIZE" value="102400" >

          <input name="image" type="file" class="style1" accept="image/jpeg" />

           <input type="hidden" name="subscriber " value="<?php echo $row_subscriber_username['subscriber_id']; ?>" />

          <input type="submit" value="Upload" >

      <span class="style10">Choose Your Profile Picture</span></div>

    </form>
And this script on my insert.php page
<?php

	

	$username = "root";

	$password = "159753";

	$host = "localhost";

	$database = "the_secrets_out";

	$table = "images";

	$column = "image";

	


	$link = mysql_connect($host, $username, $password); if (!$link) {

	die("Can not connect to database: ".mysql_error());

	}

	

	mysql_select_db ($database); 

	

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

		

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

		

		$fp      = fopen($tmpName, 'r');

      	$data = fread($fp, filesize($tmpName));

      	$data = addslashes($data);

      	fclose($fp);


		$query = "INSERT INTO $table ";

		$query .= "($column) VALUES ('$data')";

		$results = mysql_query($query, $link);

	

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

	

	}

	else {

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

	}

	

	mysql_close($link);

	?>
What do I need to change on the insert.php script to fetch the subscriber_id and insert it into the subscriber column?

Edited by whitestar, 15 February 2011 - 12:56 AM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You can insert as many columns as you like with the same query, an example modifying the main query:
$query = "INSERT INTO $table ";
$query .= "($column, subscriber) VALUES ('$data', " . mysql_real_escape_string($_POST['subscriber']) . ")";
That should get the subscriber from your hidden form element which is on add.html. You need to alter your table structure to have a subscriber id column before you do this.

You would run this once to do that:
mysql_query("ALTER TABLE the_secrets_out ADD subscriber INT");

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
thanks alexander it is working perfectly.

#4
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Hello
I got the form to work and it now inserts the id into the subscriber column but I wanted to add a text form element to give the image a name (image_name) and I tried to use the the same script on the insert.php for the id but im picking up some errors on the insert.php page and I tried a couple of other things but all that was happening was it was inserting the words “image_name”. Please advise me if my form element is correct and what I need to add in the VALUES on my insert.php script to get it to work.
Thank you in advance
 

        <div align="left">

          <input type="hidden" name="MAX_FILE_SIZE" value="102400" >

          <input name="image" type="file" class="style1" accept="image/jpeg" />

          <span class="style10">Name the Image</span>

          <input type="text" name="image_name" value="" size="32" />

          <input type="hidden" name="subscriber" value="<?php echo $row_subscriber_username['subscriber_id']; ?>" />

          <input type="submit" value="Upload" >

      <span class="style10">Choose & Name Your Profile Picture</span></div>

    </form>

This is my insert.php page
<?php

	

	$username = "root";

	$password = "159753";

	$host = "localhost";

	$database = "the_secrets_out";

	$table = "images";

	$column = "image";

	$id = "subscriber";

	$name ="image_name";  

	

	$link = mysql_connect($host, $username, $password); if (!$link) {

	die("Can not connect to database: ".mysql_error());

	}

	

	mysql_select_db ($database); 

	

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

		

		$tmpName = $_FILES[$column]['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[$id]) . ")";

		$results = mysql_query($query, $link);

	

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

	

	}

	else {

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

	}

	

	mysql_close($link);

	?>


#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You would need to do the same thing, but with a varchar column. First add it separately:
mysql_query("ALTER TABLE the_secrets_out ADD image_name VARCHAR(30)");  

And then of course add it to your query:
$query .= "($column, $name, $id) VALUES ('$data', '" . mysql_real_escape_string($_POST['image_name'] . "', " . mysql_real_escape_string($_POST[$id]) . ")"; 

As the image name is a string it needs to be wrapped around single quotes.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Thank you Alexander yet again you were 100% right you forgot to close the bracket after
('$data', '" . mysql_real_escape_string($_POST['image_name'] . "'
but it was an easy one to figure out! Thnak you so much and I dont think I will need any more help "cross fingers".




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users