php ban script. Is this right?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

metjet2
Forum Newbie
Posts: 14
Joined: Mon Dec 06, 2010 9:21 am

Re: php ban script. Is this right?

Post by metjet2 »

There is no error at all the IP is just not added to the database
metjet2
Forum Newbie
Posts: 14
Joined: Mon Dec 06, 2010 9:21 am

Re: php ban script. Is this right?

Post by metjet2 »

The var dump SHOWS the correct IP address on the page but it is not going into the database
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php ban script. Is this right?

Post by social_experiment »

@metjet2 : What i meant was REMOVE mysql_result() completely but regardless, according to your picture there is no 'username' field in the table, that might be it?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
metjet2
Forum Newbie
Posts: 14
Joined: Mon Dec 06, 2010 9:21 am

Re: php ban script. Is this right?

Post by metjet2 »

I just want to insert the ip that it grabbed from the specific username into the banned_ips table in the database
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php ban script. Is this right?

Post by social_experiment »

metjet2 wrote:The var dump SHOWS the correct IP address on the page but it is not going into the database
Ok.

Code: Select all

<?php mysql_query("INSERT INTO banned_ips (banned_ips) VALUES ($ip)"); ?>
This should insert the data ok, here's the format of an INSERT sql query INSERT INTO tableName (field) VALUES (value)
Last edited by social_experiment on Fri Dec 17, 2010 9:57 am, edited 1 time in total.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: php ban script. Is this right?

Post by superdezign »

The error likely lies in your insertion query:

Code: Select all

INSERT INTO banned_ips ('$ip')
If you had error reporting on, MySQL would inform you that your query is malformed. You do not have the keyword VALUES (or VALUE) before you define your values. Also, you do not need to define the fields explicitly, but if you do not, your syntax will require that you define the value of all fields. By your description, your table has at least two fields: ID and IP. Your query only defines 1 of them, thus still being invalid.

Assuming that your table has only two columns, id and ip, the insertion can be correctly made a few ways:

Code: Select all

insert into `banned_ips` values (1, '$ip');
insert into `banned_ips` (`ip`) values ('$ip');
insert into `banned_ips` set `ip` = '$ip';
Also, @social_experiment, you forgot the string delimiters in your query. And I doubt that the column is named "banned_ips". :P
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: php ban script. Is this right?

Post by social_experiment »

superdezign wrote:Also, @social_experiment, you forgot the string delimiters in your query. And I doubt that the column is named "banned_ips".
I did indeed. Luckily you corrected a similar error earlier in the post so i don't think metjet2 will get it wrong ;) Yip it's called ip actually, my bad.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
metjet2
Forum Newbie
Posts: 14
Joined: Mon Dec 06, 2010 9:21 am

Re: php ban script. Is this right?

Post by metjet2 »

I tried the 3 ways listed above..The ID field is being increased (before it was not so it is somewhat improving) but the ip is not going into the IP field
metjet2
Forum Newbie
Posts: 14
Joined: Mon Dec 06, 2010 9:21 am

Re: php ban script. Is this right?

Post by metjet2 »

Actually never mind (error on my part) the script now works :D thank you guys so much :D
Post Reply