Problem uploading / renaming images to server and mysql | Newbie | | Join Date: May 2009
Posts: 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!!\ -
-
<?php require_once('../Connections/JMI10.php');
-
-
if (!function_exists("GetSQLValueString")) {
-
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
-
{
-
if (PHP_VERSION < 6) {
-
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
-
}
-
-
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
-
-
switch ($theType) {
-
case "text":
-
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
-
break;
-
case "long":
-
case "int":
-
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
-
break;
-
case "double":
-
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
-
break;
-
case "date":
-
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
-
break;
-
case "defined":
-
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
-
break;
-
}
-
return $theValue;
-
}
-
}
-
?>
-
<?php
-
$colname_Recordset1 = "-1";
-
if (isset($_SESSION['MM_Username'])) {
-
$colname_Recordset1 = $_SESSION['MM_Username'];
-
}
-
mysql_select_db($database_JMI10, $JMI10);
-
$query_Recordset1 = sprintf("SELECT * FROM `user` WHERE username = %s", GetSQLValueString($colname_Recordset1, "text"));
-
$Recordset1 = mysql_query($query_Recordset1, $JMI10) or die(mysql_error());
-
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
-
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
-
?>
-
<?
-
//This is the directory where images will be saved
-
$target = "/home/content/n/w/c/nwclark/html/jmi2/secure/idphotos/";
-
$target = $target . basename( $_FILES['photo']['name']);
-
-
//This gets all the other information from the form
-
$name=$_POST['username'];
-
$pic=($_FILES['photo']);
-
$width = 640;
-
$height = 480;
-
//Writes the information to the database
-
mysql_query("INSERT INTO `user` VALUES ('$name', '$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 ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
-
}
-
else {
-
-
//Gives and error if its not
-
echo "Sorry, there was a problem uploading your file.";
-
}
-
-
mysql_free_result($Recordset1);
-
?>
-
-
-
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: Problem uploading / renaming images to server and mysql
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. -
mysql_query("INSERT INTO `user` VALUES ('$name', '$pic')") or die("Error: " . mysql_error());
-
| | Newbie | | Join Date: May 2009
Posts: 9
| | | re: Problem uploading / renaming images to server and mysql
Oh.. ok.. cool .. i should've known that.
i get this error..
Error: Column count doesn't match value count at row 1
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: Problem uploading / renaming images to server and mysql
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.
| | Newbie | | Join Date: May 2009
Posts: 9
| | | re: Problem uploading / renaming images to server and mysql
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
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: Problem uploading / renaming images to server and mysql
Not being sure how your table is set up, I'll use an example.
Table set up: -
Database: User
-
Table: Photos
-
Columsn: id | photo_name | photo_upload_time | photo_tags
-
Inserting only 2 fields (no fields defined in Insert) -
INSERT INTO
-
`User.Photos`
-
/** No fields defined here **/
-
VALUES
-
(
-
'photo.jpg',
-
'00:00:00'
-
);
-
That would cause the same error you received, for the previously mentioned reason.
The code revised: -
INSERT INTO
-
`User.Photos`
-
(
-
'photo_name',
-
'photo_upload_time'
-
)
-
VALUES
-
(
-
'photo.jpg',
-
'00:00:00'
-
);
-
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. -
INSERT INTO
-
`User.Photos`
-
/** No fields defined here **/
-
VALUES
-
(
-
'id',
-
'photo.jpg',
-
'my photo'
-
'a, cool, photo'
-
);
-
Understand?
Mark.
| | Newbie | | Join Date: May 2009
Posts: 9
| | | re: Problem uploading / renaming images to server and mysql
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.
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,752
| | | re: Problem uploading / renaming images to server and mysql
Hi.
Line #53 of your code:
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: - $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: -
INSERT INTO `user`(`name`, `pic`)
-
VALUES ('$name', '$pic')
-
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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|