Misty87 Posted November 30, 2011 Share Posted November 30, 2011 Hello everyone, I'm making a site with PHP script and PHPmyadmin database, i need some help with how to add news and then edit them or delete them on the site, not in the Database(if that is even possible to do it there), so far i can only delete the news, but when i try to add new stuff i just get the error message of my code, which is not very nice of it but hey, i'm a noob at this PHP so i must be doing something wrong. this is the code i have for the current situation. <?php include_once( 'database_functions.php' ); $connectID = connectToDatabase( 'login' ); mysql_set_charset( 'utf8', $connectID ); $categories = mysql_query('SELECT * FROM ref_categories', $connectID); if( @$_POST && @$_POST['submitted'] && ( !@$_GET[ 'id' ] ) ) { $title = ( $_POST[ 'topic_name' ] ); $topic = ( $_POST[ 'topic_desc' ] ); //write to database mysql_query ( "INSERT into news (title, topic) VALUES ('$title', '$topic')", $connectID ) or die ("Unable to insert record into database"); print "Record successfully added"; } elseif( !$_POST && $_GET && $_GET['id'] ) { $id = $_GET['id']; $thisRecord = mysql_query("SELECT * FROM ref_categories WHERE id = $id", $connectID ) or die( "Can't Read this record." ); $recordData = mysql_fetch_array( $thisRecord, MYSQL_ASSOC ); } elseif( $_POST && $_POST['submitted'] && ( $_GET['id'] ) ) { $title = ( $_POST[ 'title' ] ); $topic = ( $_POST[ 'topic_desc' ] ); $id = $_GET['id']; $success = mysql_query( "SELECT * FROM news", $connectID ) or die( "Can't Update this record" ); if( $success ) { header( 'Location: links_admin.php?updated=1' ); } } else { } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 30, 2011 Share Posted November 30, 2011 I think you are making your conditions overly complex. For example, what is all of this: if( @$_POST && @$_POST['submitted'] && ( !@$_GET[ 'id' ] ) ) Why are you using the at symbol in there? Anyway, you need to simplify your logic. I suspect that code is the core logic for any editing you want to do for new articles: add, edit or delete - correct? Then you should only be performing one type of operation when the page is called. So, if you can detect that a delete operation is being requested, then you don't need to check that an edit request wasn't submitted. For each type of operation you should only need to check one value. I'll assume that deletes are done by clicking a link and that updates and adds are done via a form POST. I would always pass a variable to determine the operation to be performed (let's say 'op'). So, my delete link might look something like this <a href="somefile.php?op=del&id=12">Delete this news article</a> Then for my form(s) for update and add operations I would create a hidden field with the same name ('op') and would use 'add' or 'edit' as the value. Also, the edit form would require a hidden field for the 'id'. Then the page that handles all the operations can simply check the value of 'op' to determine what logic to perform if(isset($_POST['op'])) { $op = $_POST['op']; } elseif(isset($_GET['op'])) { $op = $_GET['op']; } else { $op = false; } if($op=='del') { //Perform delete operations } elseif($op=='edit') { //Perform Edit operations }elseif($op=='add') { //Perform Add operations } else { //No valid operation passed. Display error or display news list } Quote Link to comment Share on other sites More sharing options...
Misty87 Posted November 30, 2011 Author Share Posted November 30, 2011 thanks, this was great. I'm using the @ because i had a undefined whatever error, and putting that @ there took it away (this is something my teacher told me to do). again big thanks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 30, 2011 Share Posted November 30, 2011 I'm using the @ because i had a undefined whatever error, and putting that @ there took it away (this is something my teacher told me to do). Hmm, I hate to state something contrary to your instructor, but that's a very poor 'solution' in my book. You should almost never 'mask' errors. You can simply use the isset() function to determine if a variable is set before trying to reference it. Look at the first section of code I posted. I didn't use if($_POST['op']) or if(@$_POST['op']), I used isset() to determine if the variable was set. The logic would use isset() to determine what value to use then set a value for $op. So, in the rest of the script you can use $op and not have to worry about suppressing errors. By suppressing errors you are going to create more headaches for yourself. You might have a situation, for example, where you are 'certain' that all your variables have values and something doesn't work. You are not going to get any errors that would have identified the problem so you will need to go through a tedious debugging process that may take a couple minutes or even hours depending on the situation. But, that's just my opinion. You can make your own choice. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted November 30, 2011 Share Posted November 30, 2011 I'm using the @ because i had a undefined whatever error, and putting that @ there took it away (this is something my teacher told me to do). Hmm, I hate to state something contrary to your instructor, but that's a very poor 'solution' in my book. You should almost never 'mask' errors. I have to agree totally with mjdamato, here.... bugs the hell out of me! seeing them 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.