View Full Version : can any point out a way of doing this (insert title name into mysql)

01-08-2007, 09:20 AM
I got my form to submit an image and display it but i want to add extra info i setup the table with the right fields. But it will not add title information into the table with the mage data.

So am trying to get it to display a custom title above the image that the user inputs into the form along with the image. I also might want to add other information in the future if someone could show me a way of doing this i would be very grateful i have been trying since Friday.


echo '
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<form method="post" enctype="multipart/form-data" action="add_image.php">
Select Image File:
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<td colspan="3" bgcolor="#E6E6E6"><strong>Create News</strong> </td>
<td width="14%"><strong>Title</strong></td>
<td width="2%">:</td>
<td width="84%"><input name="title" type="text" id="title" size="50" /></td>
<td valign="top"><strong>Preview image</strong></td>
<td valign="top">:</td>
<td><input type="file" name="userfile" size="40"></td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>




// database connection
$conn = mysql_connect("localhost", "username", "password")
OR DIE (mysql_error());
@mysql_select_db ("image", $conn) OR DIE (mysql_error());


$postdate=date("d/m/y h:i:s");

$sql2="INSERT INTO image(title)VALUES('$title')";

if ($_FILES) {
$image_types = Array ("image/bmp",
if (is_uploaded_file ($_FILES['userfile']['tmp_name'])) {
$userfile = addslashes (fread
(fopen ($_FILES["userfile"]["tmp_name"], "r"),
filesize ($_FILES["userfile"]["tmp_name"])));
$file_name = $_FILES["userfile"]["name"];
$file_size = $_FILES["userfile"]["size"];
$file_type = $_FILES["userfile"]["type"];

if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO image "
. "(image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$userfile}', '{$file_size}', "
. "'{$file_name}', NOW())";
@mysql_query ($sql, $conn);


01-08-2007, 11:11 AM
In that case you will have to add an extra column called e.g. 'title' in your image table in your database to hold that extra piece of information.

After this is done, modify your insert query to this

$sql = "INSERT INTO image (title, image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES ('{$title}', '{$file_type}', '{$userfile}', '{$file_size}', '{$file_name}', NOW())";


Perhaps also include image/png to allow png images.

Also be sure to find out whether magic_quotes are on or off in your php.ini settings. If off you better also apply addslashes to $_POST['title'] before running your query. Or perhaps even better: use mysql_real_escape_string (http://php.net/manual/en/function.mysql-real-escape-string.php)

01-08-2007, 11:34 AM
I modified my query to the code you shown but now it does not even add the image into the database? Any ideas to way its stopped adding anything into it now?

01-08-2007, 12:08 PM
Until you get the script working remove the last two statements: header... and exit(); because otherwise you won't see the error messages.

If this doesn't help then try to echo $sql immediately after it is defined to see what query you are sending.

echo $sql;

Also, are you sure that you actually have all the needed columns in your table?

Edit: Until you get it firing you should also remove the @-operators in front of mysql_... 2 places in order to see warning messages.

01-08-2007, 12:28 PM
thank you for your help so this is what i get????

INSERT INTO image (title, image_type, image, image_size, image_name, image_date) VALUES ('hope', 'image/pjpeg', '\0JFIF\0,,\0\0 LPhotoshop 3.0\08BIM Resolution\0\0\0\0,\0\0\0\0,\0\0\0\08BIM FX Global Lighting Angle\0\0\0\0\0\0\0x8BIMFX Global Altitude\0\0\0\0\0\0\08BIM Print Flags\0\0\0 \0\0\0\0\0\0\0\0\08BIM Copyright Flag\0\0\0\0\0\08BIM\'Japanese Print Flags\0\0\0\0 \0\0\0\0\0\0\0\08BIMColor Halftone Settings\0\0\0H\0/ff\0\0lff\0\0\0\0\0\0\0/ff\0\0\0\0\0\0\0\0\02\0\0\0\0Z\0\0\0\0\0\0\0\0\05\0\0\0\0-\0\0\0\0\0\0\0\08BIMColor Transfer Settings\0\0\0p\0\0\0\0\0\0\0\0\0

ok it seems to see what i have put into the form. If you look in values hope was the title and am guessing that text is the image? But still no luck with getting it to add into the database?

01-08-2007, 12:49 PM
And you don't get any warning messages? Which would be most likely if it wasn't working.

In that case warning messages have probably been disabled. Try to put in the very top of your script:

ini_set('display_errors', 1);

Then execute again.

01-08-2007, 02:28 PM

Thanks for your help my script is now working.

I just rewrote it from the ground up and added your code that you shown from the start and it worked. I guess i just had something i did not see or forgot about causing the problem. It works now any way thanks a lot.