I am trying to upload few texts and the image using the code But its showing error have a look at the two codes...

<body>
<center>
    <h2>Add News</h2>
    
    <form name="news_registration" action="addnews_process.php" method="post" enctype="multipart/form-data">
    <table width="600" border="0" align="center">
      <tr>
        <td width="49%" align="left">News Type</td>
        <td width="3%">&nbsp;</td>
        <td width="48%" align="right"><select name="category" id="category">
			<?php
				include ('db_connect.php');
				$sql="SELECT category FROM category";
				$result=mysql_query($sql);
				while($mem=mysql_fetch_array($result))
				{
				?>
					<option value="<?php echo $mem['category'];?>"><?php echo $mem['category'];?></option>
				<?php
				}
				?>
		</select>
		</td>
      </tr>
      <tr>
        <td align="left">Place</td>
        <td>&nbsp;</td>
        <td><input name="place" type="text" id="place" maxlength="100" /></td>
      </tr>
      <tr>
        <td align="left">Headlines</td>
        <td>&nbsp;</td>
        <td><input name="headline" type="text" id="headline" size="50" maxlength="200" />
		</textarea><br>
		</td>
      </tr>
      <tr>
        <tr>
        <td align="left">News Abstract</td>
        <td>&nbsp;</td>
        <td><textarea name="bdesc" id="bdesc" rows='2' cols='50' >
		</textarea>
		</td>
      </tr>
      </tr>
      <tr>
        <td align="left">News in Detail</td>
        <td>&nbsp;</td>
        <td><textarea name="para1" id="para1" rows='10' cols='50'>
		</textarea>
		</td>
      </tr>
      <tr>
      <td colspan="3" align="left"><font color="red" size="2"><p>
      <?php
				include ('db_connect.php');
				$sql="SELECT nno FROM flag";
				$result=mysql_query($sql);
				$mem=mysql_fetch_array($result);
				$mem_nno = $mem['nno'];
				$z=$mem_nno+1;
				echo "<i>* Its better if you rename your image as $z </i>";
	  ?>
	  </p>
	  </font>
	  </td>
	  </tr>
      <tr>
        <td align="left">Add Image</td>
        <td>&nbsp;</td>
        <td><label for="file">Filename:</label><input type="file" name="file" id="file" /></td>
      </tr>
      <tr>
        <td align="left">&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td align="right"><input name="submit" type="submit" id="button" value="Submit" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    </form>
</body>

This was the form


And the upload script is

<?php
	include('db_connect.php');
	if(empty($_POST['category']) or empty($_POST['place']) or empty($_POST['headline']) or empty($_POST['bdesc']) or empty($_POST['para1'])){
		echo "<script language='javascript' type='text/javascript'> window.alert('Some of the fields are empty! Please fill it up.'); </script>";
		echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";

	}
	else{
		$pno = "1";
		$sql="SELECT * FROM flag where sno=$pno";
		$result=mysql_query($sql);
		$st=mysql_fetch_array($result);
		$st_nno=$st["nno"];
		$st_nno=$st_nno+1;
		echo "$st_nno";
	
		$sql = mysql_query("INSERT INTO news VALUES('$_POST[category]','$_POST[place]',now(),'$_POST[headline]','$_POST[bdesc]','$_POST[para1]',$st_nno)");
	}

	if ((($_FILES["file"]["type"] == "image/jpgf")
	|| ($_FILES["file"]["type"] == "image/jpeg")
	|| ($_FILES["file"]["type"] == "image/png"))
	&& ($_FILES["file"]["size"] < 2000000))
	  {
	  if ($_FILES["file"]["error"] > 0)
	    {
	    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
	    }
	  else
	    {
	   $fname= $_FILES["file"]["name"];
	    if (file_exists("upload/".$_FILES["file"]["name"]))
	      {
	      	echo "<script language='javascript' type='text/javascript'> window.alert('File already exists rename it to $st_nno'); </script>";
			echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";
	      }
	    else
	      {
	      move_uploaded_file($_FILES["file"]["tmp_name"],
	      "upload/" . $_FILES["file"]["name"]);
	      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
	      }
	    }
	  }
	else
	  {
	  echo "Invalid file";
	  }
	  
	 if($sql){
	 	$sql = mysql_query("UPDATE flag SET nno=$st_nno WHERE sno=$pno");
	 	$result = mysql_query("INSERT INTO pic VALUES ('$st_nno','$fname')");
		echo "<script language='javascript' type='text/javascript'> window.alert('News has been added'); </script>";
		echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";
		}
		else{
		echo "<script language='javascript' type='text/javascript'> window.alert('Error adding news'); </script>";
		echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";
		}
	?>

But while adding news If i amk writing the news in the textbox then its properly uploaded but if i am copying the text and pasting it then it shows error

someone please help me its urgent...

What are you copying and pasting from? Microsoft Word?

Try copying and pasting from a standard .txt file and see if you have the same problem. Some text editors like Word add arbitrary code that you don't see when you copy and paste and it can cause strange issues like that.

Member Avatar for iamthwee
if ((($_FILES["file"]["type"] == "image/jpgf")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000))

Try simplifying this statement first to see if you can upload just a png file.

Ya I hav copied from notepad

Also corrected thatimage file is getting uploaded but the problem is with the text

The para1 I am uploading is Varchar(2000) is there any problem with taht ?

<?php
	include('db_connect.php');
	if(empty($_POST['category']) or empty($_POST['place']) or empty($_POST['headline']) or empty($_POST['bdesc']) or empty($_POST['para1'])){
		echo "<script language='javascript' type='text/javascript'> window.alert('Some of the fields are empty! Please fill it up.'); </script>";
		echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";

	}
	else{
		$pno = "1";
		$sql="SELECT * FROM flag where sno=$pno";
		$result=mysql_query($sql);
		$st=mysql_fetch_array($result);
		$st_nno=$st["nno"];
		$st_nno=$st_nno+1;
	
		$sql = mysql_query("INSERT INTO news VALUES('$_POST[category]','$_POST[place]',now(),'$_POST[headline]','$_POST[bdesc]','$_POST[para1]',$st_nno)");
	}

	if ((($_FILES["file"]["type"] == "image/jpg")
	|| ($_FILES["file"]["type"] == "image/jpeg")
	|| ($_FILES["file"]["type"] == "image/png"))
	&& ($_FILES["file"]["size"] < 2000000))
	  {
	  if ($_FILES["file"]["error"] > 0)
	    {
	    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
	    }
	  else
	    {
	   $fname= $_FILES["file"]["name"];
	    if (file_exists("upload/".$_FILES["file"]["name"]))
	      {
	      	echo "<script language='javascript' type='text/javascript'> window.alert('File already exists rename it to $st_nno'); </script>";
			echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";
	      }
	    else
	      {
	      move_uploaded_file($_FILES["file"]["tmp_name"],
	      "upload/" . $_FILES["file"]["name"]);
	      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
	      }
	    }
	  }
	else
	  {
	  echo "Invalid file";
	  }
	  
	 if($sql){
	 	$sql = mysql_query("UPDATE flag SET nno=$st_nno WHERE sno=$pno");
	 	$result = mysql_query("INSERT INTO pic VALUES ('$st_nno','$fname')");
		echo "<script language='javascript' type='text/javascript'> window.alert('News has been added'); </script>";
		echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";
		}
		else{
		echo "<script language='javascript' type='text/javascript'> window.alert('Error adding news'); </script>";
		echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";
		}
?>

What version of MySQL are you working with? Varchar was only able to handle up to 255 characters before MySQL 5.0.3 but recent versions can handle up to 65,535 characters in 5.0.3 and later versions.

use CODE tags to post your code.

What error are you getting?
Are you using single or double quote in notepad text?

its ok its 5

<?php
	include('db_connect.php');
	if(empty($_POST['category']) or empty($_POST['place']) or empty($_POST['headline']) or empty($_POST['bdesc']) or empty($_POST['para1'])){
		echo "<script language='javascript' type='text/javascript'> window.alert('Some of the fields are empty! Please fill it up.'); </script>";
		echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";

	}
	else{
		$pno = "1";
		$sql="SELECT * FROM flag where sno=$pno";
		$result=mysql_query($sql);
		$st=mysql_fetch_array($result);
		$st_nno=$st["nno"];
		$st_nno=$st_nno+1;
	
		$sql = mysql_query("INSERT INTO news VALUES('$_POST[category]','$_POST[place]',now(),'$_POST[headline]','$_POST[bdesc]','$_POST[para1]','$st_nno')");
		if($sql)
		{
	 		$rs = mysql_query("UPDATE flag SET nno=$st_nno WHERE sno=$pno");
	 		echo "<script language='javascript' type='text/javascript'> window.alert('News has been added'); </script>";
			echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";
		}
		else
		{
			echo "<script language='javascript' type='text/javascript'> window.alert('Error adding news'); </script>";
			echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";
		}
	}

	if ((($_FILES["file"]["type"] == "image/jpg")
	|| ($_FILES["file"]["type"] == "image/jpeg")
	|| ($_FILES["file"]["type"] == "image/png"))
	&& ($_FILES["file"]["size"] < 2000000))
	  {
	  if ($_FILES["file"]["error"] > 0)
	    {
	    	echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
	    }
	  else
	    {
	    	$fname= $_FILES["file"]["name"];
	    	if (file_exists("upload/".$_FILES["file"]["name"]))
	      		{
	      			echo "<script language='javascript' type='text/javascript'> window.alert('File already exists rename it to $st_nno'); </script>";
					echo "<script language='javascript' type='text/javascript'> location.href='addnews.php' </script>";
	      		}
	    	else
	      		{
	      			move_uploaded_file($_FILES["file"]["tmp_name"],
	      			"upload/" . $_FILES["file"]["name"]);
			    }
	    }
	  }
	else
	  {
	  	echo "Invalid file";
	  }
	  $result = mysql_query("INSERT INTO pic VALUES ('$st_nno','$fname')");	 
	?>

See the problem here is that if am writing the text and then uploading then its ok but if am copy pasting the test its saying "Error Adding News"

Try:

echo = $sql;

right after that error in the code to see if it looks correct.

Its showing

Parse error: parse error in C:\wamp\www\files\admin\addnews_process.php on line 26

and if am using echo "$sql"; then no change...

Add this code at line#16.
Try to copy paste data and Post query you are getting.
Also try to run that query in phpmyadmin.

echo "INSERT INTO news VALUES('$_POST[category]','$_POST[place]',now(),'$_POST[headline]','$_POST[bdesc]','$_POST[para1]','$st_nno')"; exit;

Like vibhadevit had asked you, are you using any single or double quotes in your text that you are pasting in?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.