topflight Posted October 7, 2008 Share Posted October 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
topflight Posted October 7, 2008 Author Share Posted October 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
Orio Posted October 7, 2008 Share Posted October 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 $result = mysql_query($query) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
topflight Posted October 7, 2008 Author Share Posted October 7, 2008 Thanks it works may you please tell me what u did also how can I stop the script for inserting it into the database even is their is an error Quote Link to comment Share on other sites More sharing options...
topflight Posted October 7, 2008 Author Share Posted October 7, 2008 U did it may you please tell me what u did and how to have more errors display at once u currently had it set up so that is displays one error at a time how do u all the errors? Quote Link to comment Share on other sites More sharing options...
topflight Posted October 7, 2008 Author Share Posted October 7, 2008 Now for some reason it is not inserting into the database. Quote Link to comment Share on other sites More sharing options...
Orio Posted October 7, 2008 Share Posted October 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 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'; } } ?> Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 Using die() terminates the rest of the script. If there's any other footer information, it's not going to be displayed. It's best to avoid using die() in this situation. Quote Link to comment Share on other sites More sharing options...
topflight Posted October 7, 2008 Author Share Posted October 7, 2008 Stil not inserting into the database. Quote Link to comment Share on other sites More sharing options...
Orio Posted October 7, 2008 Share Posted October 7, 2008 Yeah that's why I made it in a seprate function so if he wants to call a footer function or something he could do that. I design forms with errors in a totally different way anyways... Orio. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 Double check the table's structure versus your INSERT query. Also make sure that you have the column types as strings and not integers. Quote Link to comment Share on other sites More sharing options...
topflight Posted October 7, 2008 Author Share Posted October 7, 2008 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'; Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 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'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.