PHP and MySQL trouble.
Aug. 25th, 2006 10:38 amSo, I have this PHP code, talking to an SQL database.
The first part creates a text box with a button. When you put something in the box and click the button, it inserts it into an SQL database. The second half, after the form, reads all entries in that SQL database and displays them.
The second part works perfectly.
The first part fails with absolutely no errors - it just never adds anything to the table.
This example is RIGHT out of the book, literally.
Any PHP geeks out there who can tell me what the hell I'm doing wrong?
And yes, I've munged the brackets. Setting those back to angles is trivial. Making Livejournal obey a PRE tag is not trivial.
[html]
[?php
if ($comment){
mysql_connect("localhost","USERNAME","PASSWORD" )
or die("Could not connect to MySQL as USERNAME");
@mysql_select_db("test_database")
or die("could not select the test_database");
$string="INSERT INTO comment_table VALUES ('0','$comment')";
mysql_query($string)
or die(mysql_error( ));}
?]
[? echo $PHP_SELF ?]
[form action="[? echo $PHP_SELF ?]" method="get"]
[input type="text" name="comment" size="80"] [br]
[input type="submit"]
[/form]
[?php
$conn=mysql_connect("localhost","USERNAME","PASSWORD" )
or die("Could not connect to MySQL as USERNAME");
mysql_select_db("test_database", $conn)
or die("could not select the test_database");
$string="SELECT * FROM comment_table";
$result= mysql_query($string)
or die(mysql_error( ));
$numbers_cols= mysql_num_fields($result);
print "[b]query: $string[/b]";
print "[table border =1]\n";
print "[tr]";
print "[td] ID[/td]";
print "[td] Comment [/td]";
print "[/tr]";
while (list($id,$comment) = mysql_fetch_array($result)){
print "[tr]";
print "[td]$id[/td]";
print "[td]$comment[/td]";
print "[/tr]";}
print "[/table]";
?]
[/html]
EDIT: I've found the problem, I think. $PHP_SELF is not working. Now I just need to fix it...
EDIT THE SEQUEL: Fixed that, but still can't submit data. At least I'm getting an error, now.
EDIT PART THREE: ELECTRIC BOOGALOO: Fixed. The problem is that modern PHP can't just read comments submitted through the form by default, and you need to use $_REQUEST("comment") to grab the data that's fed into the form from a form named "comment"
The first part creates a text box with a button. When you put something in the box and click the button, it inserts it into an SQL database. The second half, after the form, reads all entries in that SQL database and displays them.
The second part works perfectly.
The first part fails with absolutely no errors - it just never adds anything to the table.
This example is RIGHT out of the book, literally.
Any PHP geeks out there who can tell me what the hell I'm doing wrong?
And yes, I've munged the brackets. Setting those back to angles is trivial. Making Livejournal obey a PRE tag is not trivial.
[html]
[?php
if ($comment){
mysql_connect("localhost","USERNAME","PASSWORD" )
or die("Could not connect to MySQL as USERNAME");
@mysql_select_db("test_database")
or die("could not select the test_database");
$string="INSERT INTO comment_table VALUES ('0','$comment')";
mysql_query($string)
or die(mysql_error( ));}
?]
[? echo $PHP_SELF ?]
[form action="[? echo $PHP_SELF ?]" method="get"]
[input type="text" name="comment" size="80"] [br]
[input type="submit"]
[/form]
[?php
$conn=mysql_connect("localhost","USERNAME","PASSWORD" )
or die("Could not connect to MySQL as USERNAME");
mysql_select_db("test_database", $conn)
or die("could not select the test_database");
$string="SELECT * FROM comment_table";
$result= mysql_query($string)
or die(mysql_error( ));
$numbers_cols= mysql_num_fields($result);
print "[b]query: $string[/b]";
print "[table border =1]\n";
print "[tr]";
print "[td] ID[/td]";
print "[td] Comment [/td]";
print "[/tr]";
while (list($id,$comment) = mysql_fetch_array($result)){
print "[tr]";
print "[td]$id[/td]";
print "[td]$comment[/td]";
print "[/tr]";}
print "[/table]";
?]
[/html]
EDIT: I've found the problem, I think. $PHP_SELF is not working. Now I just need to fix it...
EDIT THE SEQUEL: Fixed that, but still can't submit data. At least I'm getting an error, now.
EDIT PART THREE: ELECTRIC BOOGALOO: Fixed. The problem is that modern PHP can't just read comments submitted through the form by default, and you need to use $_REQUEST("comment") to grab the data that's fed into the form from a form named "comment"
(no subject)
Date: 2006-08-25 02:48 pm (UTC)@mysql_select_db("test_database")
The @ sign suppresses errors from the function. Try removing it and see if you get an error.
You could also try:
error_reporting(E_ALL);
for maximum error/warning levels.
As far as "best practices" go, the return values of ALL functions should be checked for errors. But I'll be the first to admit that I do not do this 100% of the time.
D'oh!
Date: 2006-08-25 02:50 pm (UTC)I just noticed the "or die" clause, which does error checking. Silly me.
I'd still suggest turning up error reporting, though.
(no subject)
Date: 2006-08-25 03:05 pm (UTC)Notice: Undefined index: comment in /var/www/test.php on line 4
Notice: Undefined variable: PHP_SELF in /var/www/test.php on line 18
(no subject)
Date: 2006-08-25 03:11 pm (UTC)Okay, those aren't a big deal. Like the text says that're just notices. PHP's equivilent of saying, "this isn't 100% correct and you may want to fix it, but it's not a deal breaker either".
register globals may be off
Date: 2006-08-25 02:52 pm (UTC)Try this:
if ($_REQUEST["comment"])
The bonus with $_REQUEST and friends is that you can use print_r() on them to get a dump of everything that was submitted for debugging purposes.
Re: register globals may be off
Date: 2006-08-25 03:01 pm (UTC)"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"
Re: register globals may be off
Date: 2006-08-25 03:09 pm (UTC)$string="INSERT INTO comment_table VALUES ('0','$comment')";
Are there any parenthesis in your comment? That would bork the query. You can avoid this by using mysql_real_escape_string() (http://us3.php.net/manual/en/function.mysql-real-escape-string.php):
$string="INSERT INTO comment_table VALUES ('0', '" . mysql_real_escape_string($comment) . "')";
Also, from a security standpoint, I should warn you that ANY input coming from the outside world should be considered "untrusted" by default and sanitized in this manner. Otherwise you'll leave yourself wide open to a SQL injection attack.
If you want to see the query that's being choked on, you could also put an "echo $string" in there to see what query is being sent.
Re: register globals may be off
Date: 2006-08-25 03:31 pm (UTC)Security: Thanks, but at the moment, I'm not worried about that. I'm just trying to make it *work* on a system that has no internet access, and *then* I'll worry about making it safe. Noted for the record, though.
Re: register globals may be off
Date: 2006-08-25 03:33 pm (UTC)If no luck, can you echo the query and paste it here?
Re: register globals may be off
Date: 2006-08-25 03:38 pm (UTC)INSERT INTO comment_table VALUES (,'')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 1So, I think we see the problem.
Re: register globals may be off
Date: 2006-08-25 03:42 pm (UTC)$string="INSERT INTO comment_table VALUES ('0','".$comment."')";
or some such, to get the value of $comment put into its place.
Re: register globals may be off
Date: 2006-08-25 03:49 pm (UTC)Re: register globals may be off
Date: 2006-08-25 03:44 pm (UTC)THIS:
mysql_query("INSERT INTO comment_table VALUES (0,'Totally, dude!')")
or die(mysql_error( ));
Works fine. It adds the comment "totally, dude" at place X, where X is the maximum current index plus one (because the 0 is taken and it's set to auto-increment.
The problem is that $comment is not being read. It doesn't know what $comment is.
Re: register globals may be off
Date: 2006-08-25 03:45 pm (UTC)Re: register globals may be off
Date: 2006-08-25 03:46 pm (UTC)Re: register globals may be off
Date: 2006-08-25 03:49 pm (UTC)Just to clarify: anything you submit from a form will be in the $_REQUEST associative array.
More information on why the old method or just using variable names for form elements will not work anymore: http://us2.php.net/register_globals
Re: register globals may be off
Date: 2006-08-25 03:52 pm (UTC)Re: register globals may be off
Date: 2006-08-25 03:54 pm (UTC)That being said, it's still my favorite language to program in.
Re: register globals may be off
Date: 2006-08-25 03:48 pm (UTC)Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/test.php on line 12
Re: register globals may be off
Date: 2006-08-25 03:51 pm (UTC)$string = "INSERT INTO comment_table VALUES ('0', "
. "'" . mysql_real_escape_string($_REQUEST["comment"]) . "'"
. ")";
Re: register globals may be off
Date: 2006-08-25 03:54 pm (UTC)Re: register globals may be off
Date: 2006-08-25 03:51 pm (UTC)Re: register globals may be off
Date: 2006-08-25 03:53 pm (UTC)$comment = $_REQUEST["comment"];
Re: register globals may be off
Date: 2006-08-25 04:04 pm (UTC)I think magic_quotes is on. Now I need to find out how to turn it off.
Re: register globals may be off
Date: 2006-08-25 04:05 pm (UTC)SantaSatan.http://us3.php.net/manual/en/security.magicquotes.disabling.php
Re: register globals may be off
Date: 2006-08-25 04:11 pm (UTC)(no subject)
Date: 2006-08-25 03:01 pm (UTC)No semicolons following the echo statements?
<?echo $PHP_SELF; ?>(no subject)
Date: 2006-08-25 03:04 pm (UTC)(no subject)
Date: 2006-08-25 03:03 pm (UTC)You do realize that all the added values have the same ID? If the ID is a primary key, this might wreak merry havoc.
In the second example, you use the resource link identifier in the select_db function; in the first, you don't. Maybe you should put it in [you know, $conn = mysql_connect() and then select_db(name, $conn)] and see if it helps.
(no subject)
Date: 2006-08-25 03:05 pm (UTC)I'll try that.
(no subject)
Date: 2006-08-25 03:17 pm (UTC)Try saying
$string="INSERT INTO comment_table (comment) VALUES '$comment'";
assuming the name of the second column is comment. That'll engage autoincrementing.
Why this isn't showing the error in the or die() statement, dunno.
(no subject)
Date: 2006-08-25 03:36 pm (UTC)mysql> CREATE TABLE comment_table(
-> id INT NOT NULL auto_increment,
-> comment TEXT,
-> PRIMARY KEY(id));
I can add and remove things from here using SQL commands inside SQL or from the linux command line all day long, and it works perfectly. It's only through the PHP that it's failing.
(no subject)
Date: 2006-08-25 03:39 pm (UTC)(no subject)
Date: 2006-08-25 03:46 pm (UTC)It's the problem of running into three different new things all at once. I've never used PHP before, or mySQL, or had to make HTML forms talk to them.
(no subject)
Date: 2006-08-25 03:29 pm (UTC)(no subject)
Date: 2006-08-25 03:32 pm (UTC)(no subject)
Date: 2006-08-25 03:34 pm (UTC)(no subject)
Date: 2006-08-25 04:23 pm (UTC)