By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,491 Members | 3,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,491 IT Pros & Developers. It's quick & easy.

Problem uploading / renaming images to server and mysql

P: 9
Ok... I have a noobie issue.

I am trying to upload a photo to specific directory on my server. (this is working correctly with the code below). However, I am also trying to rename the file and upload that filename to a mysql database (users.photo).

This is my code.. it shows no errors but it does not add it to my server.

HELP WOULD BE SO APPRECIATED!!\

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php require_once('../Connections/JMI10.php'); 
  3.  
  4. if (!function_exists("GetSQLValueString")) {
  5. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  6. {
  7.   if (PHP_VERSION < 6) {
  8.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  9.   }
  10.  
  11.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  12.  
  13.   switch ($theType) {
  14.     case "text":
  15.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  16.       break;    
  17.     case "long":
  18.     case "int":
  19.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  20.       break;
  21.     case "double":
  22.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  23.       break;
  24.     case "date":
  25.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  26.       break;
  27.     case "defined":
  28.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  29.       break;
  30.   }
  31.   return $theValue;
  32. }
  33. }
  34. ?>
  35. <?php
  36. $colname_Recordset1 = "-1";
  37. if (isset($_SESSION['MM_Username'])) {
  38.   $colname_Recordset1 = $_SESSION['MM_Username'];
  39. }
  40. mysql_select_db($database_JMI10, $JMI10);
  41. $query_Recordset1 = sprintf("SELECT * FROM `user` WHERE username = %s", GetSQLValueString($colname_Recordset1, "text"));
  42. $Recordset1 = mysql_query($query_Recordset1, $JMI10) or die(mysql_error());
  43. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  44. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  45. ?>
  46. <?
  47. //This is the directory where images will be saved 
  48. $target = "/home/content/n/w/c/nwclark/html/jmi2/secure/idphotos/"; 
  49. $target = $target . basename( $_FILES['photo']['name']); 
  50.  
  51. //This gets all the other information from the form 
  52. $name=$_POST['username']; 
  53. $pic=($_FILES['photo']); 
  54. $width = 640; 
  55. $height = 480; 
  56. //Writes the information to the database 
  57. mysql_query("INSERT INTO `user` VALUES ('$name', '$pic')") ; 
  58.  
  59. //Writes the photo to the server 
  60. if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
  61.  
  62. //Tells you if its all ok 
  63. echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
  64. else { 
  65.  
  66. //Gives and error if its not 
  67. echo "Sorry, there was a problem uploading your file."; 
  68.  
  69. mysql_free_result($Recordset1);
  70. ?>
  71.  
  72.  
  73.  
May 30 '09 #1
Share this Question
Share on Google+
7 Replies


Markus
Expert 5K+
P: 6,050
You can use mysql_error() to retrieve the latest error, otherwise, mysql will not tell you any errors. Add or die(mysql_error()) to your mysql function calls.

Expand|Select|Wrap|Line Numbers
  1. mysql_query("INSERT INTO `user` VALUES ('$name', '$pic')") or die("Error: " . mysql_error());
  2.  
May 30 '09 #2

P: 9
Oh.. ok.. cool .. i should've known that.

i get this error..

Error: Column count doesn't match value count at row 1
May 30 '09 #3

Markus
Expert 5K+
P: 6,050
If you omit the column names from the INSERT statement, MySQL assumes you're going to supply data for every column. After all, how would it know which columns to put data in?

You can either add the column names to your query, or provide values for every column in the VALUES section.
May 30 '09 #4

P: 9
ok.. sorry to sound so ignorant.. (which i am) .. how would you do that?

BTW: I owe you big time.. working hard to learn PHP/MYSQL
May 30 '09 #5

Markus
Expert 5K+
P: 6,050
Not being sure how your table is set up, I'll use an example.

Table set up:

Expand|Select|Wrap|Line Numbers
  1. Database: User
  2. Table: Photos
  3. Columsn: id | photo_name | photo_upload_time | photo_tags
  4.  
Inserting only 2 fields (no fields defined in Insert)
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO
  2.     `User.Photos`
  3.     /** No fields defined here **/
  4.     VALUES
  5.     (
  6.         'photo.jpg',
  7.         '00:00:00'
  8.     );
  9.  
That would cause the same error you received, for the previously mentioned reason.

The code revised:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO
  2.     `User.Photos`
  3.     (
  4.         'photo_name',
  5.         'photo_upload_time'
  6.     )
  7.     VALUES
  8.     (
  9.         'photo.jpg',
  10.         '00:00:00'
  11.     );
  12.  
See that we have now specified which fields should have the data inserted?

If you do want to omit giving the fields, you will have to provide all fields in the VALUES.

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO
  2.     `User.Photos`
  3.     /** No fields defined here **/
  4.     VALUES
  5.     (
  6.         'id',
  7.         'photo.jpg',
  8.         'my photo'
  9.         'a, cool, photo'
  10.     );
  11.  
Understand?

Mark.
May 30 '09 #6

P: 9
Starting to i think .. but none of these option work.. I guess the issue is that i am trying to update a single column on the user table. I did that rather than creating a whole new table for the photo name.

so the basic db design is:

table=user
column=photo

I want to place the photo name there and nothing else.
May 30 '09 #7

Atli
Expert 5K+
P: 5,058
Hi.

Line #53 of your code:
Expand|Select|Wrap|Line Numbers
  1. $pic=($_FILES['photo']);
And then you go on to use $pic as a string in your INSERT query on line #57.
This would put "Array" into the query, which I assume you don't want.

You probably mean to do this:
Expand|Select|Wrap|Line Numbers
  1. $pic = $_FILES['photo']['name'];
As to the other thing...

Like Markus says, your INSERT statements needs to provide values for EVERY column in the table that needs one. But you can leave out columns that have default values.

You just need to specify which columns you want to provide values for.
(Not specifying the columns, like you do in your INSERT query, is never good. You should always specify the columns you plan to use, even if you are going to use all of them. Allows you to add to the table later on without messing up your old queries.)

For example:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `user`(`name`, `pic`)  
  2. VALUES ('$name', '$pic')
  3.  
Assuming the names of the columns are `name` and `pic`, and that there are no other columns in the table that do not have a default value.
May 31 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.