ajetrumpet Posted October 13, 2019 Share Posted October 13, 2019 (edited) hi guys, I'm trying to use a very basic PHP script and i think I've got everything right but I'm not getting any records inserted into my table. here is my script: $dbHost = "ip address"; $dbName= "rptDatabase"; $dbUsername = "username"; $dbPassword = "password"; $ip = $_SERVER['REMOTE_ADDR']; $page = $_SERVER['PHP_SELF']; $referrer = $_SERVER['HTTP_REFERER']; $date = date("m/d/y"); $time = date("h:i:a"); $db = mysql_connect($dbHost, $dbUsername, $dbPassword) or trigger_error(mysql_error(),E_USER_ERROR); mysql_select_db($dbName, $db); $sql = "INSERT INTO tblTraffic (ip, page, referrer, date, time) VALUES ($ip, $page, $referrer, $date, $time)"; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); mysql_close($db); can someone tell me what I'm doing wrong? thanks. Edited October 13, 2019 by ajetrumpet Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 (edited) The list of what you are doing right might be shorter, however... You are using obsolete MySQL_ functions. Use PDO instead. string values in a query need to be inside quotes eg VALUES ('$ip', '$page', ...etc) You should be using prepared queries so input values are not inserted directly into the query statement You should always store dates in a database in yyyy-mm-dd format Instead of calculating current time and date in PHP then putting them in the query, just use a TIMESTAMP column which will update automatically You probably haven't got error reporting turned on Edited October 13, 2019 by Barand Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 1: I will look up PDO. what functions am I using that are obsolete? 6. I thought error reporting was always on in PHP. How do I check for this? I'm using the American Company Godaddy for my web hosting hosting and I manage mysql databases with phpMyAdmin. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 All function beginning with mysql_ are obsolete, replaced several years ago by either mysqli_ (improved) or PDO and removed completely from PHP 7.0 onwards.. The consensus is that PDO is better than mysqli. I assumed it's turned off as you aren't apparently getting warning messages about the use of mysql_* functions. The settings are in your php.ini file. It could be the version of PHP is obsolete. Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 well I downloaded eclipse and the version of php is 7.0 something, so the latest one I believe. I can't find a php.ini file. I remember seeing one last time I did this, but in eclipse when you begin a new project you only get a PROJECT file, and the setting is not in there. where can i find the .ini file? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 Run this script <?php phpinfo(); ?> The first few lines of the output should contain a line like this Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 it is listed as: /opt/alt/php54/etc/php.ini I searched my entire server directories and I can't find it anywhere. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 It will be on the php host server. Maybe there is a cPanel to access it or something similar Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 (edited) attached is what I see in my cPanel. none of the options in there looks like they are correct. any ideas? Edited October 13, 2019 by ajetrumpet Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 You'd have to try some likely ones and see. One possibility is that errors are reported and logged instead of being displayed. Check for a php error log. Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 (edited) Ok godaddy told me that error reporting is turned on by default and that if i wanted overwrite that file i need to create my own .user.ini file in my root dir. And yes i have an error log file. I will take a look in a little bit to see what it has logged. Edited October 13, 2019 by ajetrumpet Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 Check the "Core" section in the output from phpinfo(). That will tell you what's happening regarding error reporting settings. You may not have to do anything other than find your php_error.log Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 in my phpinfo() output, I see 4 related records: Directive Local Value Master Value display_errors Off Off error_log error_log error_log error_reporting 32767 32767 log_errors On On here is my current code: $dbHost = "ip address"; $dbName= "rptDatabase"; $dbUsername = "username"; $dbPassword = "password"; $ip = $_SERVER['REMOTE_ADDR']; $page = $_SERVER['PHP_SELF']; $referrer = $_SERVER['HTTP_REFERER']; $date = date("m/d/y"); $time = date("h:i:a"); $db = new mysqli($dbHost, $dbUsername, $dbPassword) or trigger_error(mysql_error(),E_USER_ERROR); mysql_select_db($dbName, $db); $sql = "INSERT INTO tblTraffic (ip, page, referrer, date, time) VALUES ('$ip', '$page', '$referrer', '$date', '$time')"; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); mysqli_close($db); going to the page throws numerous errors into the log: [13-Oct-2019 18:20:06 UTC] PHP Notice: Undefined index: HTTP_REFERER in /home/name/public_html/DOMAIN/test/recordpageview.php on line 9 [13-Oct-2019 18:20:06 UTC] PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'username'@'ip-ipAddress.ip.secureserver.net' (using password: YES) in /home/home/public_html/DOMAIN/test/recordpageview.php on line 13 [13-Oct-2019 18:20:06 UTC] PHP Warning: mysql_select_db() expects parameter 2 to be resource, object given in /home/name/public_html/DOMAIN/test/recordpageview.php on line 15 [13-Oct-2019 18:20:06 UTC] PHP Warning: mysql_query(): Access denied for user 'username'@'localhost' (using password: NO) in /home/name/public_html/DOMAIN/test/recordpageview.php on line 18 [13-Oct-2019 18:20:06 UTC] PHP Warning: mysql_query(): A link to the server could not be established in /home/name/public_html/DOMMAIN/test/recordpageview.php on line 18 [13-Oct-2019 18:20:06 UTC] PHP Fatal error: Access denied for user 'username'@'localhost' (using password: NO) in /home/name/public_html/DOMAIN/test/recordpageview.php on line 18 this really doesn't make a whole lot of sense, does it? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 13, 2019 Share Posted October 13, 2019 YOu have a mysqlI call and a mysql call. That's the problem. (among others I expect). You should read up on prepared query statements Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 (edited) fine. I will. I'll get back to you guys after I do so. Edited October 13, 2019 by ajetrumpet Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 (edited) my new script after reading about preps is: $dbHost = "ipaddress"; $dbName= "rptDatabase"; $dbUsername = "username"; $dbPassword = "password"; $ip = $_SERVER['REMOTE_ADDR']; $page = $_SERVER['PHP_SELF']; $referrer = $_SERVER['HTTP_REFERER']; $date = date("m/d/y"); $time = date("h:i:a"); $db = new mysqli($dbHost, $dbUsername, $dbPassword) or trigger_error(mysql_error(),E_USER_ERROR); $stmt = $conn->prepare("INSERT INTO tblTraffic (ip, page, referrer, date, time) VALUES (?, ?, ?, ?, ?)"); $stmt->bind_param("sss", $ip, $page, $referrer, $date, $time); $stmt->execute(); $stmt->close(); $db->close(); and I get less errors, but still plenty: [13-Oct-2019 18:59:36 UTC] PHP Notice: Undefined index: HTTP_REFERER in /home/name/public_html/DOMAIN/test/recordpageview.php on line 9 [13-Oct-2019 18:59:36 UTC] PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'username'@'ip-ipaddress.ip.secureserver.net' (using password: YES) in /home/ name/public_html/DOMAIN/test/recordpageview.php on line 13 [13-Oct-2019 18:59:36 UTC] PHP Notice: Undefined variable: conn in /home/name/public_html/DOMAIN/test/recordpageview.php on line 16 [13-Oct-2019 18:59:36 UTC] PHP Fatal error: Call to a member function prepare() on a non-object in /home/name/public_html/DOMAIN/test/recordpageview.php on line 16 Edited October 13, 2019 by ajetrumpet Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 37 minutes ago, ajetrumpet said: this really doesn't make a whole lot of sense, does it? It does if you have some of your own. 38 minutes ago, ajetrumpet said: 13-Oct-2019 18:20:06 UTC] PHP Notice: Undefined index: HTTP_REFERER in /home/name/public_html/DOMAIN/test/recordpageview.php on line 9 There is no item in the $_SERVER array with the key "HTTP_REFERER" 39 minutes ago, ajetrumpet said: [13-Oct-2019 18:20:06 UTC] PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'username'@'ip-ipAddress.ip.secureserver.net' (using password: YES) in /home/home/public_html/DOMAIN/test/recordpageview.php on line 13 The username and password you are using to connect are invalid 41 minutes ago, ajetrumpet said: [13-Oct-2019 18:20:06 UTC] PHP Warning: mysql_select_db() expects parameter 2 to be resource, object given in /home/name/public_html/DOMAIN/test/recordpageview.php on line 15 You are calling a mysql_ function and pasing it a mysqli object. (You cannot mix'n'match) Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 (edited) Barand, This has taken quite a bit of my time and I'm getting a bit buggy in the brain. If you have time, please review my last post, as I have written a new script with prepared query statements but i'm still getting the same errors. I will try and correct some of the more obvious ones. Edited October 13, 2019 by ajetrumpet Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 Most of my last comments still apply. You haven't done much about those errors. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 This should fly better <?php $dbHost = "ipaddress"; // USE $dbName= "rptDatabase"; // VALID $dbUsername = "username"; // CREDENTIALS $dbPassword = "password"; // HERE $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $page = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : ''; $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; // check they exist $date = date("Y-m-d"); $time = date("h:i:a"); mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); // tells mysqli to automatically report errors $conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);; $stmt = $conn->prepare("INSERT INTO tblTraffic (ip, page, referrer, date, time) VALUES (?, ?, ?, ?, ?)"); $stmt->bind_param("sssss", $ip, $page, $referrer, $date, $time); $stmt->execute(); $stmt->close(); $db->close(); ?> Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 I added my username to the database as an authorized user and clicked "allow all privaleges". As far as REFERER goes, everything I read on the web mentions nothing about an array section of the variable that I have to specify. the code given is exactly what I have. do you have a page you can point me to that shows me what you're talking about? Here's a new error I'm getting as well: [13-Oct-2019 19:36:59 UTC] PHP Warning: mysqli::prepare(): Couldn't fetch mysqli in /home/name/public_html/DOMAIN/test/recordpageview.php on line 17 Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 thanks for the new script. i'll try it and get back to you. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 1 minute ago, ajetrumpet said: As far as REFERER goes, everything I read on the web mentions nothing about an array section of the variable that I have to specify. https://www.php.net/manual/en/reserved.variables.server.php Quote Link to comment Share on other sites More sharing options...
ajetrumpet Posted October 13, 2019 Author Share Posted October 13, 2019 (edited) i tried: $dbHost = "ip address"; $dbName= "rptDatabase"; $dbUsername = "username"; $dbPassword = "password"; $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $page = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; $date = date("m/d/y"); $time = date("h:i:a"); mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName) $stmt = $conn->prepare("INSERT INTO tblTraffic (ip, page, referrer, date, time) VALUES (?, ?, ?, ?, ?)"); $stmt->bind_param("sssss", $ip, $page, $referrer, $date, $time); $stmt->execute(); $stmt->close(); $conn->close(); and I get only one error, but coming from you I can't understand why this would be thrown now: [13-Oct-2019 20:39:37 UTC] PHP Parse error: syntax error, unexpected '$stmt' (T_VARIABLE) in /home/name/public_html/DOMAIN/test/recordpageview.php on line 16 Edited October 13, 2019 by ajetrumpet Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2019 Share Posted October 13, 2019 1 minute ago, ajetrumpet said: $conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName) You've removed the ";" from the end of the statement Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.