jmr3460 Posted June 7, 2009 Share Posted June 7, 2009 OK I have a script that I have been that I actually wrote. WOW yea. My goal is to come up with a counter of some kind. I started by creating a table with id, ip, and time as the fields. There are a few more like uname and ucomments but I am going to take them out eventually. I want to get a kind of unique count using the users ip and time inserted as a kind of cookie. Anyway so far I have got the form so that it will insert these fields on submission. Right now I am looking for a way to get the highest id number (id is auto_increment and Primary Key). I have tried a query selecting all from table where id is MAX(), Limit 1. I have also tried LAST_INSERT_ID(). I have set this query to a variable then I am trying to echo or print the variable. I know that this is bad but I can't seem to copy to this post. I don't know if it is my browser or what. I am going to have to reboot and see if that will do it. Please be patient for the code. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/161252-solved-last-inserted-row/ Share on other sites More sharing options...
Ken2k7 Posted June 7, 2009 Share Posted June 7, 2009 SELECT LAST_INSERT_ID() FROM tablename LIMIT 1; That should select the right one right? Quote Link to comment https://forums.phpfreaks.com/topic/161252-solved-last-inserted-row/#findComment-850885 Share on other sites More sharing options...
jmr3460 Posted June 7, 2009 Author Share Posted June 7, 2009 That gave me an echo of resource #3 <?php if(isset($_GET['commented'])) { $host = "localhost"; $user = "user"; $pass = "pass"; $database = "counter"; $table = "comments"; $uname = $_GET['uname']; $ucomment = $_GET['ucomment']; $ip = getenv('REMOTE_ADDR'); $date = date("M-d-Y h:m", time()); mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); $add_all = "INSERT INTO $table values('','$date','$ip','$uname','$ucomment')"; mysql_query($add_all) or die(mysql_error()); } else { ?> <form method="get" action="index.php"> Name : <input type="text" name="uname"> <br><br> Comment : <input type="text" name="ucomment"> <br><br> <input type="hidden" name="commented" value="set"> <input type="submit" value="Post your comment"> </form> <?php } ?> <?php $host = "localhost"; $user = "user"; $pass = "pass"; $database = "counter"; //Connects to your DB mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); $data = mysql_query("SELECT * FROM comments") or die(mysql_error()); $id_counter = mysql_query("SELECT LAST_INSERT_ID() FROM comments"); echo "<html><body><table border=\"1\">"; echo "<tr><th>Name</th><th>Comments</th><th>IP Address</th><th>Time</th><th>User ID</th></tr>"; while($info = mysql_fetch_array($data)) { echo "<tr><td>" . $info['uname'] . "</td><td>" . $info['ucomment'] . "</td><td>" . $info['ip'] . "</td><td>" . $info['time'] . "</td><td>" . $info['id'] . "</td></tr>"; } echo "</table></body></html>"; echo $id_counter; ?> I an trying to ste the variable $id_counter as the mysql_query(). Am I on the right path? Quote Link to comment https://forums.phpfreaks.com/topic/161252-solved-last-inserted-row/#findComment-850891 Share on other sites More sharing options...
Ken2k7 Posted June 7, 2009 Share Posted June 7, 2009 Why are you making 2 connections to the server? Isn't one mysql_connect good enough? And you missed my point in my code sample. Try this - SELECT * FROM comments WHERE id = LAST_INSERT_ID(); Put that as the SQL for $data in your second part and remove the $id_counter part. It should work. Quote Link to comment https://forums.phpfreaks.com/topic/161252-solved-last-inserted-row/#findComment-850896 Share on other sites More sharing options...
jmr3460 Posted June 7, 2009 Author Share Posted June 7, 2009 Thanks Ken2k7, That got the right row. Now I need to just pull the id off that row. The previous code was a combination of two scripts. I made a few changes and took out the form so it will add a row everytime the page is opened. This looks a lot simpler: <?php $host = "localhost"; $user = "simplic5_jmr3460"; $pass = "freedom98714"; $database = "simplic5_counter"; $table = "comments"; $uname = $_GET['uname']; $ucomment = $_GET['ucomment']; $ip = getenv('REMOTE_ADDR'); $date = date("M-d-Y h:i:s", time()+3600); mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); $add_all = "INSERT INTO $table values('','$date','$ip','','')"; mysql_query($add_all) or die(mysql_error()); $data = mysql_query("SELECT id FROM comments WHERE id = LAST_INSERT_ID();") or die(mysql_error()); while($info = mysql_fetch_array($data)) { echo $info['id']; } ?> This just shows the id number of the last row. Now I want to only add a row if a cookie is not set. Quote Link to comment https://forums.phpfreaks.com/topic/161252-solved-last-inserted-row/#findComment-850901 Share on other sites More sharing options...
jmr3460 Posted June 7, 2009 Author Share Posted June 7, 2009 I have set a cookie and I think that is ok but Now I am getting this warning: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home3/simplic5/public_html/php/auto_count.php on line 24 <?php setcookie("setcount","baseball",time()+7200,"/"); $host = "localhost"; $user = "user"; $pass = "pass"; $database = "counter"; $table = "comments"; $ip = getenv('REMOTE_ADDR'); $date = date("M-d-Y h:i:s", time()+3600); if (!isset($_COOKIE['homerun'])) { mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); $add_all = "INSERT INTO $table values('','$date','$ip','','')"; mysql_query($add_all) or die(mysql_error()); } else { mysql_connect($host, $user, $pass); $data = mysql_query("SELECT id FROM comments WHERE id = LAST_INSERT_ID();") or die(mysql_error()); } while($info = mysql_fetch_array($data)); { echo $data; } ?> This is my attempt to enter a row of data if cookie is not set otherwise echo the id of the last id. I am wanting to use the rest of the table rows to print to a log.txt for review dada dada dada. Quote Link to comment https://forums.phpfreaks.com/topic/161252-solved-last-inserted-row/#findComment-850902 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.