插入时获取MySQL ID

I'm trying to get the row ID after the slug (ex. post 1 returns "/bligpost.php?id=1"). Instead, it returns no ID. Where am I doing it wrong? (I have included my other attempts in comments.)

mysql_connect("$hosty","$uname","$paswd");
@mysql_select_db($dbnme) or die( "Unable to select database");

$name=$_POST['Title'];
$slug="blogpost.php?id=";
$auth=$_POST['Author'];
$date=$_POST['Date'];
$cont=$_POST['Content'];

//$query = ("INSERT INTO Blogs (Name, URL, Content, Author, Date) VALUES ('$name', '$slug', '$cont', '$auth', '$date')");
mysql_query("INSERT INTO Blogs (id, Name, URL, Content, Author, Date) VALUES (NULL, '$name', '$slug', '$cont', '$auth', '$date')");
//$pind = mysql_query("SELECT LAST_INSERT_ID()");
mysql_query("UPDATE Blogs SET URL=blogpost.php?id=`id` WHIERE id=LAST_INSERT_ID()");
//mysql_query("UPDATE Blogs SET URL=blogpost.php?id=".$pind." WHERE Content=".$cont);
mysql_close();

What you're actually doing wrong in the commented line when assigning to $pind is you expect the mysql_query to return your new id, but what it actually returns is a resource from which you must get the id using mysql_fetch_row or any similar function from the mysql_fetch_ family.

As for the uncommented row with WHIERE id=LAST_INSERT_ID(), it would probably work, but you're not concatenating the prefix string with your id. You should do it like this:

mysql_query("UPDATE blogs SET url = CONCAT('blogpost.php?id=', id) WHERE id = LAST_INSERT_ID()");

On the other hand I don't approve of your design of holding your urls in the database when you already have everything you need in the database (i.e. the id), so you should just prepend the "blogpost.php?id=" to the id you get when selecting that row and you're all set, this url of yours is completely unnecessary.

Oh and people are correct when they say this is deprecated, but it seems you're still learning so this is probably a little easier to grasp than the mysqli approach so you can stick with it for now and move up to mysqli once you're comfortable.

Hope that helps. Good luck.

Try with mysql_insert_id() like

mysql_query("INSERT INTO Blogs (id, Name, URL, Content, Author, Date) VALUES (NULL, '$name', '$slug', '$cont', '$auth', '$date')");
$id =  mysql_insert_id();
echo "My Last Inserted Id ".$id;

Tr this LINK And dont use mysql_* functions due to they are depricated,instead of it,use mysqli_* or PDO statements

And try to update your update query like

mysql_query("UPDATE Blogs SET URL = 'blogpost.php?id=$id' WHERE id=$id");

EDIT Based on your commented query try like

mysql_query("UPDATE Blogs SET URL=blogpost.php?id=$pind WHERE Content='".$cont."'")

or

mysql_query("UPDATE Blogs SET URL=blogpost.php?id=$pind WHERE Content='$cont'")

How about this?

   $pind = mysql_insert_id();

http://php.net/manual/en/function.mysql-insert-id.php

$id = mysql_insert_id();

In table id field should be auto increment field