Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 PHP post-db insert error
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DanShadow




PostPosted: Sat Oct 23, 2010 10:27 pm   Post subject: PHP post-db insert error

Hey all.

I'm having a weird issue after using a MySQL insert statement, where rather than rendering the web-site, it only paints the screen black, with no text (even though if you 'View Source', the correct output is there, it just doesnt display on screen).

[quote="code']
"INSERT INTO comments (postid, posteruserid, message, commentdate, commenttime) VALUES ('" . $_SESSION['my_uid'] . "', '" . $trim($_POST['pid']) . "', '" . $comment . "', '" . date("Y-m-d") . "', NOW())"
[/code]

Has' anybody ran into anything like that before?

The only way I can fix it is by removing the insert statement from the code.
I've tried running an "alert()' JS in the same if statement right before the SQL insertion, and it works.
There are no exit() calls around.
Sponsor
Sponsor
Sponsor
sponsor
yoursecretninja




PostPosted: Sat Oct 23, 2010 10:43 pm   Post subject: Re: PHP post-db insert error

Based on what I see, the problem is with your insert statement. You have a typo. trim is a function. Change $trim($_POST['pid']) to trim($_POST['pid']).

JavaScript is really not a helpful way of debugging PHP since JavaScript is executed client side and PHP is executed server side. By placing an alert inside a control block, all you are confirming is what condition was met. You could have just checked the value of the variable(s) used for your control.

When debugging PHP, turn error reporting on. It will tell you some but not all problems, such as fatal errors on database inserts or syntax errors. If need to know the value of a variable (or the contents of an array) use the print_r function and pass in the variable or array as a parameter. Make sure to wrap the output in a <pre> html tag. These are probably the two most valuable tools when debugging PHP scripts.

Here's a reference for error reporting: http://php.net/manual/en/function.error-reporting.php
Here's a reference for print_r: http://php.net/manual/en/function.print-r.php

Hope that helps!
DanShadow




PostPosted: Sun Oct 24, 2010 7:40 am   Post subject: RE:PHP post-db insert error

Haha, that definitely fixed my problem, thanks!

Funny, i went over that insert statement about 5 times and didnt come up with that syntax error >.>
yoursecretninja




PostPosted: Sun Oct 24, 2010 12:20 pm   Post subject: Re: PHP post-db insert error

That happens to me sometimes too. Funny how you can stare at your code so much that the little things can escape you sometimes Smile
DanShadow




PostPosted: Sun Oct 24, 2010 1:48 pm   Post subject: RE:PHP post-db insert error

Yeah defintely, lol. Sucks when its only 1 character, and the program still runs >.<
DanShadow




PostPosted: Sun Oct 24, 2010 2:55 pm   Post subject: RE:PHP post-db insert error

I was wondering for all those more experienced in Database usage than I...

I have nearly a dozen tables in my database, and I find myself doing a lot of SELECT * FROM table WHERE ... statements to get specific data.
Would it be quicker to just do a dozen SELECT * FROM table statements (and parse them in code), or to continue doing seperate SELECT statements specifying WHERE arguments???
DemonWasp




PostPosted: Sun Oct 24, 2010 3:24 pm   Post subject: RE:PHP post-db insert error

Using "SELECT * FROM table" returns every row in the table, whereas "SELECT * FROM table WHERE condition" returns only rows that match that condition. Generally, you will encounter a bottleneck between your database and your application if you select all the rows of a large enough table, whereas conditionally selecting only the ones you want will not bottleneck.

If you can get several pieces of data at the same time, you should do that. This often becomes possible by using JOIN to gather data from multiple tables in the same query.
DanShadow




PostPosted: Sun Oct 24, 2010 5:13 pm   Post subject: RE:PHP post-db insert error

kk, thanks for the advice Smile
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: