Hey everyone,
So much for thinking MySql is smart, but what's an easy way to update an auto increment field after deleting a record. the code is below, please help, specifically part 3 is where the magic happens. thanks in advance. by the way id is the name of the auto increment field.
Another stupid matter is, I can't delete a table, lol. the code is posted below, thanks again.
<?php
if (isset($_POST['action']) && $_POST['action'] == 'submitted')
{//Part 2
$dname = $_POST[dname];
$tname = $_POST[tname];
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$dname");
echo "Database: $dname<br>Table: $tname<br>";
$result = mysql_query("select * from $tname");
echo "<table width='10%' border=2><tr>";
if (mysql_num_rows($result)>=0)
{
//loop thru the field names to print the correct headers
$i = 0;
while ($i < mysql_num_fields($result))
{
echo "<th>". mysql_field_name($result, $i) . "</th>";
$i++;
}
echo "</tr>";
while($row=mysql_fetch_assoc($result))
{
echo "<tr>";
foreach($row as $col=>$val)
{
echo "<td>$val</td>";
}
echo "</tr>";
}
}
$result = mysql_query("select * from $tname");
$i = 0;
echo "<form action=\"deleterecord.php\" method=\"post\">";
$meta = mysql_fetch_field($result, 0);
echo "$meta->name:<input type=\"text\" name=\"fname[]\" /><br>";
mysql_close($con);
echo "<input type=\"hidden\" name=\"action\" value=\"lastsubmit\" />";
echo "<input type=\"hidden\" name=\"field_dname\" value=\"$dname\" />";
echo "<input type=\"hidden\" name=\"field_tname\" value=\"$tname\" />";
echo "<input type=\"submit\" name=\"submit\" value=\"Show Field(s)\" />
</form>";
}
else if (isset($_POST['action']) && $_POST['action'] == 'lastsubmit')
{//Part 3
$wp = $_POST['field_dname'];
$motto = $_POST['field_tname'];
$fname2=$_POST['fname'];
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($wp, $con);
$result = mysql_query("select * from $motto");
$result1 = "DELETE FROM $motto WHERE id = '$fname2[0]'";
mysql_query($result1);
echo "record deleted<br>";
mysql_close($con);
echo <<< Here
<br>
<ul>Please select from the items below:
<li> <a href="http://localhost/default/showrecord.php">Show Records</a></li>
<li> <a href="http://localhost/default/homepage.html">Return to Home page</a></li>
</ul>
Here;
}
else
{//Part 1
echo <<< HERE
<form action="deleterecord.php" method="post">
Database Name: <input type="text" name="dname" />
Table Name: <input type="text" name="tname" />
<input type="hidden" name="action" value="submitted" />
<input type="submit" name="submit" value="Delete Record" />
</form>
HERE;
}
?>
deleting table code isn't working
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dname, $con);
$sql = "DROP TABLE $tname";
mysql_query($sql,$con);
mysql_close($con);