storing ip address in mysql? - php

i am trying to store a users ip address into a mysql table under VARCHAR (39) but its storing as just this "::1"
i am using this code:
<?php
session_start();
$db_hostname = 'localhost';
$db_database = 'hewden1';
$db_username = 'root';
$db_password = '';
$db_server = mysql_connect($db_hostname, $db_username, $db_password)
or die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$cname = $_POST['cname'];
$creg = $_POST['creg'];
$address = $_POST['address'];
$post = $_POST['post'];
$contactn = $_POST['contactn'];
$contactt = $_POST['contactt'];
$email = $_POST['email'];
$vat = $_POST['vat'];
$ipaddress = $_SERVER["REMOTE_ADDR"];
$sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";$result = mysql_query($sql);
if($result){
echo "jobs a gooden";
}else {
echo "ERROR";
}
?>
can someone please show me where i am going wrong thanks

Simple solution, change your ip column to varbinary (16) datatype and then store ips using the following:
$ip = bin2hex(inet_pton($_SERVER['REMOTE_ADDR']));
Note: make sure your php version is up to date as inet_pton() is a new function.

Related

How to insert IP address in table?

I'm coding an admin page where I keep track of users/visitors. I have some code so far, but I need to add ip addresses from the users/visitors to the table as well. This is my code, everything gets added to the database table except for ip address. The table is users4project and column is ip address with the int(10) UNSIGNED NOT NULL I created the table in phpmyadmin.
<?php
function visitor($record) {
// my database info
$db_host = "";
$db_username = "";
$db_password = "";
$db_name = "";
$db_table = "ipusers4project";
$counter_page = "access_page";
$counter_field = "access_counter";
$db = mysqli_connect ($db_host, $db_username, $db_password, $db_name)
or die("Host or database not accessible");
$sql_call = "INSERT INTO ".$db_table." (".$counter_page.",
".$counter_field.") VALUES ('".$record."', 1) ON DUPLICATE KEY UPDATE ".$counter_field." = ".$counter_field." + 1";
mysqli_query($db, $sql_call) or die("Error while entering");
$sql_call = "SELECT ".$counter_field. " FROM ".$db_table." WHERE ".$counter_page. " = '".$record. "'";
$sql_result = mysqli_query($db, $sql_call) or die("SQL request failed ");
$row = mysqli_fetch_assoc($sql_result);
$x = $row[$counter_field];
mysqli_close($db);
return $x;
}
?>
<?php
$ipadress = $_SERVER['REMOTE_ADDR'];
$sql = "INSERT INTO
ipusers4project
( ipadress )
VALUES
( '$ipadress')";
?>
EDIT: On index.php I have this code:
<?php
$page_name = "index.php";
?>
<title><?php echo $page_name; ?></title>
<?php
include "webcounter.php";
$access_number = visitor($page_name);
?>
Just add this as another column in the row that visitor() is adding.
<?php
function visitor($record) {
// my database info
$db_host = "";
$db_username = "";
$db_password = "";
$db_name = "";
$db_table = "ipusers4project";
$counter_page = "access_page";
$counter_field = "access_counter";
$ipadress = $_SERVER['REMOTE_ADDR'];
$db = mysqli_connect ($db_host, $db_username, $db_password, $db_name)
or die("Host or database not accessible");
$sql_call = "INSERT INTO ".$db_table." (".$counter_page.",
".$counter_field.", ipadress) VALUES ('".$record."', 1, '$ipadress') ON DUPLICATE KEY UPDATE ".$counter_field." = ".$counter_field." + 1, ipadress = VALUES(ipadress)";
mysqli_query($db, $sql_call) or die("Error while entering");
$sql_call = "SELECT ".$counter_field. " FROM ".$db_table." WHERE ".$counter_page. " = '".$record. "'";
$sql_result = mysqli_query($db, $sql_call) or die("SQL request failed ");
$row = mysqli_fetch_assoc($sql_result);
$x = $row[$counter_field];
mysqli_close($db);
return $x;
}
?>

Receiving Error Connecting to mySQL server

I am self-learning MySQL and PHP, so please bear with me. I am trying to set up a DB with values being input through a PHP form. I am following along in a book, but cannot find why I am getting the error:
Error Connecting to MySQL server.
I am running a local host, root username, no password, and table called alien_abductions.
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$when_happened = $_POST['whenithappened'];
$how_long_gone = $_POST['howlongwereyougone'];
$howmany = $_POST['howmany'];
$description = $_POST['description'];
$whattheydid = $_POST['whattheydid'];
$fangspotted = $_POST['fangspotted'];
$email = $_POST['email'];
$dbc = mysqli_connect('localhost', 'root', '', 'up_amd_running')
or die('Error connecting to MySQL server');
$query = "INSERT INTO alien_abductions (first_name, last_name, " .
"when_happened, how_long_gone, howmany, description, " .
"whattheydid, fangspotted, email " .
"VALUES ('$first_name', '$last_name',
'$when_happened','$how_long_gone', '$howmany', '$description',
'$whattheydid', `'$fangspotted', '$email')";
echo $query;
$result = mysqli_query($dbc, $query)
or die('Error querying database');
mysqli_close($dbc);

Insert into mysql database through php

I try to insert some information about user but it gives error no database selected (im using phpmyadmin and xampp btw)
code:
<?php
$username = $_POST['username'];
$name = $_POST['name'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($password == $cpassword)
{
mysql_escape_string($username);
mysql_escape_string($name);
mysql_escape_string($password);
mysql_escape_string($cpassword);
$md5pass = md5($password);
mysql_select_db("users");
mysql_query("INSERT INTO users (id, username, name, password) VALUES (DEFAULT, '$username', '$name', '$md5pass'") or die(mysql_error());
}
else
{
die("Passwords don't match");
}
?>
You haven't established connection with your mysql database.
Use following code to make connection with server.
$link = mysql_connect('your servers address', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
else
{
//rest of your code
}

Updating MySQL Error but nothing is wrong in code?

So I get this error:
Problem updating record. MySQL 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 'WHERE KittenID = '2''
But then in my code:
<?php
if(isset($_POST['Modify']))
{
$connection = mysql_connect("Deleted the login info");
// Check connection
if (!$connection)
{
echo "Connection failed: " . mysql_connect_error();
}
else
{
//select a database
$dbName="Katz";
$db_selected = mysql_select_db($dbName, $connection);
//confirm connection to database
if (!$db_selected)
{
die ('Can\'t use $dbName : ' . mysql_error());
}
else
{
$KittenID = $_POST["KittenID"];
$KittenAge = $_POST['KittenAge'];
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Gender = $_POST['Gender'];
$Personality = $_POST['Personality'];
$Activity = $_POST['Activity'];
$Comments = $_POST['Comments'];
$query = "UPDATE Kittenzz
SET KittenID = '$KittenID',
KittenAge = '$KittenAge',
Name = '$Name',
Email = '$Email',
Gender = '$Gender',
Personality = '$Personality',
Activity = '$Activity',
Comments = '$Comments',
WHERE KittenID = '$KittenID'";
$res = mysql_query($query);
if ($res)
{
echo "<p>Record Updated<p>";
}
else
{
echo "Problem updating record. MySQL Error: " . mysql_error();
}
}
}
mysql_close($connection);
}
?>
It makes no sense, I've read those lines of code for an hour, I cannot see the problem. It should run. Can anyone lend me fresh eyes?
Remove the comma near '$comments'
$query = "UPDATE Kittenzz
SET KittenID = '$KittenID',
KittenAge = '$KittenAge',
Name = '$Name',
Email = '$Email',
Gender = '$Gender',
Personality = '$Personality',
Activity = '$Activity',
Comments = '$Comments'
WHERE KittenID = '$KittenID'";
it may possible that in connection username and password in required.
like this :-
$connection = mysql_connect("localhost","username","password");

Insert form data in one table and then update an enum value in a different table?

I am using a code to insert a users form data into the database, that bit works fine. However, i also want to run another query and update an enum value 'form2_completed?' from No to Yes. I have added in the update query and now for some reason the script is not working and says 'ERROR'
Can someone please show me where i am going wrong. thanks
<?php
session_start();
$db_hostname = 'localhost';
$db_database = 'hewden1';
$db_username = 'root';
$db_password = '';
$db_server = mysql_connect($db_hostname, $db_username, $db_password)
or die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$cname = $_POST['cname'];
$creg = $_POST['creg'];
$address = $_POST['address'];
$post = $_POST['post'];
$contactn = $_POST['contactn'];
$contactt = $_POST['contactt'];
$email = $_POST['email'];
$vat = $_POST['vat'];
$ipaddress = $_SERVER["REMOTE_ADDR"];
$sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";
$sql="UPDATE supplier_session SET form2_completed? = 'Yes' WHERE form2_completed? = 'No'";
$result = mysql_query($sql);
if($result){
$success = "<div class='success'></div>"; // use the $success
//encode the URL parameter as :
$success = urlencode($success);
header("Location: index.php?success=$success");
}else {
echo "ERROR";
}
?>
You are overwriting the variable $sql and not running INSERT. Try:
$sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";
$result = mysql_query($sql);
$sql="UPDATE supplier_session SET form2_completed? = 'Yes' WHERE form2_completed? = 'No'";
$result = mysql_query($sql);
Please note that the method you have used is deprecated from php 5.5.0. so i suggest you consider mysqli or PDO. examples can be found in below php manual links
http://www.php.net/manual/en/mysqli.query.php
http://www.php.net/manual/en/pdo.query.php

Categories