Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsDiscussion Forum → mysql help
12
mysql help
2010-08-17, 3:59 PM #1
hi there.

new install of php5, mysql5, apache 2 running on ubuntu 10.04.
this won't enter anything into my db.

Code:
<?php

$name=$_POST[name];

$user="user";
$password="password";
$database="test";
mysql_connect(localhost, $user, $password) or die(mysql_error("Unable to connect to database"));
mysql_select_db($database) or die(mysql_error("Unable to select database"));
$query= INSERT INTO test(name, status) VALUES ('_$POST[name]', 0);
mysql_query($query);
mysql_close();

echo "thanks for posting."
?>


there are 3 fields in the db. name, number and status. number is auto increment.
i don't get any errors nor does the "thanks for posting" text show up. no records are in the db.
i've fooled around with this too much and i think there may be a problem with my mysql/php install.

any suggestions?
2010-08-17, 4:06 PM #2
Sorry, can't help you but good to see you again, old man!
"I would rather claim to be an uneducated man than be mal-educated and claim to be otherwise." - Wookie 03:16

2010-08-17, 4:10 PM #3
If that's the actual code, then you have errors. Use this:

[PHP]
<?php

$name=$_POST['name'];

$user="user";
$password="password";
$database="test";
mysql_connect(localhost, $user, $password) or die(mysql_error("Unable to connect to database"));
mysql_select_db($database) or die(mysql_error("Unable to select database"));
$query= INSERT INTO test(name, status) VALUES ('$name', 0);
mysql_query($query);
mysql_close();

echo "thanks for posting."
?>
[/PHP]

The $_POST[name] should be $_POST['name']
2010-08-17, 4:10 PM #4
thanks. correct me if i'm wrong but you're the old man aren't you? :p
2010-08-17, 4:12 PM #5
I can't help with the php, but I can delete your database very easily.
Detty. Professional Expert.
Flickr Twitter
2010-08-17, 4:13 PM #6
Oh Darth Evad. What crazy websites are you cooking up this time?
COUCHMAN IS BACK BABY
2010-08-17, 4:15 PM #7
thanks zecks. i've tried that and it didn't work and i just copy and pasted your code and it didn't work either.

is there anything i should look for in my module installs that may not be working? i just installed them as per the instructions (software installer in ubuntu).
2010-08-17, 4:15 PM #8
Are you thinking about SQL injection, Detty?
2010-08-17, 4:16 PM #9
yeah, unless the mysql module does auto-sanitizing now?
Detty. Professional Expert.
Flickr Twitter
2010-08-17, 4:17 PM #10
Originally posted by Darth Evad:
thanks. correct me if i'm wrong but you're the old man aren't you? :p


I AM one of the old men around here but you're a tad older.
"I would rather claim to be an uneducated man than be mal-educated and claim to be otherwise." - Wookie 03:16

2010-08-17, 4:17 PM #11
lol. it's been years since i've actually coded anything and i wasn't very good at it then either. just fooling around with time on my hands.
2010-08-17, 4:17 PM #12
You better stay off of Dave's lawn!
2010-08-17, 4:18 PM #13
Detty: No, it doesn't.

Evad: Have you made sure that proper permissions are set up for the user so it can actually insert data?
2010-08-17, 4:20 PM #14
Use parameterized queries
Bassoon, n. A brazen instrument into which a fool blows out his brains.
2010-08-17, 4:28 PM #15
yes. all privileges.

parameterized queries?

stay off my lawn!
2010-08-17, 4:36 PM #16
Evad: I just found your problem. I completely overlooked that you have die(mysql_error("Some text")); That's incorrect. You could simply use die("Some text"); because mysql_error() is to return the error from MySQL. See if this works:

[PHP]
<?php

$name=$_POST['name'];

$user="user";
$password="password";
$database="test";
mysql_connect(localhost, $user, $password) or die("Unable to connect to database");
mysql_select_db($database) or die("Unable to select database");
$query= INSERT INTO test(name, status) VALUES ('$name', 0);
mysql_query($query);
mysql_close();

echo "thanks for posting."
?>
[/PHP]

Emon: I wasn't aware PHP was able to do parameterized queries until now. It's possible with MySQLi.
2010-08-17, 4:42 PM #17
hmmm... that didn't work either.

i appreciate your help.
2010-08-17, 4:46 PM #18
Haha. I'm blind tonight. You need a ; at the end of the final echo statement.
2010-08-17, 4:51 PM #19
should the query be a string?

ie

"INSERT INTO test(name, status) VALUES ($name);"

or does PHP do that using some weird method too?
Detty. Professional Expert.
Flickr Twitter
2010-08-17, 4:52 PM #20
i was gonna start headbutting babies if that was it.

didn't work.

if i used the package manager to install everything should i assume it's all in the proper directories etc.?
i installed phpmyadmin and it's working fine. i ran the insert query in it and it inserted a row.
2010-08-17, 4:55 PM #21
I really am completely blind tonight and only capable of finding one error at a time. Good God. You forgot some quotation marks in $query. Here:

$query = "INSERT INTO test ( name , status ) VALUES ( '$name' , 0 )";
2010-08-17, 4:55 PM #22
dumb question.
should php, mysql etc be installed in /var/www/ or /etc/ ?
2010-08-17, 4:57 PM #23
zecks, that didn't work either. d'oh.
2010-08-17, 4:58 PM #24
That I am unsure of as it's been quite some time since I've used linux, but I would assume the package manager installed them alright, especially if phpmyadmin is working just fine.
2010-08-17, 5:02 PM #25
Another fix needed (quotes around localhost):

mysql_connect("localhost", $user, $password) or die("Unable to connect to database");
2010-08-17, 5:19 PM #26
Originally posted by Darth Evad:
dumb question.
should php, mysql etc be installed in /var/www/ or /etc/ ?


Neither? Usually it's /usr/bin or somewhere in /usr. /var/www is the place where the webpages live. /etc/ is configs.
2010-08-17, 6:52 PM #27
thanks matty. i'll try that tomorrow.

thanks zecks.
2010-08-17, 7:01 PM #28
Maybe your server is setup to not display errors. Run error_reporting(E_ALL) at the begging of the script.
"Nulla tenaci invia est via"
2010-08-17, 7:15 PM #29
Originally posted by Darth Evad:
parameterized queries?


I don't use PHP much (opinion withheld), but it looks like this[1] may be an implementation of it.

Parameterized queries are better because you don't have to worry about escaping single quotes in strings (or, at least, you don't have to with JDBC - I'm inferring here).

More importantly, however (at least in Oracle), usage of paramterized queries allows for reuse of their parsed, optimized forms. For example, let's say I submit the following query to Oracle:

Code:
select * from user where user_id = 1234567890.0


Then I submit the following to Oracle:

Code:
select * from user where user_id = 987654321.0


It's the same query, right? The only thing that's changed is the user ID. However, Oracle sees it as two completely different queries and will re-parse the execution path for each query. The cost is negligible in this case, but high-volume executions of simple queries or a few executions of complex queries can have a measurable impact on performance. Instead, you could use the following notation:

Code:
select * from user where user_id = ?


I can now use bind variables[2] to bind the two values I queried for above. Oracle will receive the above query and the value bindings and, for the first execution, do a hard parse of the execution path of the query. For the second, it will do a soft parse and retrieve the stored execution path from memory.

1. http://us2.php.net/ref.mysqli
2. http://en.wikipedia.org/wiki/SQL_injection#Parameterized_statements
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2010-08-17, 9:39 PM #30
I think he should get his script working before he worries about prepared statements :P

I mean look he is inserting post data directly to the DB
"Nulla tenaci invia est via"
2010-08-18, 1:18 AM #31
Originally posted by zanardi:
I think he should get his script working before he worries about prepared statements :P

I mean look he is inserting post data directly to the DB


Heh, yeah but prepared statements would sort out all the quoting issues. You're using the mysql extension which doesn't support preapred statements anyway. You'd need to use mysqli or PDO (which is currently FOTM in php) to use them.

For example:

Code:
$pdo = new PDO(...);
$stmt = $pdo->prepare('INSERT INTO test(name, status) VALUES (:name, :status)');
$stmt->execute(array(':name' => $name, ':status' => 0));


This should sort out any SQL injection too.
TheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWho
SaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTh
eJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSa
ysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJ
k
WhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSays
N
iTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkW
2010-08-18, 9:23 AM #32
Hi Evad!
My Parkour blog
My Twitter. Follow me!
2010-08-18, 9:38 AM #33
Originally posted by Cool Matty:
Neither? Usually it's /usr/bin or somewhere in /usr. /var/www is the place where the webpages live. /etc/ is configs.


Yes. But on ubuntu, you'll see a symbolic link to a phpmyadmin folder in /var/www/. Obviously, since it's a web interface.
2010-08-18, 10:07 AM #34
Originally posted by Trigger Happy Chewie:
Yes. But on ubuntu, you'll see a symbolic link to a phpmyadmin folder in /var/www/. Obviously, since it's a web interface.


huh? I mean yes I know it's there but what's that have to do with where php is installed?
"Nulla tenaci invia est via"
2010-08-18, 11:40 AM #35
CM, can we please have a white background for the PHP code?
Sorry for the lousy German
2010-08-18, 12:46 PM #36
Originally posted by Trigger Happy Chewie:
Yes. But on ubuntu, you'll see a symbolic link to a phpmyadmin folder in /var/www/. Obviously, since it's a web interface.


Originally posted by zanardi:
huh? I mean yes I know it's there but what's that have to do with where php is installed?


What zanardi said.

PHPMyAdmin is completely unrelated to PHP itself except for the fact that it runs on PHP. Obviously it'd have the php files in an area where PHP is processing them, since they're just webpages.
2010-08-18, 4:54 PM #37
Turn on error display in the php config file...

2010-08-18, 7:05 PM #38
Originally posted by Cool Matty:
What zanardi said.

PHPMyAdmin is completely unrelated to PHP itself except for the fact that it runs on PHP. Obviously it'd have the php files in an area where PHP is processing them, since they're just webpages.


I know, I was agreeing with you that is where PHP would be installed; however, Evad mentioned phpmyadmin, /etc AND /var/www. I was merely showing they were two entities and not to look for php in /var/www. I see that was confusing.
2010-08-20, 4:27 PM #39
well, i've tried everything i could. i even tried reinstalling mysql.
i've set up mysql and php on a local machine before with relatively little trouble.

i couldn't get mysql and php to install in the /usr/bin/ directory. they're still in /etc/.

is there anything i might be overlooking?
2010-08-20, 4:46 PM #40
Is there a reason you're trying to move everything?
12

↑ Up to the top!