Jump to content

UPDATE / INSERT / JOIN problems


triole1

Recommended Posts

Hello,

 

I am using MySQL 5.1. I have a php form to upload files. I have a table just for that where i also want to store the user's ID, name, and a link to the file. The upload form is ok (it populates the db with the link and the file's going to the server), but I can't i pull the users info from the signup table where the rest of the information is (ID, name, email)

 

Can you please help me?

 

Thanks a lot!

Link to comment
Share on other sites

HI, Thanks for your reply,

 

I want to pull the name and email from india to photo_india to the same line that the user is uploading the photo. The form is only a 'choose file' button. I am using sessions to identify the user. 

 

I would like the photo_india to be something like:

 

ID  Name  email  link

1      a          b        c

2      a          b        d 

3      e          r        t

4      e          r        j

 

If you know a better way to do this, please tell me.

 

This is the table where i store the signup information:

 

CREATE TABLE `india` (

`id` varchar(65) COLLATE latin1_general_ci NOT NULL,

`name` varchar(50) COLLATE latin1_general_ci NOT NULL,

`password` varchar(50) COLLATE latin1_general_ci NOT NULL,

`email` varchar(50) COLLATE latin1_general_ci NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=57 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci

 

 

This is the table where I want to store the id, name, email and photo's link:

CREATE TABLE `photo_india` (

`id` varchar(65) COLLATE latin1_general_ci NOT NULL,

`name` varchar(65) COLLATE latin1_general_ci NOT NULL,

`email` varchar(65) COLLATE latin1_general_ci NOT NULL,

`photo` varchar(65) COLLATE latin1_general_ci NOT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci

Link to comment
Share on other sites

Like I said, as you already have the name and email info in the india table it makes sense not to double up on that info (google data normalisation when you have a couple hours free).

So, your photo_india table only has ID, userID and URL.  Use the following replacing the PHP variables with the ones that you are using:

INSERT INTO photo_india (userID, URL) VALUES ($_SESSION['userID'], '$url')

to insert the info into the table.

When you want to pull the name and email of the user and attach it to the image use:

SELECT name, email, url FROM india LEFT OUTER JOIN photo_india ON (india.ID = photo_india.userID) where india.ID = $user_id_to_show

Link to comment
Share on other sites

Thanks a lot but something is going wrong, my mistake for sure!.. :( can you find what it is??

 

here goes the code:

 

<?php

 

  // Connects to your Database through config.php

include_once 'config.php';

 

session_start();

 

//This is the directory where images will be saved

$target = "images/";

$target = $target . basename( $_FILES['photo']['name']); 

 

//This gets all the other information from the form

$pic=($_FILES['photo']['name']);

 

//Writes the information to the database

mysql_query("INSERT INTO photo_india (id,photo) VALUES (".$_SESSION['id'].", '$pic') ") ;

 

//Writes the photo to the server

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))

{

 

//Tells you if its all ok

echo "The file  has been uploaded, and your information has been added to the directory";

echo "<br /><a href='upconf.php'>Confirm</a>";

 

}

else {

 

//Gives and error if its not

echo "Sorry, there was a problem uploading your file.";

echo "<br /><a href='index.php'>Try again</a>";

}

 

?>

Link to comment
Share on other sites

What is going wrong and where is it going wrong?

for better error checking in development you should always use something to capture any possible errors in your database queries.  The easiest way is to use the OR DIE function call:

mysql_query("INSERT INTO photo_india (id,photo) VALUES (".$_SESSION['id'].", '$pic') ") or die (mysql_error()); 

This can also be expanded on if you assign your query string to a variable before trying to execute it:

$sql = "INSERT INTO photo_india (id, photo) VALUES ({$_SESSION['id']}, '$pic')";
mysql_query($sql) or die ("The query : <br>$sql<br><br> Returned the following error :<br>".mysql_error());

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.