Jump to content

[SOLVED] Last inserted row


jmr3460

Recommended Posts

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!

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.