Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-04-2013, 09:13 PM   PM User | #1
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 321
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
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
elitis is offline   Reply With Quote
Old 01-04-2013, 11:21 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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;




.
mlseim is offline   Reply With Quote
Old 01-05-2013, 12:30 AM   PM User | #3
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 321
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
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
elitis is offline   Reply With Quote
Old 01-05-2013, 12:39 AM   PM User | #4
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 860
Thanks: 68
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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.
__________________
Carewizard - http://www.carewizard.co.uk
LearningCoder is offline   Reply With Quote
Old 01-05-2013, 12:58 AM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 01-05-2013, 04:23 AM   PM User | #6
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 321
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
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
elitis is offline   Reply With Quote
Old 01-05-2013, 05:35 PM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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) {
mlseim is offline   Reply With Quote
Old 01-05-2013, 05:54 PM   PM User | #8
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 01-05-2013, 11:43 PM   PM User | #9
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 321
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
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
elitis is offline   Reply With Quote
Old 01-05-2013, 11:51 PM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 01-06-2013, 12:06 AM   PM User | #11
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 321
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
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
elitis is offline   Reply With Quote
Old 01-06-2013, 01:28 AM   PM User | #12
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 01-08-2013, 09:37 PM   PM User | #13
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 321
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
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
elitis is offline   Reply With Quote
Old 01-08-2013, 09:45 PM   PM User | #14
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
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..
TFlan is offline   Reply With Quote
Old 01-08-2013, 10:13 PM   PM User | #15
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:39 AM.


Advertisement
Log in to turn off these ads.