Jump to content

ApplicationError

New Members
  • Posts

    9
  • Joined

  • Last visited

ApplicationError's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Quick question.... when I move the $db out of the function.... I get an error. Notice: Undefined variable: db... Same when I try to set it to a global... global $db;
  2. Finally, I got it to work. It was this code that caused the problem: // Go back to page $callingPage = $_SERVER['HTTP_REFERER']; header('location:' . $callingPage); Having figured it out, it makes sense. After GetEmAll_comment.php posted to itself to add the comment, it went back to GetEmAll_comment.php, but at that time there was nothing to post. The code was supposed to push it back to GetEmAll.php. So here is the working code <?php // Gets passed in from GetEmAll.php via form POST $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_STRING); // Gets passed form this file "GetEmAll_Comment.php" via form POST $id_form = filter_input(INPUT_POST, 'id_form', FILTER_SANITIZE_STRING); $comment_form = filter_input(INPUT_POST, 'comment_form', FILTER_SANITIZE_STRING); $callingPage = filter_input(INPUT_POST, 'callingPage_form', FILTER_SANITIZE_STRING); // Output for debugging problem echo "id from GetEmAll.pgp: ".$id."<br/><br/>"; echo "id_form set from this file: ".$id_form."<br/>"; echo "comment_form set from this file: ".$comment_form."<br/>"; $existing_comment = ''; // needs to be defined atleast. // Check if the comment was set (posted) if(isset($comment_form) && isset($id_form)){ // if both $comment_form and $id_form are set it must be submitted from the form // if it is submitted from the form, update the database record addComment($id_form, $comment_form, $callingPage); echo "added: ".$comment_form." to ".$id."<br />"; } elseif (isset($id)) { $existing_comment = checkForComment($id); $callingPage = $_SERVER['HTTP_REFERER']; } elseif (empty($id_form)) { echo 'id_form is empty <br/>'; } // Update the database with the new comment function addComment($id, $comment, $callingPage){ $db = new mysqli('localhost', 'root', '', 'newstoolv1'); $sql = "UPDATE tbl_news SET clmn_comment='".$comment."' WHERE id=".$id; $db->query($sql); $db->close(); // Go back to page header('location:' . $callingPage); } function checkForComment($id){ echo "<br/><br/>checkForComment() Function<br/>"; echo "id: ".$id."<br/><br/>"; $db = new mysqli('localhost', 'root', '', 'newstoolv1'); $sql = "SELECT * FROM tbl_news WHERE id=".$id; $results = $db->query($sql); $db->close(); echo "The SQL statement:<br/>"; echo "sql var: ".$sql."<br/>"; // var_dump($results); $r = ''; while($output_item = $results->fetch_object()){ $r = $output_item->clmn_comment; // echo "Output from DB".$output_item->clmn_comment."<br/>"; } return $r; } ?> <form class="form" method="POST" action="GetEmAll_Comment.php"> <input type="hidden" name="id_form" value="<?PHP echo $id;?>"> <input type="hidden" name="callingPage_form" value="<?PHP echo $callingPage;?>"> <textarea rows="1" cols="60" name="comment_form"><?PHP echo $existing_comment;?></textarea> <input type="submit" value="Submit"> </form>
  3. Thanks, no luck. It is a problem with posting to itself somehow. Really strange, but probably just a logic problem.
  4. This problem is killing me. I could post to yet another PHP file and I think it would work.
  5. Thank you for the help. Interesting, it seems as though the form post doesn't work, or the POST values are not captured. They show up as empty, in which case the SQL quey can't run correctly. I've tried to seperate the form code a bit. <?php $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_STRING); // Gets passed in from GetEmAll.php via form POST // Gets passed form this file "GetEmAll_Comment.php" via form POST $id_form = filter_input(INPUT_POST, 'id_form', FILTER_SANITIZE_STRING); $comment_form = filter_input(INPUT_POST, 'comment_form', FILTER_SANITIZE_STRING); echo "id: ".$id."<br/>"; // echo "comment: ".$comment."<br/>"; echo "id_form: ".$id_form."<br/>"; echo "comment_form: ".$comment_form."<br/>"; // Check if the comment was set (posted) if(isset($comment_form) && isset($id_form)){ // if both $comment_form and $id_form are set it must be submitted from the form // if it is submitted from the form, update the database record addComment($id_form, $comment_form); echo "added: ".$comment_form." to ".$id."<br />"; } else { $existing_comment = checkForComment($id); } // Update the database with the new comment function addComment($id, $comment){ $db = new mysqli('localhost', 'root', '', 'newstoolv1'); $sql = "UPDATE tbl_news SET clmn_comment='".$comment."' WHERE id=".$id; $db->query($sql); $db->close(); // Go back to page $callingPage = $_SERVER['HTTP_REFERER']; header('location:' . $callingPage); } function checkForComment($id){ echo "checkForComment() Function<br/>"; echo "id: ".$id."<br/>"; $db = new mysqli('localhost', 'root', '', 'newstoolv1'); $sql = "SELECT * FROM tbl_news WHERE id=".$id; $results = $db->query($sql); $db->close(); echo "The SQL statement:<br/>"; echo "sql var: ".$sql."<br/>"; var_dump($results); $r = ''; while($output_item = $results->fetch_object()){ // $r = $output_item->clmn_comment; echo "Output from DB".$output_item->clmn_comment."<br/>"; } /* return $r; */ } ?> <form class="form" method="POST" action="GetEmAll_Comment.php"> <input type="hidden" name="id_form" value="<?PHP echo $id;?>"> <textarea rows="1" cols="60" name="comment_form"><?PHP echo $existing_comment;?></textarea> <input type="submit" value="Submit"> </form>
  6. Interesting. Thanks for the feedback... (a) I don't understand why the addComment() function is called when I submit the form, as the $comment should be set - but maybe it isn't, but if it isn't then why does it update the value in the database (B) it looks like when I submit the form, indeed, the $id and $comment are not set... but if they are not set, then how is the new comment added/updated (it is infact updated in the database). Here a little code update... just tried to unset the $comment in one part, hoping it would help to not call addComment(). <?php $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_STRING); $comment = filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_STRING); echo $id; echo $comment; echo "----"; $existing_comment = ''; // Check if the comment was set (posted) if(isset($comment)){ addComment($id, $comment); echo "record should be added"; } elseif (empty($comment)) { $existing_comment = checkForComment($id); } // Update the database with the new comment function addComment($id, $comment){ $db = new mysqli('localhost', 'root', '', 'newstoolv1'); $sql = "UPDATE tbl_news SET clmn_comment='".$comment."' WHERE id=".$id; $db->query($sql); $db->close(); unset($comment); // Go back to page $callingPage = $_SERVER['HTTP_REFERER']; header('location:' . $callingPage); } function checkForComment($id){ $db = new mysqli('localhost', 'root', '', 'newstoolv1'); $sql = "SELECT * FROM tbl_news WHERE id=".$id; $results = $db->query($sql); $db->close(); $r = ''; while($output_item = $results->fetch_object()){ $r = $output_item->clmn_comment; } return $r; } ?> <form class="form" method="POST" action="GetEmAll_Comment.php"> <input type="hidden" name="id" value="<?PHP echo $id;?>"> <textarea rows="1" cols="60" name="comment"><?PHP echo $existing_comment;?></textarea> <input type="submit" value="Submit"> </form>
  7. I have a list of news items in a database, and have a form to add/edit the comment. It all works fine.... loads the existing comment, saves a new comment (of changed comment), but it throws a really strange error message. I am sure it isn't strange for most here Fatal error: Call to a member function fetch_object() on a non-object in C:\wamp\www\NewsToolv1\GetEmAll_Comment.php on line 39 GetEmAll_Comment.php <?php $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_STRING); $comment = filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_STRING); $existing_comment = ''; // Check if the comment was set (posted) if(isset($comment)){ addComment($id, $comment); echo "record should be added"; } elseif (empty($comment)) { $existing_comment = checkForComment($id); } // Update the database with the new comment function addComment($id, $comment){ $db = new mysqli('localhost', 'root', '', 'newstoolv1'); $sql = "UPDATE tbl_news SET clmn_comment='".$comment."' WHERE id=".$id; $db->query($sql); $db->close(); // Go back to page $callingPage = $_SERVER['HTTP_REFERER']; header('location:' . $callingPage); } function checkForComment($id){ $db = new mysqli('localhost', 'root', '', 'newstoolv1'); $sql = "SELECT * FROM tbl_news WHERE id=".$id; $results = $db->query($sql); $db->close(); $r = ''; while($output_item = $results->fetch_object()){ $r = $output_item->clmn_comment; } return $r; } ?> <form class="form" method="POST" action="GetEmAll_Comment.php"> <input type="hidden" name="id" value="<?PHP echo $id;?>"> <textarea rows="1" cols="60" name="comment"><?PHP echo $existing_comment;?></textarea> <input type="submit" value="Submit"> </form>
  8. Good evening all. I have been dabbling in PHP code for many years now, but am certainly no programmer. I change existing code, add it’s of functionality to Word Press, but am certainly no programmer. My new year’s resolution is to really dive into PHP and start developing some more sophisticated applications to make things easier for me (and others). I understand how to set variables, work with arrays, create and use functions, connect to databases, but don’t yet understand the concept of objects. Regular expressions are also still a bit of a struggle. One day at a time. I’ve taught myself HTML, CSS and Photoshop. Have the same level of understanding in C#, so I think I will manage. Time and practice. What would be great is to find a 5 day intensive course, that could really go through the basics. A bootcamp of sorts. This hasn’t been easy to find, if somebody has a recommendation... Also, if somebody has a recommendation on video tutorials / courses...
×
×
  • 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.