/    Sign up×
Community /Pin to ProfileBookmark

IP Address in PHP

Hi Guys,
I have been working on this code and it keeps giving me an error saying:

Error: 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 ‘24.130.244.65)’ at line 3

here is my PHP Code:
<?php
function getRealIpAddr()
{
if (!empty($_SERVER[‘HTTP_CLIENT_IP’])) //check ip from share internet
{
$ip=$_
SERVER[‘HTTP_CLIENT_IP’];
}
elseif (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])) //to check ip is pass from proxy
{
$ip=$_
SERVER[‘HTTP_X_FORWARDED_FOR’];
}
else
{
$ip=$_SERVER[‘REMOTE_ADDR’];
}
return $ip;
}
$ip = getRealIpAddr();
$con = mysql_connect(“localhost”,”root”,”44881234″);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“my_db”, $con);

$sql=”INSERT INTO Phone (FirstName, LastName, AC, RC, Num, Email, Time, IP)
VALUES
(‘$_POST[firstname]’,’$_POST[lastname]’,’$_POST[AC]’,’$_POST[RC]’,’$_POST[Num]’,’$_POST[Email]’, NOW()), $ip)”;

if (!mysql_query($sql,$con))
{
die(‘Error: ‘ . mysql_error());
}
echo “1 record added”;

mysql_close($con)
?>

And here is my html form code:

<html>
<body>

<SCRIPT TYPE=”text/javascript”>
<!–
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;

// numbers
else if (((“0123456789”).indexOf(keychar) > -1))
return true;

// decimal point jump
else if (dec && (keychar == “.”))
{
myfield.form.elements[dec].focus();
return false;
}
else
return false;
}

//–>

<!–
var downStrokeField;
function autojump(fieldName,nextFieldName,fakeMaxLength)
{
var myForm=document.forms[document.forms.length – 1];
var myField=myForm.elements[fieldName];
myField.nextField=myForm.elements[nextFieldName];

if (myField.maxLength == null)
myField.maxLength=fakeMaxLength;

myField.onkeydown=autojump_keyDown;
myField.onkeyup=autojump_keyUp;
}

function autojump_keyDown()
{
this.beforeLength=this.value.length;
downStrokeField=this;
}

function autojump_keyUp()
{
if (
(this == downStrokeField) &&
(this.value.length > this.beforeLength) &&
(this.value.length >= this.maxLength)
)
this.nextField.focus();
downStrokeField=null;
}
//–>
</SCRIPT>

<form action=”insert.php” method=”post”>
First Name: <input type=”text” name=”firstname” required=”required” title=”Please Enter Your First Name” /> <br/>
Last Name: <input type=”text” name=”lastname” required=”required” title=”Please Enter Your Last Name” /><br/>
Phone Number: 1( <input onKeyPress=”return numbersonly(this, event)” title=”Please Enter Your Phone Number” required=”required” type=”text” name=”AC” maxlength=”3″ style=”width:30px;”/> )
<input onKeyPress=”return numbersonly(this, event)” title=”Please Enter Your Phone Number” required=”required” type=”text” name=”RC” maxlength=”3″ style=”width:30px;” /> –
<input onKeyPress=”return numbersonly(this, event)” title=”Please Enter Your Phone Number” required=”required” type=”text” name=”Num” maxlength=”4″ style=”width:40px;” /> <br/>
E-mail Adress: <input type=”email” name=”Email” /> <br/>
<input type=”submit” />
</form>

<SCRIPT TYPE=”text/javascript”>
autojump(‘AC’, ‘RC’, 3);
autojump(‘RC’, ‘Num’, 3);
</SCRIPT>

</body>
</html>

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscFeb 09.2012 — If there is an error in your syntax, then echo your SQL and check for the error.

It looks something like this right now
<i>
</i>INSERT INTO Phone (FirstName, LastName, AC, RC, Num, Email, Time, IP) VALUES('fname','lname','ac','rc','num','email', NOW()), $ip)";


You can see that you have a closing parenthetical after NOW()[b])[/b] that is not supposed to be there. As well, the $ip will need to be quoted, like the rest of your variables.

All that being said, good luck with putting raw $_REQUEST variables into your database, let me know the URL of your form and we'll see it takes to destroy your database by simple filling out the form.

http://php.net/manual/en/function.mysql-real-escape-string.php
×

Success!

Help @veshant spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...