Need some PHP help

clamum

Lifer
Feb 13, 2003
26,252
403
126
I have a form where I gather some user input and when the user presses submit, the info is stored in a MySQL database. This is done on one .php page (if-else statement to decide if the form was submitted or not).

Now after the info is inserted into the database, I'd like that page to refresh so the user can instantly see the newly inserted data. Problem is, I can't get it to work right. After the query, I tried echo'ing some JavaScript with a "window.location.href" statement and then tried a PHP "header('Location: xxx');" but neither worked and the error I got was a MySQL error, which sorta confused me.

Any suggestions? Basically what I want is how the comment form at pics.bbzzdd.com works. Click "Comments", then see existing ones and at the bottom is a form. Enter comment, click submit, and popup refreshes and you see your comment.

BTW: This is the MySQL error I got: Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
 

Alone

Diamond Member
Nov 19, 2006
7,490
0
0
This happens automatically with form submitted information.

/* MySQL Statement to get the comments would go here.*/
if(submit)
{
// mysql_query = // Insert new comments
}
/* Form goes here. */

EDIT:

Your code:
// sql query to insert comment into table
$sql = "INSERT INTO blah blah blah";

// perform query
$result = mysql_query($sql);

// invalid query
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// query was performed with no errors
else {
header("Location: http://www.meatspin.com/");
}
The problem:

INSERT INTO
Insert what?
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
I guess I'm not sure why it's entering the if statement part when the headers are re-sent, I mean wouldn't that clear out the $_POST array? That's how I'm checking if the form was submitted (array_key_exists('_submit', $_POST)).
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
when you send a header location, follow it with a die() or an exit(), preferably with an optional link to the new destination (in case the users browser ignores the redirect)
the parser doesn't stop at the header function.

also, i don't see an array_key_exists in your code, can you post a more complete example?
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Originally posted by: troytime
when you send a header location, follow it with a die() or an exit(), preferably with an optional link to the new destination (in case the users browser ignores the redirect)
the parser doesn't stop at the header function.

also, i don't see an array_key_exists in your code, can you post a more complete example?
Yeah I forgot to add a die() after the header redirection... I did and still got the same error. Here is what I have:
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
the insert query is erroring right?

check all insert values for length before inserting, especially if any of the db tables are NOT NULL
 

txrandom

Diamond Member
Aug 15, 2004
3,773
0
71
You can use HTML, I think it may have to go inside <head> </head>.

<META HTTP-EQUIV='Refresh' CONTENT='0; URL=comments.php'>

Change the time and actual URL.

I'm not sure how your comments is laid out but say it's laid out like this:

Comment #1

Comment #2

Comment #3

Add A Comment - Submit

Now the PHP would appear like this:

if submitted {
add comment to database}

Display comments already in database

Display Add your own comment


You understand what I'm saying? PM and I can help you. I'm actually redoing my webpage, and I'm going to do my comments like this.
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Originally posted by: troytime
the insert query is erroring right?

check all insert values for length before inserting, especially if any of the db tables are NOT NULL
Yeah, it's erroring on the insert query but it shouldn't. I mean, without that header() call, and just outputting "Your comment has been added.", it works totally fine. As soon as that header() is added, or the JavaScript location.href is echo'ed out, it gives me that MySQL error.

txrandom: YGPM.
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
so when you submit a comment, it gets inserted without a problem, then the page redirects to itself and the insert query happens again, but with errors?

take the php_self out of the header location and test it with the actual url. if it works without error, the php_self was passing along user submitted data
$_SERVER['script_name'] might work better for ya
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Originally posted by: troytime
so when you submit a comment, it gets inserted without a problem, then the page redirects to itself and the insert query happens again, but with errors?

take the php_self out of the header location and test it with the actual url. if it works without error, the php_self was passing along user submitted data
$_SERVER['script_name'] might work better for ya
Ah sh!t... I know why. I was looking in the wrong spot for the MySQL error. It's not erroring on the insert, it's erroring below where the script stores a variable from the $_GET array and uses that in a select query.

Well, my dumb ass didn't put that variable in the redirect URL. Jeez. Thanks for your help guys!
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |