BlueM Posted July 26, 2015 Share Posted July 26, 2015 Hey guys, I have been trying to figure out what is wrong with my unique hit counter on my site. With a lot of trouble shooting i have figured out that the function to add the ip to the table is not working. Can anyone help me out? function ip_add($i){ global $con; global $realid; $query_run=mysqli_query($con,"INSERT INTO media(ip) VALUES ('$i') WHERE id=".$realid); } Quote Link to comment Share on other sites More sharing options...
requinix Posted July 26, 2015 Share Posted July 26, 2015 Use mysqli_error to see if there were any errors. Otherwise, what's the value of $i and $realid? The exact values - output them (you could output the query before executing it), don't just guess. Quote Link to comment Share on other sites More sharing options...
BlueM Posted July 26, 2015 Author Share Posted July 26, 2015 $i is just supposed to be a placeholder for when i actually run the function and $realid is a value taken from the url. I'm not quite sure how i would be able to use the mysqli_error() function. I'm a newb :s Quote Link to comment Share on other sites More sharing options...
requinix Posted July 26, 2015 Share Posted July 26, 2015 $i is just supposed to be a placeholder for when i actually run the function and $realid is a value taken from the url.That's nice. But the error happens when the function actually runs, so what are the values of those two variables when the function actually runs? I'm not quite sure how i would be able to use the mysqli_error() function. I'm a newb :sDid you click the link I posted? Quote Link to comment Share on other sites More sharing options...
BlueM Posted July 26, 2015 Author Share Posted July 26, 2015 (edited) $user_ip =$_SERVER['REMOTE_ADDR']; ip_add($user_ip); and $realid is a number that corresponds to the id collum in my table $url=$_SERVER['REQUEST_URI']; $realid=trim(strstr($url, '.'),'.'); and yeah i clicked on the link but i still dont understand... if (!mysqli_query($query_run, "SET ip=1")) { printf("Errormessage: %s\n", mysqli_error($query_run)); } that gives me a bunch of errors. Edited July 26, 2015 by BlueM Quote Link to comment Share on other sites More sharing options...
requinix Posted July 26, 2015 Share Posted July 26, 2015 (edited) and $realid is a number that corresponds to the id collum in my table $url=$_SERVER['REQUEST_URI']; $realid=trim(strstr($url, '.'),'.'); There is nothing in that which says $realid is a number. I'm trying to get actual. values. of. those. variables. Not a description of what you think they are. Not code showing how they get values. Actual. Values. Change your function to look like function ip_add($i){ global $con; global $realid; $query="INSERT INTO media(ip) VALUES ('$i') WHERE id=".$realid; echo "THE QUERY IS '$query'"; $query_run=mysqli_query($con,$query); }Run your script. What is the entire message that it outputs? I'd like you to try to provide the entire message from that echo. Yes, I know what the first three words will be, but that's not the point. if (!mysqli_query($query_run, "SET ip=1")) { printf("Errormessage: %s\n", mysqli_error($query_run)); } that gives me a bunch of errors. You've now demonstrated that you do, in fact, understand how to use the function. Or at least came upon some combination of letters and symbols that together formed valid code which also happened to demonstrate how mysqli_error() is used. Now, I have no idea what this new bit of code has to do with anything you've posted so far. Maybe it's supposed to work the same way as an INSERT query? But if you want to resolve those errors then your next step should be to post what those errors are. Again, the actual errors. Not a description of what they say. The actual messages you are seeing. Edited July 26, 2015 by requinix Quote Link to comment Share on other sites More sharing options...
BlueM Posted July 26, 2015 Author Share Posted July 26, 2015 When I run ip_add($user_ip); this is the output.... THE QUERY IS 'INSERT INTO media(ip) VALUES ('::1') WHERE id=51' im so lost, I dont know why it wont run Quote Link to comment Share on other sites More sharing options...
BlueM Posted July 26, 2015 Author Share Posted July 26, 2015 I got it to work with an update query instead. I should be doing that because I need to append to the data that is already there. Thanks for the printf tip Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted July 27, 2015 Solution Share Posted July 27, 2015 For what it's worth, the WHERE clause doesn't work with INSERT. More information on the syntax for INSERT can be found here: http://dev.mysql.com/doc/refman/5.6/en/insert.html 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.