divinedesigns1 Posted June 19, 2012 Share Posted June 19, 2012 i cant seem to grab the information from the database and display it in the form, to be edit then put it back into the database, yes i asked this question a while ago ( cant fine the post i made for it ) anyway, i would like some help on this page.this is the code im using at the moment, how it suppose to work is when you hit the edit button, on the administrator page it suppose to pull up the information from the database and when your finish it suppose to update the information into the database, but its not doing as it suppose too. and i keep getting this error Notice: Undefined index: id in C:\wamp\www\news\edit_news.php on line 3 even if i placed the 2 variable below the include fine<?phpinclude_once('MyConnect.php');if((isset($_GET['id'])) && (is_numeric($_POST['id']))){ $id = $_GET['id']; $query = "SELECT * FROM news WHERE id='$id'"; $result = mysqli_query($con, $query); }elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))){ $query = "SELECT * FROM news WHERE id='$id'"; $result = mysqli_query($con, $query); $id = $_POST['id'];}else{ echo 'Please choose a news post to edit'; exit();}if(isset($_POST['submitted'])){ $error = array(); if(empty($_POST['title'])){ $error[] = 'You forgot to enter a title. '; }else{ $title = $_POST['title']; } if(empty($_POST['name'])){ $error[] = 'You forgot to enter an author.'; }else{ $name = $_POST['name']; } if(empty($_POST['message'])){ $error[] = 'You forgot to enter a message'; }else{ $message = $_POST['message']; } if(empty($errors)){ $query = "UPDATE news SET title='$title', author='$name', post='$message' WHERE id=$id"; $result = mysqli_query($con, $query); if($result){ echo "News post has been updated!"; }else{ echo 'News post could not be updated.'; } }else{ echo 'news post could not be updated for the following resions -<br />'; foreach($errors as $msg){ echo "$msg<br />\n"; } }}else{ $query = "SELECT title, author, post, id FROM news WHERE id=$id"; $result = mysqli_query($con, $query); $num = mysqli_num_rows($result); $row = mysqli_fetch_array($result, MYSQL_NUM); if($num == 1){ echo '<h3>Edit news post</h3> <form action="?id=edit_news$num='.$id.'" method="post"> <p>News Title: <input type="text" name="title" size="25" maxlength="255" value="'.$title.'" /></p> <p>Name: <input type="text" name="name" size="15" maxlength="255" value="'.$name.'" /></p> <p>Message:<br /><textarea row="5" cols="40" name="message">'.$message.'</textarea></p> <p><input type="submit" name="submit" vaule="submit" /></p> <input type="hidden" name="id" value="'.$id.'" />'; }else{ echo 'news post could not be edited, please try again.'; }}?> Link to comment Share on other sites More sharing options...
boen_robot Posted June 19, 2012 Share Posted June 19, 2012 You're using both $_GET['id'] and $_POST['id'] on the line if((isset($_GET['id'])) && (is_numeric($_POST['id']))){ Changes are only one of them is defined, which is why you get the notice. Pick the correct one, and replace all references to the other with it.Also, }elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))){ $query = "SELECT * FROM news WHERE id='$id'"; $result = mysqli_query($con, $query); $id = $_POST['id']; The ID should be defined before the query, so: }elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))){ $id = $_POST['id']; $query = "SELECT * FROM news WHERE id='$id'"; $result = mysqli_query($con, $query); P.S. mysqli_real_escape_string()? Link to comment Share on other sites More sharing options...
divinedesigns1 Posted June 19, 2012 Author Share Posted June 19, 2012 i did that but im still getting ask to pick a topic to edit, im being able to connect to the database no problem.this is the updated code i maked changes too if(isset($_GET['id'])){ $id = $_GET['id']; $query = "SELECT * FROM news WHERE id='$id'"; $result = mysqli_query($con, $query); }elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))){ $id = $_POST['id']; $query = "SELECT * FROM news WHERE id='$id'"; $result = mysqli_query($con, $query);}else{ echo 'Please choose a news post to edit'; exit();} and when i remove the is_numerics im still getting the same message saying to pick a topic to be edited Link to comment Share on other sites More sharing options...
boen_robot Posted June 19, 2012 Share Posted June 19, 2012 And does the URL have "id" in it?Let's try some debugging. At the top, right before the first if, place: var_dump($_GET, $_POST); And see what you're getting. Link to comment Share on other sites More sharing options...
divinedesigns1 Posted June 19, 2012 Author Share Posted June 19, 2012 ok i did that and i got this array emptyarray empty which says nothing is being obtain Link to comment Share on other sites More sharing options...
divinedesigns1 Posted June 19, 2012 Author Share Posted June 19, 2012 whoopz and yes the url have an $row['id'] in it, first thing i looked at since i made that mistake on the index.php page Link to comment Share on other sites More sharing options...
thescientist Posted June 19, 2012 Share Posted June 19, 2012 ok i did that and i got thiswhich says nothing is being obtain right, so nothing is getting passed into the page via $_GET or $_POST. Unless you are loading this page with a url and query string, or submitting it by a form, where do you expect those values to come from? Link to comment Share on other sites More sharing options...
divinedesigns1 Posted June 19, 2012 Author Share Posted June 19, 2012 right, so nothing is getting passed into the page via $_GET or $_POST. Unless you are loading this page with a url and query string, or submitting it by a form, where do you expect those values to come from?the $_GET, suppose to grab the information from the database, then display it into a form on the page where the $_POST submit it to the database back.then that mean i need to fetch that information and display it in a form with a $_POST method. ok let me try that and ill come back Link to comment Share on other sites More sharing options...
thescientist Posted June 19, 2012 Share Posted June 19, 2012 (edited) the $_GET, suppose to grab the information from the database, then display it into a form on the page where the $_POST submit it to the database back.then that mean i need to fetch that information and display it in a form with a $_POST method. ok let me try that and ill come back do you actually understand what $_GET and $_POST are? If you are indeed submitting anything to the page with a form, using GET/POST, then something would be in there. If not, then there is probably something wrong with your form. Edited June 19, 2012 by thescientist Link to comment Share on other sites More sharing options...
divinedesigns1 Posted June 19, 2012 Author Share Posted June 19, 2012 do you actually understand what $_GET and $_POST are? If you are indeed submitting anything to the page with a form, using GET/POST, then something would be in there. If not, then there is probably something wrong with your form.not fully understand what get and post are but i have some understanding about what they do Link to comment Share on other sites More sharing options...
divinedesigns1 Posted June 19, 2012 Author Share Posted June 19, 2012 (edited) how would you write this so it can work echo '<td><textarea rows="5" cols="40" name="message"> . $row[post] . </textarea></td>'; because i was rewriting the code and when i use the fetch statement im able to see the post on third id in the database butttt this part isnt working, so if this work then im going to change the form in the original script Edit: and if i put the single quotes in the $row['post'] i get this error Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\wamp\www\news\edit_news.php on line 20 but i need to keep the single quote Edited June 19, 2012 by DDs1 Link to comment Share on other sites More sharing options...
divinedesigns1 Posted June 19, 2012 Author Share Posted June 19, 2012 nevermind i figure it out now ill go try it on the original script Link to comment Share on other sites More sharing options...
boen_robot Posted June 19, 2012 Share Posted June 19, 2012 $_GET and $_POST don't really "do" anything. They just contain data. You are supposed to make them appear to do something by checking them and making PHP act accordingly.$_GET contains data from the URL's query string. If you call the PHP file like index.php?id=avalue&somethingElse=anotherValue then $_GET will look like the equivalent of $_GET = array('id' => 'avalue', 'somethingElse' => 'anotherValue'); It's a similar thing with $_POST, but instead of the URL, it's the request body. If you have a form like: <form method="post" action="processor.php"><input name="id" value="avalue" /><input name="somethingElse" value="anotherValue" />...</form> Then submitting this form will result in processor.php receiving a $_POST that has contents equivalent to: $_POST = array('id' => 'avalue', 'somethingElse' => 'anotherValue'); Link to comment Share on other sites More sharing options...
divinedesigns1 Posted June 19, 2012 Author Share Posted June 19, 2012 $_GET and $_POST don't really "do" anything. They just contain data. You are supposed to make them appear to do something by checking them and making PHP act accordingly. $_GET contains data from the URL's query string. If you call the PHP file likeindex.php?id=avalue&somethingElse=anotherValue then $_GET will look like the equivalent of $_GET = array('id' => 'avalue', 'somethingElse' => 'anotherValue'); It's a similar thing with $_POST, but instead of the URL, it's the request body. If you have a form like: <form method="post" action="processor.php"><input name="id" value="avalue" /><input name="somethingElse" value="anotherValue" />...</form> Then submitting this form will result in processor.php receiving a $_POST that has contents equivalent to: $_POST = array('id' => 'avalue', 'somethingElse' => 'anotherValue'); oh ok, now i understand, thanks ill write that down also Link to comment Share on other sites More sharing options...
divinedesigns1 Posted June 19, 2012 Author Share Posted June 19, 2012 woot woot i fixed it. thanks boen and thesci now i just need to clean it up a little bit so it can look professional i have achieve 2 things today thanks a million guys Link to comment Share on other sites More sharing options...
divinedesigns1 Posted June 19, 2012 Author Share Posted June 19, 2012 (edited) for those who are following this topic and would like to know how i got it fix, what i did with the help of thesci and boenwas remove the if(num == 1) and change it to if($result) and in the curlbraces i placed a while statement in it which help me remove the variable $row = mysqli_fetch_array($result, MYSQLI_ASSOC)) from outside of the if($result). like this if($result){ while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ echo '<h3>Edit news post</h3> <form action="edit_news.php?id=' . $row['id'] . '" method="post"> <p>News Title: <input type="text" name="title" size="25" maxlength="255" value="'. $row['title'] .'" /></p> <p>Name: <input type="text" name="name" size="15" maxlength="255" value="'. $row['author'] .'" /></p> <p>Message:<br /><textarea row="5" cols="60" name="message">'. $row['post'] .'</textarea></p> <p><input type="submit" name="submit" vaule="submit" /></p> <input type="hidden" name="id" value="'. $row['id'] .'" />'; } and the below code is the original code i had before $row = mysqli_fetch_array($result, MYSQLI_NUM); if($num == 0){ echo '<h3>Edit news post</h3> <form action="?id=edit_news$num='.$id.'" method="post"> <p>News Title: <input type="text" name="title" size="25" maxlength="255" value="'.$title.'" /></p> <p>Name: <input type="text" name="name" size="15" maxlength="255" value="'.$name.'" /></p> <p>Message:<br /><textarea row="5" cols="40" name="message">'.$message.'</textarea></p> <p><input type="submit" name="submit" vaule="submit" /></p> <input type="hidden" name="id" value="'.$id.'" />'; } and i remove the is_numeric($_post) from the first and second if statements, and left everything else alone. Edited June 19, 2012 by DDs1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now