I'm making a simple php/mysql blog and got the script below to add new messages.
The problem is that when I delete messages, and then post new messages the auto_increment of id starts at the last deleted message, and is not reset to the last table entry. So I have to run alter table tablename auto_increment = 1; to reset the table everytime I delete a message.
Is there a way to do this automatically ?
<?php
mysql_connect("localhost", "user","pass") or die("Could not connect: " . mysql_error());
mysql_select_db("db");
# The variables
$name=mysql_real_escape_string($_POST['name']);
$message=$_POST['message'];
$date = mysql_real_escape_string(date("d.m.Y | H:i:s"));
$ip=$_SERVER['REMOTE_ADDR'];
$browser=$_SERVER['HTTP_USER_AGENT'];
if(!$_POST) {
echo "<form method='post' action=".$_SERVER['PHP_SELF'].">";
echo "<input type='hidden' name='id'>";
echo "<input type='hidden' name='browser'>";
echo "<input type='hidden' name='ip'>";
echo "<input type='hidden' name='date'><br>";
echo "Your name :<br><input type='text' size='70' name='name'><br>";
echo "Your message : <br><textarea rows='20' cols='100' name='message'></textarea><br>";
echo "<input type='submit' value='Post'>";
echo "<input type='button' onclick='history.go(-1)' value='Cancel'>";
echo "</form>";
}
elseif(empty($_POST['name']) or empty($_POST['message'])) {
print "Fill inn all fields! <a class=cd href=add.php>Try again</a>";
}
else {
$sql = "INSERT INTO table SET name='$name', message='$message', date='$date', browser='$browser', ip='$ip'";
$query = mysql_query($sql) or die(mysql_error());
echo "Message posted.......<br><a href=index.php?id=blog.php>To mainpage</a>";
}
?>