Jump to content

Couple of Problems


topflight

Recommended Posts

Like I stated in my post before I am still learning PHP and I just receiving two problems. 1) is I keep receiving this message here Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\addnewsv.php on line 38 and the second problem I am having is that when the user does not complete a filed on the from the error message shows but it still get submitted in the database here is my code

<?
if(isset($_POST['news'])){
// Dsfining Variables
$authorname = $_POST['authorname'];
$newstitle = $_POST['newstitle'];
$news = $_POST['news'];
$ip = $_SERVER['REMOTE_ADDR'];
$now = date("m.d.y"); 

// Did the admin completed everything?

if($authorname == NULL){
echo 'Please provide you name!</br>';
}
if($newstitle == NULL) {
echo 'Please provide a Title!</br>';
}
if($news == NULL){
echo 'Please provide an Article!</br>';
} else {
$query = "SELECT * FROM news WHERE title = '$newstitle'";
$result = mysql_query($query);

$num = mysql_num_rows($result);

if($checkt > '1'){
echo 'That Title Already Exists';
}
}
}

$update = mysql_query("INSERT INTO `news` (authorname,date,newstitle,news,ip) VALUES ('".mysql_real_escape_string($_POST['authorname'])."','$now','".mysql_real_escape_string($_POST['newstitle'])."','".mysql_real_escape_string($_POST['news'])."','$ip')")or die (mysql_error());


echo 'News Submitted In Database';



?>

 

Thanks in advances

Link to comment
https://forums.phpfreaks.com/topic/127415-couple-of-problems/
Share on other sites

Well I fiexed that problem and Now I am recceving an error saying

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\addnewsv.php on line 36

 

and line 36 is

 

35:$query = "SELECT * FROM news WHERE title = '$newstitle'";

36:$result = mysql_query($query)or die mysql_error();

 

Also I am still trying to figure out a way if the user did not complete all the fields it will not insert into the database.

Link to comment
https://forums.phpfreaks.com/topic/127415-couple-of-problems/#findComment-659071
Share on other sites

Try it this way...

What are you getting?

 

<?
if(isset($_POST['news']))
{
// Dsfining Variables
$authorname = $_POST['authorname'];
$newstitle = $_POST['newstitle'];
$news = $_POST['news'];
$ip = $_SERVER['REMOTE_ADDR'];
$now = date("m.d.y");

// Did the admin completed everything?

if($authorname == NULL){
	error('Please provide you name!</br>');
}
if($newstitle == NULL){
	error('Please provide a Title!</br>');
}
if($news == NULL){
	error('Please provide an Article!</br>');
}
else
{
	$query = "SELECT * FROM `news` WHERE title = '$newstitle'";
	$result = mysql_query($query) or die(mysql_error());

	$num = mysql_num_rows($result) or die(mysql_error());

	if($checkt > '1'){
		error( 'That Title Already Exists');
	}
}
}

$update = mysql_query("INSERT INTO `news` (authorname,date,newstitle,news,ip) VALUES ('".mysql_real_escape_string($_POST['authorname'])."','$now','".mysql_real_escape_string($_POST['newstitle'])."','".mysql_real_escape_string($_POST['news'])."','$ip')") or die (mysql_error());


echo 'News Submitted In Database';


function error ($str)
{
die("<b><font color=\"red\">$str</font></b>");
}


?>

 

 

Orio.

Link to comment
https://forums.phpfreaks.com/topic/127415-couple-of-problems/#findComment-659072
Share on other sites

If you want to display all of the errors you could try something like that:

 

<?php

if(isset($_POST['news']))
{
   // Dsfining Variables
   $authorname = $_POST['authorname'];
   $newstitle = $_POST['newstitle'];
   $news = $_POST['news'];
   $ip = $_SERVER['REMOTE_ADDR'];
   $now = date("m.d.y");

   // Did the admin completed everything?
   $errors = array();

   if($authorname == NULL){
      $errors[] = 'Please provide your name!';
   }
   if($newstitle == NULL){
      $errors[] = 'Please provide a Title!';
   }
   if($news == NULL){
      $errors[] = 'Please provide an Article!';
   }
   else
   {
      $query = "SELECT * FROM `news` WHERE title = '$newstitle'";
      $result = mysql_query($query) or die(mysql_error());

      $num = mysql_num_rows($result) or die(mysql_error());

      if($checkt > '1'){
         $errors[] = 'That Title Already Exists';
      }
   }

   if(!empty($errors))
	error(implode("<br />", $errors);

$update = mysql_query("INSERT INTO `news` (authorname,date,newstitle,news,ip) VALUES ('".mysql_real_escape_string($_POST['authorname'])."','$now','".mysql_real_escape_string($_POST['newstitle'])."','".mysql_real_escape_string($_POST['news'])."','$ip')") or die (mysql_error());

echo 'News Submitted In Database';
}

function error ($str)
{
   die("<b><font color=\"red\">$str</font></b>");
}


?>

 

Orio

Link to comment
https://forums.phpfreaks.com/topic/127415-couple-of-problems/#findComment-659083
Share on other sites

Try this:

<?
if(isset($_POST['news']))
{
// Defining Variables
$authorname = $_POST['authorname'];
$newstitle = $_POST['newstitle'];
$news = $_POST['news'];
$ip = $_SERVER['REMOTE_ADDR'];
$now = date("m.d.y");

// Did the admin completed everything?

$error_msg = '';

if($authorname == NULL){
	$error_msg .= 'Please provide you name!</br>';
}
if($newstitle == NULL){
	$error_msg .= 'Please provide a Title!</br>';
}
if($news == NULL){
	$error_msg .= 'Please provide an Article!</br>';
}
else
{
	$query = "SELECT * FROM `news` WHERE title = '$newstitle'";
	$result = mysql_query($query) or die(mysql_error());

	$num = mysql_num_rows($result) or die(mysql_error());

	// I'm pretty sure you wanted $num here instead of $checkt - Brandon
	if($num > 0){
		$error_msg .= 'That Title Already Exists';
	}
}

if(!empty($error_msg))
{
	echo '<font style="color: red; font-weight: bold;">' . $error_msg . '</font>';
}
else
{
	$update = mysql_query("INSERT INTO `news` (authorname,date,newstitle,news,ip) VALUES ('".mysql_real_escape_string($_POST['authorname'])."','$now','".mysql_real_escape_string($_POST['newstitle'])."','".mysql_real_escape_string($_POST['news'])."','$ip')") or die (mysql_error());
	echo 'News Submitted In Database';
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/127415-couple-of-problems/#findComment-659084
Share on other sites

This is was the insert looks like

 

 

$update = mysql_query("INSERT INTO `news` (authorname,date,newstitle,news,ip) VALUES ('".mysql_real_escape_string($_POST['authorname'])."','$now','".mysql_real_escape_string($_POST['newstitle'])."','".mysql_real_escape_string($_POST['news'])."','$ip')") or die (mysql_error());

 

 

 

 

 

echo 'News Submitted In Database';

 

 

Link to comment
https://forums.phpfreaks.com/topic/127415-couple-of-problems/#findComment-659282
Share on other sites

Replace what you just wrote with:

$authorname = mysql_real_escape_string($_POST['authorname']);
$newstitle = mysql_real_escape_string($_POST['newstitle']);
$news = mysql_real_escape_string($_POST['news']);

$update = mysql_query("INSERT INTO `news` (authorname,date,newstitle,news,ip) VALUES ("$authorname","$now","$newstitle","$news","$ip")") or die (mysql_error());

echo 'News Submitted In Database';

Link to comment
https://forums.phpfreaks.com/topic/127415-couple-of-problems/#findComment-659442
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.