Dear Experts,
Is this the best way for me to generate a query which I will later execute to insert the data into the databases?
<?php
$connection = mysql_connect('server', 'user', 'pass') or die (mysql_error());
$db = mysql_select_db('database'
, $connection) or die (mysql_error());
$query = "Select DataID + 1 as NextID From tblData where DataID < 18581088 Order By DataID desc limit 1";
$result = mysql_query($query) or die('Error, query `' . $query . '` failed');
$row = mysql_fetch_array($result)
;
extract($row);
$the_heading = "the heading goes here";
$the_date = "2008-06-26 00:00:00"';
$the_content = "the content goes here";
$the_categories = "4380138181,43830184182,43
801558183,
4380158184
,438065181
85,4384181
86";
$generate_query = "insert into tblData ( Heading, Date, Contents, DataID ) values ( '$the_heading', '$the_date', '$the_content', $NextID );";
$categories = explode(",",the_categories
);
for ($i=0;$i<count($categories
);$i++){
$query = "select GroupName from tblIDLookup where DataID = $categories[$i]";
$result = mysql_query($query) or die('Error, query `' . $query . '` failed');
$row = mysql_fetch_array($result)
;
extract($row);
$generate_query .= "<br />insert into tblCat (DataID, CatID, CatName) values ($NextID, $categories[$i], $GroupName);";
}
echo $generate_query;
mysql_close($connection);
?>
It does the following
1. Get the next id but ignore the higher number ids
2. The 4 variables $the_heading, $the_date, $the_content, $the_categories will be textboxes on the users screen which will be filled in by the user
3. I loop through the categories to get the category name from another table based on the exploded category ids, then generate the next query, this continues until all categores are found
I feel this is not very optimum and not using best practices. Any suggestions for improvements?
When I say "improve" or "optimum", I mean is my code good practice, is it easy on the server, is it fast for the user to download etc etc.
Thanks.