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.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Stuck on an INSERT..

    I feel really dumb for asking this.

    I'm still fairly new to coding in PHP so I'm a little confused here: Simply put, I'm trying to insert data into a table from a simple form but I can't get it to work. I've tried everything I can think of and I still don't understand.. Can anyone see an obvious error in the code?

    PHP Code:
    if ($_POST['do'] == 'insertnewcomment') {
        
    globalize($_POST, array('comment' => INT,'title' => STR_NOHTML));
        
        
    $DB_site->query("
        INSERT INTO "
    .TABLE_PREFIX."usercomment (userid,postedbyid,comment,title,dateline) 
        VALUES ("
    .$userinfo[userid].",".$bbuserinfo[userid].",".mysql_escape_string($comment)."',".mysql_escape_string($title)."',".TIMENOW."')");
            
    //Redirect to Member Profile
        
    $url "member.php?$session[sessionurl]u=$userinfo[userid]";
        eval(
    print_standard_redirect('redirect_postthanks'));


    Code:
    <form action="member.php" method="post">
    	<input type="hidden" name="do" value="insertnewcomment" />
    	<input type="hidden" name="userid" />
    	<input type="hidden" name="postedbyid" />
    	<input type="hidden" name="s" value="$session[sessionhash]" />
    	<input class="bginput" type="text" name="title" size="45" maxlength="80" /><br />
    	<input class="bginput" type="text" name="comment" size="45" maxlength="80" /><br />
    		<input type="submit" class="button" value="Submit" accesskey="s" />
    		</form>
    Thanks,

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    First thing that popped out at me was a missing space here.

    INSERT INTO ".TABLE_PREFIX."usercomment
    should be
    INSERT INTO ".TABLE_PREFIX." usercomment

    Next thing was that I personally wouldn't name anything something that is a keyword in any language I might use. That includes "do".

    Other than that, when you get an error, it helps us if you post it.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think maybe the space is meant to be missing, since the prefix and the string form the name of the field, eg if TABLE_PREFIX was "delinear_" then the query would be INSERT INTO delinear_usercomment - so putting in a space would break it. But I agree, if there's an error show us what it says, it helps massively to debug what's going wrong and where.

  • #4
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for not getting back.

    There actually wasn't an error at all, it just won't insert into the database at all.

    I have slightly modified the code since this post (I'll post it as soon as I get home and have access to it) but it's still not inserting any data.

    Thanks,

  • #5
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    I find that when often where there is no error, yet things don't happen, it's often the fault of an IF that I thought would evaluate to true, but didn't.

    See what this actually evaluates to:
    if ($_POST['do'] == 'insertnewcomment')

    Maybe the insert statement isn't even running.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

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