I have some code between a CREATE TABLE IF NOT EXISTS and a LOAD DATA LOCAL INFILE that checks if there are records that already exist in the table.
If so I want to truncate so the user can start over with an empty table.
Here is what I have so far.
$checktable = $db->sql_query("SELECT * FROM ".$prefix."$table_name");
$table_total = $db->sql_numrows($checktable);
//We already have records in table
if ($table_total > 0) {
echo "<form action='".$_server['php_self']."' method='post'";
OpenTable();
echo"<tr><td>There are records already loaded from a previous file (TL Session).</td></tr>";
echo"<tr><td>You must empty the table and start over.</td></tr>";
echo"<tr>"
. "<td align=\"center\"><input type=\"Submit\" name=\"TruncateTable\" value=\"Continue\"></td>"
. "</tr>";
Closetable();
echo"</form>";
die();
}
if (isset($_POST[TruncateTable])) {
$success = TruncateTable($table_name);
}
function TruncateTable($table_name) {
$sql = ("TRUNCATE TABLE ".$prefix."$table_name");
$result = $db->sql_query($sql);
if (!$result) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
}
Does this look correct and is it the best way of handling this issue?