Clicky

Hi all,

I am getting the following error in this code:

Notice: Undefined variable: title in C:\wamp\www\index.php on line 27

This is line 27:

 SET ext='$ext', title='$title',

Here is the full code:

<?php

if(isset($_POST['upload']))
{
if (isset($_FILES['photo']))
{
@list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']);


if ($imtype == 3)
$ext="png";  
elseif ($imtype == 2)
$ext="jpeg";
elseif ($imtype == 1)
$ext="gif";
else
$msg = 'Error: unknown file format';

if (!isset($msg))
{
$data = file_get_contents($_FILES['photo']['tmp_name']);
$data = mysql_real_escape_string($data);


mysql_query("INSERT INTO myTable
SET ext='$ext', title='$title',
data='$data'");

$msg = 'Success: image uploaded';
}
}
}

?>

<form action="index.php" method="POST" enctype="multipart/form-data">
<label for="title">Title:</label><br>
<input type="text" name="title" id="title" size="64"><br><br>

<label for="photo">Photo:</label><br>
<input type="file" name="photo" id="photo"><br><br>

<input type="submit" name="upload" value="upload">
</form>


Thanks

asked 12/16/2011 07:50

error77's gravatar image

error77 ♦♦


4 Answers:
It's exactly as it says. You are referring to a variable called $title, but nowhere in your code sets this variable so it's undefined.

Set it using the following somewhere before line 27

$title="Your Title";



link

answered

ChrisStanyon's gravatar image

ChrisStanyon

Try to use attached code and see if it makes sense.

Cheers
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
<?php
if(isset($_POST['upload']))
{
if (isset($_FILES['photo']))
{
@list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']);

$msg = '';
if ($imtype == 3)
$ext="png";  
elseif ($imtype == 2)
$ext="jpeg";
elseif ($imtype == 1)
$ext="gif";
else
$msg = 'Error: unknown file format';

if (!empty($msg))
{
$data = file_get_contents($_FILES['photo']['tmp_name']);
$data = mysql_real_escape_string($data);


mysql_query("INSERT INTO myTable
SET ext='$ext', title='$title',
data='$data'");

$msg = 'Success: image uploaded';
}
}
}

?>
link

answered 2011-12-17 at 03:59:21

marqusG's gravatar image

marqusG

Seems like you are depending on register_globals, which was depricated in 5.3 (http://php.net/manual/en/security.globals.php)
You need to initialize your variables properly.
1:
2:
$title = '';
if(isset($_POST['title'])) $title = $_POST['title'];
link

answered 2011-12-17 at 03:59:30

sudaraka's gravatar image

sudaraka

hi,
please print the o/p

 please replace  isset condition with
if(isset($_POST['upload']) && ($_POST['upload'] =='upload'))
{
// your rest code......
}

echo $sql ="INSERT INTO myTable SET ext='$ext', title='$title',data='$data'";
mysql_query($sql);

print this script
link

answered 2011-12-17 at 03:59:51

insoftservice's gravatar image

insoftservice

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×191

Asked: 12/16/2011 07:50

Seen: 263 times

Last updated: 12/16/2011 08:23