473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem uploading / renaming images to server and mysql

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
7 2470
Markus
6,050 Expert 4TB
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
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
6,050 Expert 4TB
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
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
6,050 Expert 4TB
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
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
5,058 Expert 4TB
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

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

Similar topics

0
by: Terry A. Haimann | last post by:
I have an application, DipperBase, that keeps astronomical images in a mysql database. The actual images are stored in a blob field. To get some test data, I downloaded some SEDS images of...
4
by: Ian Davies | last post by:
Hello all The following code allows me to connect to a local MySQL database on my pc from a VB6 project. **************************************************************************** ** Dim...
3
by: Pitcairnia | last post by:
The basic purpose of the site is for authenticated users to post event listings, which often include photographs. The user is faced with a page where they can insert all of the information about...
4
by: Ramakrishnan Nagarajan | last post by:
Hi, I am facing a different problem in my application. In that I am uploading a folder that contain only images. Everything was done and was working fine too. When I executed the application using...
9
by: C.Joseph Drayton | last post by:
Hi All, I have a web site I am developing, and have a question. I would like members to be able to upload pictures. Do you think they should be saved as individual files or should they be put...
1
by: joe | last post by:
Any articles relating with Uploading images files to server and resize the image by asp.net 2.0
14
w33nie
by: w33nie | last post by:
What I'm trying to do here, is upload a video to the ../video/ folder, and up to 5 images to the ../images/ folder. As well as the database information like title, content and each file's file...
8
by: geert | last post by:
Hi all, I have a mac mini running maocosx 10.5 leopard I want to deploy a django project on. My backend is MySQL, and I have it running as a 64- bit app. Of course, apache2 is also running as...
1
by: zaheerusman | last post by:
Hi, I am using flex for interface, java for intermediate code and mysql for DB. My website contains videos so instead of uploading them on db, i have instead put a path in db and stored videos on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.