Jump to content

[SOLVED] Undefined variable


emediastudios

Recommended Posts

I get this error

Notice: Undefined variable: count in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 42

In my code below

 

<?php require_once('../Connections/p2w.php');
error_reporting(E_ALL);

//This is the directory where images will be saved 
$target = "../news/"; 

//This gets all the other information from the form 
$headline=$_POST['headline']; 
$contents=$_POST['contents']; 
$photo1=($_FILES['photo1']['name']); 
$photo2=($_FILES['photo2']['name']); 
$photo3=($_FILES['photo3']['name']);
$title1=$_POST['title1']; 
$title2=$_POST['title2'];
$title3=$_POST['title3'];  
$link=$_POST['link']; 
//Writes the photo to the server 
for ($x=1;$x<=3;$x++){
    $photo ='photo'.$x;
$target = "../news/" . basename( $_FILES[$photo]['name']);
if (($_FILES[$photo]["type"] == "image/gif")
|| ($_FILES[$photo]["type"] == "image/png")
|| ($_FILES[$photo]["type"] == "image/jpeg")
|| ($_FILES[$photo]["type"] == "image/pjpeg")
&& ($_FILES[$photo]["size"] < 1000000)){
	if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){

		//Tells you if its all ok 

		echo "Success."; 
$photo="";

		} 
}
else { 

	//Gives and error if its not 
	++$count;
} 
}

if ($count > 0){                       ///////////////////////line 42//////////////////
echo $count .'failed to upload ';

}
else
{
//Writes the information to the database 

$host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="5050888202"; // Mysql password 	 
$db_name="preparetowin"; // Database name 
    $tbl_name="news"; // Table name 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; 
print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">";

}
?>

Link to comment
Share on other sites

I tried to clarify my previous post after cooldude posted...

 

You just need to add:

$count = 0;

 

At the top where you set up the rest of your variables before you start the for loop.

You're getting an error because if all the uploads in your script are successfull, $count is never incremented, so when you test for "  if ($count > 0)  ", it doesn't like the comparison of "  if ( NULL > 0 )  ".

Link to comment
Share on other sites

I tried to clarify my previous post after cooldude posted...

 

You just need to add:

$count = 0;

 

At the top where you set up the rest of your variables before you start the for loop.

You're getting an error because if all the uploads in your script are successfull, $count is never incremented, so when you test for "  if ($count > 0)  ", it doesn't like the comparison of "  if ( NULL > 0 )  ".

 

this wont cause an error

if(NULL >0)  this is valid

even if you have null values it will still compare the two  but try also to initialize your variable maybe its a version issue of php

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.