14 Replies - 3436 Views - Last Post: 03 March 2010 - 10:27 PM Rate Topic: -----

#1 phpnoob  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 22-August 09

how to insert the image using hidden

Posted 02 March 2010 - 05:09 AM

hello everyone!!


problem statement--> I have two tables(registration and picture), i want to insert the registration fields into registration table and insert the default image of the user into picture table.

question:

how can i insert the default picture of the user into picture table making it hidden from the users
-------------------------------------->>-----------------------------------------------------------
here is few of my code------->html form submission code----->>


<form action="registerprocess.php" name="myform" onsubmit="return validatepass()" method="post">
               	 <table><tr><td>First&nbsp;Name:</td><td><input type="text" name="fname" size="30" /> </td></tr>
                  <tr><td>Last&nbsp;Name:</td><td><input type="text" name="lname" size="30" /></td></tr>
                    <tr><td>Your&nbsp;Email</td><td><input type="text" name="id" size="30" /></td></tr>
                	 <tr><td>Password:</td><td><input type="password" name="pwd" maxlength="12"size="30" /></td></tr>
                	  <tr><td>Re-Password:</td><td><input type="password" name="rpwd" maxlength="12"  size="30"/></td></tr>
                		<tr><td>I am:</td><td>


-------------------------->php process code-------------------------------->
<?php
mysql_connect("localhost","root","");
mysql_select_db("blood");
$firstname=$_POST['fname'];
$lastname=$_POST['lname'];
$sex=$_POST['sex'];
$email=$_POST['id'];
$day=$_POST['day'];
$month=$_POST['month'];
$dob=$_POST['age'];
$password=$_POST['pwd'];
$repassword=$_POST['rpwd'];
$sql="insert into registration values('$firstname','$lastname','$sex','$email','$day','$month','$dob','$password','$repassword')";
if(mysql_query($sql))
{
session_start();
$_SESSION['id']=$email;
$session_variable=$_SESSION['id'];

header('location:editprofile.php');
}
else
{
header('location:homepage.php');
}




Is This A Good Question/Topic? 0
  • +

Replies To: how to insert the image using hidden

#2 phpnoob  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 22-August 09

Re: how to insert the image using hidden

Posted 02 March 2010 - 05:19 AM

some corrections!!

how to insert the image without having an upload field!! and insert the image into database!!


can i insert like that?

hello everyone!!


problem statement--> I have two tables(registration and picture), i want to insert the registration fields into registration table and insert the default image of the user into picture table.

question:

how can i insert the default picture of the user into picture table making it hidden from the users
-------------------------------------->>-----------------------------------------------------------
here is few of my code------->html form submission code----->>


<form action="registerprocess.php" name="myform" onsubmit="return validatepass()" method="post">
               	 <table><tr><td>First&nbsp;Name:</td><td><input type="text" name="fname" size="30" /> </td></tr>
                  <tr><td>Last&nbsp;Name:</td><td><input type="text" name="lname" size="30" /></td></tr>
                    <tr><td>Your&nbsp;Email</td><td><input type="text" name="id" size="30" /></td></tr>
                	 <tr><td>Password:</td><td><input type="password" name="pwd" maxlength="12"size="30" /></td></tr>
                	  <tr><td>Re-Password:</td><td><input type="password" name="rpwd" maxlength="12"  size="30"/></td></tr>
                		<tr><td>I am:</td><td>


-------------------------->php process code-------------------------------->
<?php
mysql_connect("localhost","root","");
mysql_select_db("blood");
$firstname=$_POST['fname'];
$lastname=$_POST['lname'];
$sex=$_POST['sex'];
$email=$_POST['id'];
$day=$_POST['day'];
$month=$_POST['month'];
$dob=$_POST['age'];
$password=$_POST['pwd'];
$repassword=$_POST['rpwd'];
$sql="insert into registration values('$firstname','$lastname','$sex','$email','$day','$month','$dob','$password','$repassword')";
if(mysql_query($sql))
{
$insert="insert into picture values     ('fill in the blank???????') ";
$exe_insert="mysql_query($insert)";
session_start();
$_SESSION['id']=$email;
$session_variable=$_SESSION['id'];

header('location:editprofile.php');
}
else
{
header('location:homepage.php');
}





help appreciated!!

thank you in advance
Was This Post Helpful? 0
  • +
  • -

#3 phpnoob  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 22-August 09

Re: how to insert the image using hidden

Posted 02 March 2010 - 06:10 AM

there is no one in this forum who could solve this problem
Was This Post Helpful? 0
  • +
  • -

#4 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Re: how to insert the image using hidden

Posted 02 March 2010 - 07:29 AM

Can you provide an example use case? You said you want to do this without an upload field, does that mean you are using pre-selected images? Are you going to take any user submitted images? You also mention having this hidden from the user, I don't quite understand what exactly it is you are trying to accomplish.

If you are only using pre-selected images it would be simpler and more efficient to store a reference to the selected image rather than the image itself.
Was This Post Helpful? 0
  • +
  • -

#5 phpnoob  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 22-August 09

Re: how to insert the image using hidden

Posted 02 March 2010 - 09:26 AM

thanks for replying!!

well I am working on some projects similiar to some social networking sites!!

like this very website!! they have a default image for those users who didn't not upload their profile photos!! look at my image.. even this site has a default image, which is either being displayed hard coded without inserting into database or when the user registers they directly inserts into the database!!

which i prefer using it. as hard coding the image ( calling image not in database) becomes difficult when the query becomes long(refering from 4 to many tables)

so i want to insert the default image(pre-selected image i.e profile image) into database when the user register it.
Was This Post Helpful? 0
  • +
  • -

#6 phpnoob  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 22-August 09

Re: how to insert the image using hidden

Posted 02 March 2010 - 09:40 AM

answer to your query=Are you going to take any user submitted images?



reply: yes I am taking the images from the users too!!


once they register, they default image should be displayed and if they want to change their profile picture,
I will update the database and the new picture will be shown!

but the problem isn't either updating or inserting the image by the user!!


the problem lies, when the user register, i want to insert the default image without having upload filed(making it hidden from users, which every site does) into the database!!

help in this regard would be deeply appreciated!!
Was This Post Helpful? 0
  • +
  • -

#7 Melana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-March 10

Re: how to insert the image using hidden

Posted 02 March 2010 - 09:45 AM

Hi!
Unfortunately, I am not specialist in this area. But may be these php tutorials help you in decision of your problem!
Was This Post Helpful? 0
  • +
  • -

#8 phpnoob  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 22-August 09

Re: how to insert the image using hidden

Posted 02 March 2010 - 09:57 AM

thank you for the link!!


think the link has no solution though :P

nevermind!! there has to be ways to solve this,

there will be someone to solve this!!
Was This Post Helpful? 0
  • +
  • -

#9 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Re: how to insert the image using hidden

Posted 02 March 2010 - 11:43 AM

I haven't had much experience inserting files into a database since it is generally discouraged. I would still recommend storing the user submitted images in a folder and generate a unique name based on the user id, then store the associated image name in the database. I'm fairly certain that's more along the lines of how it's done on this site. Check out this article http://www.phpriot.c...images-in-mysql on storing images in MySQL.

If you still feel this is the way to go you could maybe try something like this:

<?php
mysql_connect("localhost","root","");
mysql_select_db("blood");
$firstname=$_POST['fname'];
$lastname=$_POST['lname'];
$sex=$_POST['sex'];
$email=$_POST['id'];
$day=$_POST['day'];
$month=$_POST['month'];
$dob=$_POST['age'];
$password=$_POST['pwd'];
$repassword=$_POST['rpwd'];

$imgData = file_get_contents("images/default.jpg");

$sql="insert into registration values('$firstname','$lastname','$sex','$email','$day','$month','$dob','$password','$repassword')";
if(mysql_query($sql))
{
  $insert = "INSERT INTO picture (image) VALUES ('{$imgData}')";
  $exe_insert="mysql_query($insert)";
  session_start();
  $_SESSION['id']=$email;
  $session_variable=$_SESSION['id'];

  header('location:editprofile.php');
} else {
  header('location:homepage.php');
}



**EDIT**
Also make sure you are using BLOB as your datatype for images

This post has been edited by bstonehill: 02 March 2010 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

#10 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: how to insert the image using hidden

Posted 02 March 2010 - 01:01 PM

It is possible to set the default value of a field when creating a new row in a relation with SQL or a subset of subset of SQL as long as the DEFAULT keyword is supported.


When creating a table use this as an option:
PictureUrl varchar(255) DEFAULT 'http://www.example.com/picture/jpg'



And each time a row is created without specifying this field then the DEFAULT value is used. The default URL can then be used to be shown on the webpage via a query. If you want to hide the details of the picture from the users you need to move it from the root directory or re-write the URL.
Was This Post Helpful? 0
  • +
  • -

#11 phpnoob  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 22-August 09

Re: how to insert the image using hidden

Posted 02 March 2010 - 10:56 PM

@ bstonehill

thank you bro!!

well i am doing the same!! I am not directly inserting the image into database!!

instead i am inserting the name of the image and the original image is being stored in folder

and when ever required the column name (pic name) associated with the user id(which is email i.e unique)

is being called to display the image!!

with your suggestion above, I think the whole image is getting inserted into image!! which we generally avoid using it!!

so the question is blob?

if i set the datatype as blob.

now the question arises, if they user wants to change the profile picture, i have a normal upload feild and user can choose any file .


a)so will the blob work ?
the datatypes for the two condition(preselected(default image) and user selected image).

since i am refering the same table(picture table) for both the default image as well as update image(user selected image).

I am not using blob in user selected image,i.e having the normal upload feild which is working fine
Was This Post Helpful? 0
  • +
  • -

#12 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Re: how to insert the image using hidden

Posted 03 March 2010 - 08:01 AM

I think you will find it much less of a headache storing the images outside of the database. Aside from not bloating your database it also keeps backups and recovery simpler.

BLOB stands for Binary Large Object, and it is the data type you use when storing files (images, documents, etc). There are variations each with a different max object size (TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB), similar to INT's. If you are not going to store the actual image in the DB you don't need to use this data type.

If you are just storing the path to the image you would just use VARCHAR as you are now. You could set the default image using westforduk's suggestion, but from an error-handling standpoint I would still have the script that loads the user image check for a null or invalid entry (file doesn't exist) and if so show the default image.
Was This Post Helpful? 0
  • +
  • -

#13 phpnoob  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 22-August 09

Re: how to insert the image using hidden

Posted 03 March 2010 - 09:55 AM

yeah thats true!!

check for the null and display the image

thats how i was doing till now!! (i mentioned in the former posts, i mean hard coding(checking for null))


but i got some query which is around 5 to 6 lines, so checking the image(not in database) becomes really complicated!! as you know, sometimes msql query alone doesn't have soultion. so things becomes little bit complicated!!

anyways thanks for your time!!
Was This Post Helpful? 0
  • +
  • -

#14 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Re: how to insert the image using hidden

Posted 03 March 2010 - 10:20 AM

No Problem.

It should be a pretty simple check. Just return the stored value from the database and then do something like this:

/*** Run Your DB Query ***/

//Retrieve image name from DB (We don't care if it's valid or null)
$userImg = $row['userImg']; 

//If image doesn't exist set to default
if(!file_exists($userImg)){ 
  $userImg = "defaultImg.jpg";
}



Good Luck!

This post has been edited by bstonehill: 03 March 2010 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#15 phpnoob  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 22-August 09

Re: how to insert the image using hidden

Posted 03 March 2010 - 10:27 PM

ok!! for those users who might have the same problem.

if you find the solution, please mail me the suggestion at

silentstriker.kills@gmail.com

the above suggestion by the users are correct!!

I mean checking the table for a row

by row, i mean checking for the image existence associated with particular unique key(in my case its email)

the above user suggested that

if the row exist, display the image from the table
else hard code the image


this is possible as i was doing it!!

but when the query becomes very long(referring from 4 to many tables), you can just keep up with the same way as the complexity increases!!.

anyways if i get the solution, I will surely post it here!!

if not, think that, this will hunt me forever :P

thank you guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1