Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts

    Comments Not Inserted Into Database

    I've looked around my code multiple times but can't find any problematic areas. It doesn't help that there are no error messages either. I don't know, many I'm overlooking something. Anyway, the problem is...well that the script isn't working right. Not too sure what the exact problem is. I mean, nothing is inserted into the database table. No empty columns are inserted, just nothingness. When I insert comments manually, there are displayed correctly, so its just something with the inserting. What baffles me is this is almost exactly the same code used on another page where the commenting is working properly.

    PHP Code:
    if ($_POST['submitted'] == 1) {
        if (
    function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
            
    $_POST['name'] = stripslashes($_POST['name']);
            
    $_POST['comment'] = stripslashes($_POST['comment']);
            
    $_POST['reply'] = stripslashes($_POST['reply']);
        }
    $username mysql_real_escape_string($_POST['name']);
    $comment mysql_real_escape_string($_POST['comment']);
    $category 'news';
    $subcategory $article['id'];
    $reply mysql_real_escape_string($_POST['reply']);
    if (empty(
    $username)) {
    echo 
    "<p>Name is a required field</p>";
    exit();
    }
    if (empty(
    $comment)) {
    echo 
    "<p>Comment is a required field</p>";
    exit();
    }

    $sql "INSERT INTO `comments` (`username`, `comment`, `date`, `category`, `subcategory`, `reply_to`) VALUES ('$username','$comment', NOW(), '$category', '$subcategory', '$reply')";

    if (!
    mysql_query($sql)) {
      die(
    'Error: ' mysql_error());
      } 
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Do you just get a white blank page when you run it?
    Are you sure that error reporting is enabled ... so you don't just get a white page when it fails?

    Add the lines in red ... after the query to make sure it's getting to that part.

    if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
    }

    echo "finished";
    exit;




    .

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by mlseim View Post
    Do you just get a white blank page when you run it?
    Are you sure that error reporting is enabled ... so you don't just get a white page when it fails?

    Add the lines in red ... after the query to make sure it's getting to that part.

    if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
    }

    echo "finished";
    exit;
    No, the design is completely intact. No error messages, no blank white pages, nothing. Just checked to make sure error reporting was enabled and it is. As for the debugging code, it isn't echoing that so...Come to think of it I did do something similar to check earlier and my variables weren't echoed either. Checked my variables' values and even the $_POST values weren't echoed. I should also mention I'm using $_GET values as well so each article in the database gets its own page, in case that might have anything to do with it.
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #4
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    923
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Instead of using NOW() to insert a date into the database, try creating a PHP timestamp, format it as you need it then try inserting the variable.

    In my PHP book, it shows in one example to insert using now() but when I tried it, it didn't work. Soon as I created a php timestamp, it worked.

    Just a thought.

    Also, what is different in this script to your last script?

    Regards,

    LC.

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Just eliminating obvious things:

    If the 'date' field is a time-stamp field that it doesn't require assigning NOW().
    You haven't just omitted the final closing bracket } ?
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by LearningCoder View Post
    Instead of using NOW() to insert a date into the database, try creating a PHP timestamp, format it as you need it then try inserting the variable.

    In my PHP book, it shows in one example to insert using now() but when I tried it, it didn't work. Soon as I created a php timestamp, it worked.

    Just a thought.

    Also, what is different in this script to your last script?

    Regards,

    LC.
    Not much is different. The $subcategory variable was added in this script, and this one is all php. The original was almost entirely static data and so I didn't need the entire page in php tags. Like I said, its almost exactly the same. As for your timestamp trick, nothing...
    Quote Originally Posted by AndrewGSW View Post
    Just eliminating obvious things:

    If the 'date' field is a time-stamp field that it doesn't require assigning NOW().
    You haven't just omitted the final closing bracket } ?
    Nope, all brackets are properly closed... and no the 'date' field is a datetime, kinda need it to be in order to work properly with an 'agoTime()' function.
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    This line:
    if ($_POST['submitted'] == 1) {

    Maybe that "if" statement is false and is skipping over everything?

    Temporarily change it to this, so it's always true:
    if ($x=1) {

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Posted-data are initially strings, or arrays of strings:

    PHP Code:
    if ($_POST['submitted'] && $_POST['submitted'] == '1') { 
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #9
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by mlseim View Post
    This line:
    if ($_POST['submitted'] == 1) {

    Maybe that "if" statement is false and is skipping over everything?

    Temporarily change it to this, so it's always true:
    if ($x=1) {
    hmm, well now its echoing "Name is a required field". So, I guess that's a start.
    Quote Originally Posted by AndrewGSW View Post
    Posted-data are initially strings, or arrays of strings:

    PHP Code:
    if ($_POST['submitted'] && $_POST['submitted'] == '1') { 
    nothing...
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #10
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    PHP Code:
    $subcategory $article['id']; 
    Where is this $article array coming from and does it contain correct values? use print_r() to display it. Use View Source on the page to read it more easily.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #11
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by AndrewGSW View Post
    PHP Code:
    $subcategory $article['id']; 
    Where is this $article array coming from and does it contain correct values? use print_r() to display it. Use View Source on the page to read it more easily.
    PHP Code:
    <?php 
        
    //News articles start
        
    $news mysql_query("SELECT * FROM `news` ORDER BY `date` DESC LIMIT 10");
        while(
    $article mysql_fetch_array($news)) { 
            
    $articlePosted $article['date'];
            
    $datePosted date("F jS, Y"strtotime($articlePosted));
            if (empty(
    $_GET['id'])) {
    database. And yes its values are correct. Later on in the code, $article is echoed just fine.
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #12
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You are using $article to fetch rows from the database. When there are no more rows, $article will be false.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #13
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    yes and? I'm sorry but I seemed to have missed your point.
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #14
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    You say that everything works if you manually enter your querys, but when you have the script insert it, it doesn't work?

    You have found the problem then.

    Echo your query statement, instead of executing it, and post it here. It is 100% a syntax error, if what you're saying is true.

    Edit:

    True not 100% - directly atleast - any problem with his variables is a problem with his query syntax aswell
    Last edited by TFlan; 01-09-2013 at 04:16 AM.

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, there is no guarantee that there exists a syntactical error with the SQL statement. This alone could be a problem here though: '$subcategory' as it stands out given that its populated from an 'id' could possibly be an integer. MySQL can be configured to strict datatyping.

    Given the check on the mysql_query function doesn't produce an error report with the mysql_error indicates to me its not syntactical. My take on the description is that it works when manually entered (presumably using a query directly on the client or with something like PHPMyAdmin).

    Based on what I see here and what the described output is, my first suggestion is to open your error reporting up:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    or check your error logs. Any issue indicating a connection failure with the database? Mysql_error when there is no possible error should produce an empty string.

    If not, post more code. This isn't by itself (we can see a syntactical fault with a missing end brace if it were by itself), so we cannot tell looking at this that its even possible to reach this branch statement.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •