• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 683
  • Last Modified:

How to put "$IP =$_SERVER['REMOTE_ADDR'];" into MySQL table?

Hi!

I wonder whether there is a way to put into MySQL table two things:
(1) $IP =$_SERVER['REMOTE_ADDR'];
(2) $date=date("l , F d, Y, h:i:s A");

I created a script as follows, but it did not work:
_______________my script ___________________________________
 $insert_qry = "INSERT INTO apply VALUES (NULL,'$IP', '$date', '$name', '$major','$gpa', '$email',  '$address', '$phone', '$year', '$skills', '$attitude', '$available')";
________________________________________________________________

For your information, the script is part of an online PHP survey.

Thanks a lot!

duta

May 30 (Tuesday), 2006, at 10:40pm
I
0
duta
Asked:
duta
1 Solution
 
ClickCentricCommented:
$insert_qry = sprintf("INSERT INTO apply VALUES (NULL,'%s', '%s', '%s', '%s','%s', '%s',  '%s', '%s', '%s', '%s', '%s', '%s')",$IP, $date, $name, $major,$gpa, $email,  $address, $phone, $year, $skills, $attitude, $available);

Note that what the fieldtypes are set to could effect things.  It would help to know the schema of the database (ie fieldname/type).
0
 
dr_dedoCommented:
you can't insert date foramted that way into mySQL, mysql accepts only YYYY-MM-DD HH:MM:SS
you can insert date that way and upont retrival, change it back to whatever suites you either from within mySQL or via PHP
there is no problem saving an IP into the DB! use it just as a common string
echo your SQL statment before using it, and test it in mysql or phpmyadmin
also, please, not that ur query here assumes you don't have an id for the table! I guess you are missing somthing here, if u have an ID , then use this
 $insert_qry = "INSERT INTO apply (`fielf1????`,`IP`,`date`,`name`,`major`,`gpa`,`email`,`address`,`phone`,`year`,`skills`,`attitude`,`available`) VALUES (NULL,'$IP', '$date', '$name', '$major','$gpa', '$email',  '$address', '$phone', '$year', '$skills', '$attitude', '$available')";
0
 
ClickCentricCommented:
Well, you can insert date that way depending on the type of the field it's being stored in.  A varchar field could store a date in any format.  And he didn't mention what exactly the error was that the INSERT was producing.  I sometimes have problems using inline variables, which is why I suggested the sprintf.  Really more information is needed to figure what exactly is being attempted. The table schema and sample values for the variables would be best.  And definitely the error that's being thrown.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dutaAuthor Commented:
TO: ClickCentric and dr_dedo:

Thank you so much for your kind, prompt response.

I have several questions from your comment:

The first question is to dr_dedo: You said, "there is no problem saving an IP into the DB! use it just as a common string." I thought of putting it as string, but I couldn't find string type in PHPAdmin's table data type options.
The second question is: You wrote: $insert_qry = "INSERT INTO apply (`fielf1????`,`IP`,`date`,`  . . .

I wondered what `fielf1????` is.

Next question is to ClickCentric: You wrote: $insert_qry = sprintf("INSERT INTO apply VALUES (NULL,'%s', '%s', '%s', '%s','%s', '%s',  '%s', '%s', '%s', '%s', '%s', '%s')",$IP, $date, $name, $major,$gpa, $email,  $address, $phone, $year, $skills, $attitude, $available);

What does it mean all the '%s'?

Thanks a lot!

duta

May 30, 2006, at 11:24 pm









0
 
dutaAuthor Commented:
TO: ClickCentric:

There was no error messages. After I clicked the SUBMIT button after filling out the survey form, all input except IP and date was stored in MySQL table. I chose varchar as data type of IP and date in the MySQL table.

I want to collect and keep (in MySQL database) (1) IP address of respondents of the survey and (2) the date and time when they responded to the survey.

Thanks again!

duta

May 30, 2006, at 11:32 pm
0
 
ClickCentricCommented:
Use type VARCHAR for strings in most situations.  TEXT if it's a really big string.  VARCHAR works up to 255 characters and TEXT, I think, works up to 65536 characters.

The %s in what I put above is a replace modifier..the sprintf function replaces those with the values appended to the end of the function call.

It would still really help if you posted the information I mentioned before, though...without knowing what field types you're working with, we're kind of taking shots in the dark.  
0
 
ClickCentricCommented:
What size did you set for the varchar fields?  And have you checked the log files to verify there were no warnings?  And if you're using phpmyadmin, have you tried plugging the insert statement that the script is sending and applying it directly to the database via the sql form?
0
 
dutaAuthor Commented:
TO: ClickCentric:

Thanks again!

The size of varchar of both field is set at 20.  For the other two questions, I haven't done them because I didn't know how do. But I will try them now.

Thanks!

duta

May 30, 2006, at 11:42 pm
0
 
dutaAuthor Commented:
TO: ClickCentric:

Using PHPAdmin's data "Insert" table, I could insert values for IP (in the format of 141.145.123.123) and date
(May 30, 2006).

I am trying to figure out how to check the log files.

Thanks!

duta

May 30, 2006, at 11:47 pm

0
 
dutaAuthor Commented:
TO: ClickCentric:

I found an error message on the screen, saying "This error occured when I tried to insert a record: Column 'IP' cannot be null".

Thanks!

duta

May 30, 2006, at 11:50pm
0
 
ClickCentricCommented:
Well, part of the problem could be that the date string you're creating with your call to date() is way longer than 20 characters.  Try making the date field 50 for now.  And verify that $_SERVER['REMOTE_ADDR'] is being defined.
0
 
ClickCentricCommented:
Ok, if you're getting that error, then somethings wrong with your code between where you're setting $IP = $_SERVER['REMOTE_ADDR'] and where your insert statement is being defined, or $_SERVER['REMOTE_ADDR'] is not being defined.  
0
 
dutaAuthor Commented:
TO: ClickCentric:

I also found surprisingly that since I added IP and date fields, no data has been put into MySQL table.

Sorry to confuse you, if I did.

Thanks!

duta

May 30,2006, at 11:54pm
0
 
ClickCentricCommented:
Ok, things have gotten a bit confusing.

What is needed for us to help you with this question is the following:

The database schema (this is a list of the fields, their types and, if applicable, their lengths...ie IP varchar 20)
The current INSERT statement that you are attempting to use (the $insert_qry variable)
And the php code that you're using to define the values for things like $name, $major, $gpa, etc.


Without this, there's really no way to help other than blindly guessing.  You've altered the table, you may have changed the field order and if you did, the order you're putting the values into the insert statement has to match.  
0
 
dutaAuthor Commented:
TO: ClickCentric and dr_dedo:

Thank you so much for your time.

I figured out what was the problem.

I initially set both IP and date '' (null) by default) when I created the table.

Once I removed "Default  ' ' " , it is working just fine.

Thanks again!

duta

May 30, 2006, at 12:17 am

0
 
dutaAuthor Commented:
TO: Moderator:

Please close this topic.  Because I resolved the problem, I may not award the points offered to anyone. But I would like to thank ClickCentric and dr_dedo for their kind effort to help me out.

Thanks!

duta

May 30, 2006, at 12:19 am

0
 
ClickCentricCommented:
I think you have to post the request to the Support section.
0
 
dutaAuthor Commented:
TO: ClickCentric:

Thank you so much for all the time you have spent for me. Even though I may not give you any points (I know you tried to help me not because of the points offered but because you wanted to help a beginner like me out of your heart), I am so grateful to you for your time. I was so moved when kind people like you, BogoJoker and Roonaan took time out of their busy schedule to respond so quickly to my calls for help.

Thanks again!

duta

May 31 (Wednesday), 2006, at 12:30 am

0
 
ClickCentricCommented:
Not a problem and good luck with your ventures.
0
 
CetusMODCommented:
PAQed with points refunded (500)

CetusMOD
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now