0

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);

?>
LAAn
  • 13
  • 6
  • 1
    Show us the actual as-implemented code, and tell us which line is #17, please. – ceejayoz May 20 '19 at 19:54
  • 2
    `$ipaddress` is not the same as `$ipadress` – Chris White May 20 '19 at 19:56
  • @ChrisWhite Thank you, the error is gone now! But it doesn't add any data to the ipadress column in the database? Only to the other columns. – LAAn May 20 '19 at 20:00
  • 1
    @LindaKaltrinaAndersson Fixing that typo shouldn't fix the "unspected" error. That's usually because you forgot the `;` at the end of the previous line. – Barmar May 20 '19 at 20:04
  • You should parameterize your query. I also would recommend storing in a different format so you can use functions on the value later. https://dev.mysql.com/doc/refman/5.6/en/miscellaneous-functions.html#function_inet6-aton – user3783243 May 20 '19 at 20:09
  • @Barmar could you please specify? I don't understand where I forgot the ; – LAAn May 20 '19 at 20:10
  • I don't see where you did, either. – Barmar May 20 '19 at 20:12
  • But misspelling a variable usually causes an "Undefined variable" error, not a "Parse error". – Barmar May 20 '19 at 20:13
  • You can also get that syntax error if you forget a `.` that's needed for concatenation. But the code you posted doesn't need that, since you substitute the variable inside a string. All I can guess is that you didn't post the actual code. – Barmar May 20 '19 at 20:14
  • Yes, during your answers I worked on the code and got different errors. I now edited my question, sorry! But now you can see what I have a problem with. :) – LAAn May 20 '19 at 20:17
  • @Barmar I have edited my question :) – LAAn May 20 '19 at 20:18
  • Which line is line #17? – Barmar May 20 '19 at 20:19
  • I don't have the file left that had that error. But I don't have any errors now and the code works well and the other columns add data. Just not ip adress, it doesn't add an ip adress to the column @Barmar – LAAn May 20 '19 at 20:23
  • Then you must have changed something else when you fixed the typo. – Barmar May 20 '19 at 20:27
  • Where is the code that calls the `visitor()` function? – Barmar May 20 '19 at 20:28
  • it's in my index file and looks like this @Barmar: <?php echo $page_name; ?> – LAAn May 20 '19 at 20:30
  • You never execute the SQL that's supposed to put the IP address in the table. – Barmar May 20 '19 at 20:41
  • Ok! What should I add, how do I execute it? – LAAn May 20 '19 at 20:46
  • @Barmar I tried adding $sql->execute(); to the code I just send you. But then I get the error: Fatal error: Call to a member function execute() on a non-object in (myfile) on line 11. Line 11 is $sql->execute(); – LAAn May 20 '19 at 21:06
  • You need to execute it the same way you execute the SQL inside the `visitor()` function. You have to call `mysqli_connect()`, and then call `mysqli_query()`. – Barmar May 20 '19 at 21:18
  • Maybe you should just move that inside the `visitor()` function, so you don't have to make another connection. – Barmar May 20 '19 at 21:19
  • @Barmar Yes, I would like to move it inside the visitor() function, but since I am new to php could you help me with how I implement it in the function? – LAAn May 20 '19 at 21:22
  • Is it supposed to be in the same row as the record that `visitor()` is adding, or is it supposed to be in a row by itself as you've shown? – Barmar May 20 '19 at 21:23

1 Answers1

0

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;
}
?>
Barmar
  • 741,623
  • 53
  • 500
  • 612