Does anyone know a better way of getting this code to execute the mysql queries.
I am trying to Insert table records to a new table then update it.
so I need to call the table cms_homecontent first then insert those values to the new table cms_homecontent_versions and also ignore the id so that it is null and it can auto increment.
Then once that has completed continue updating cms_homecontent table with the data that was submitted.
Any help would be greatly appreciated.
function update_homecontent($p) {
$id = mysql_real_escape_string($p['id']);
$pageorder = mysql_real_escape_string($p['page_order']);
$page = mysql_real_escape_string($p['page']);
$title = mysql_real_escape_string($p['title']);
$body = mysql_real_escape_string($p['body']);
if(!$title || !$body):
if(!$title):
echo '<div class="bad">The title is required!</div>';
endif;
if(!$body):
echo '<div class="bad">The body is required!</div>';
endif;
echo '<p><a href="update-home-content.php?id=' . $id . '">Try Again</a></p>';
else:
$sql=("SELECT * FROM cms_homecontent WHERE id = '$id'");
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) != 0):
while($row = mysql_fetch_assoc($res)) {
$old_id= $row['id'];
$old_page_order= $row['page_order'];
$old_page= $row['page'];
$old_title= $row['title'];
$old_body= $row['body'];
$id = mysql_real_escape_string($id);
$sql .= (mysql_query("begin"));
$sql .= (mysql_query("INSERT INTO cms_homecontent_versions(id, page_order, page, title, body, timestamp)
VALUES(null, '$old_page_order', '$old_page', '$old_title', '$old_body', NOW()) SELECT * FROM cms_homecontent WHERE id='$id';"));
$sql .= (mysql_query("UPDATE cms_homecontent SET page_order = '$pageorder', page = '$page', title = '$title', body = '$body', timestamp = NOW() WHERE id ='$id';"));
if (mysql_error()) {
$sql = (mysql_query("rollback"));
}
else {
$sql = (mysql_query("commit"));
}
echo "<div class='good'>Content Updated Successfully</div>";
}
endif;
endif;
}