May 26, 201115 yr I need to create an install.php file that creates my database tables on live servers. Does anyone know and good tutorials or have any documentation on this. Thanks
May 26, 201115 yr I need to create an install.php file that creates my database tables on live servers. Does anyone know and good tutorials or have any documentation on this. Thanks Heres an example of one i built for a blog define('site',true);//Don't remove this, site will not work if done so define('is_installing',true);//Define that our system is installing /**Include bootstrap**/ require dirname(dirname(__FILE__)) . '/load.php'; /**Create needed vars**/ $blog_name = clean_data(/****/_post('blog_name')/****/); $admin_username = clean_data(/****/_post('blog_username')/****/); $admin_password = clean_data(/****/_post('blog_password')/****/); $admin_email = clean_data(/****/_post('admin_email')/****/); /**tables**/ /****POST TABLE***/ $blog_post_table = " CREATE TABLE IF NOT EXISTS `blog_post` ( `POST_ID` int(11) NOT NULL AUTO_INCREMENT, `date_posted` datetime NOT NULL, `title` text NOT NULL, `author` varchar(255) NOT NULL, `post` longtext NOT NULL, PRIMARY KEY (`POST_ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; /****COMMENTS TABLE***/ $blog_comments_table = " CREATE TABLE IF NOT EXISTS `blog_comments` ( `COMMENT_ID` int(11) NOT NULL AUTO_INCREMENT, `date_created` datetime NOT NULL, `author` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `pid` int(11) NOT NULL, `post_name` varchar(255) NOT NULL, `comment` longtext NOT NULL, `ip_address` varchar(255) NOT NULL, `admin_state` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`COMMENT_ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; /****REPLIES TABLE***/ $blog_replies_table = " CREATE TABLE IF NOT EXISTS `blog_replies` ( `REPLY_ID` int(11) NOT NULL AUTO_INCREMENT, `cid` int(22) NOT NULL, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `reply` text NOT NULL, `date_created` datetime NOT NULL, `ip_address` varchar(255) NOT NULL, `admin_state` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`REPLY_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; /****ADMIN TABLE***/ $blog_admin_table = " CREATE TABLE IF NOT EXISTS `admin` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; /****CONFIG TABLE***/ $blog_config_table = " CREATE TABLE IF NOT EXISTS `config` ( `SETTING_ID` int(11) NOT NULL AUTO_INCREMENT, `blog_title` varchar(255) NOT NULL, `admin_email` varchar(255) NOT NULL, PRIMARY KEY (`SETTING_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; /****PHOTO TABLE***/ $blog_photo_table = " CREATE TABLE IF NOT EXISTS `photos` ( `PHOTO_ID` int(11) NOT NULL AUTO_INCREMENT, `pic_url` text NOT NULL, PRIMARY KEY (`PHOTO_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <!--Description and keywords for your blog go below Generated by blogging system v1.0.0--> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Blogging system v1.0.0 Installation</title> <style type='text/css'> #INSTALL{ padding:10px 20px 10px 20px; margin:20px auto; width:50%; } .error{ color:#FF0033; font-weight:bold; } </style> </head> <body> <div id='INSTALL'> <?php /**Check if user wants to install**/ if(_post('install')){ if($blog_name && $admin_username && $admin_password){ /**BLOG POST TABLE CREATION**/ if(db()->bl_query($blog_post_table)){ echo "Your <b>BLOG POST</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG POST</b> table could not be created!</span><br /><br />"; } /**BLOG COMMENTS TABLE CREATION**/ if(db()->bl_query($blog_comments_table)){ echo "Your <b>BLOG COMMENTS</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG COMMENTS</b> table could not be created!</span><br /><br />"; } /**BLOG REPLIES TABLE CREATION**/ if(db()->bl_query($blog_replies_table)){ echo "Your <b>BLOG REPLIES</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG REPLIES</b> table could not be created!</span><br /><br />"; } /**BLOG ADMIN TABLE CREATION**/ if(db()->bl_query($blog_admin_table)){ /**Insert needed data**/ $pre_insert = db()->bl_query("INSERT INTO admin (`username`,`password`) VALUES('$admin_username','".pass_secure($admin_password)."')"); echo "Your <b>BLOG ADMIN</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG ADMIN</b> table could not be created!</span><br /><br />"; } /**CONFIG TABLE CREATION**/ if(db()->bl_query($blog_config_table)){ /**Insert needed data**/ $pre_insert2 = db()->bl_query("INSERT INTO config (`blog_title`,`admin_email`) VALUES('$blog_name','$admin_email')"); echo "Your <b>BLOG CONFIG</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG CONFIG</b> table could not be created!</span><br /><br />"; } /**PHOTO TABLE CREATION**/ if(db()->bl_query($blog_photo_table)){ /**Insert needed data**/ echo "Your <b>BLOG PHOTOS</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG PHOTOS</b> table could not be created!</span><br /><br />"; } }else{ echo "<p>Please fill out all required fields!</p>"; } } ?> <h1>Install This Blog</h1> <h4>Configuration Details</h4> <p>Fill in your details for this installation below!</p> <form method='post' action='install.php'> <table cellpadding='3' cellspacing='3'> <tr> <td>*Name Of Blog</td><td><input type='text' name='blog_name' /></td> </tr> <tr> <td>*Admin Username For Blog</td><td><input type='text' name='blog_username' /></td> </tr> <tr> <td>*Admin Password For Blog</td><td><input type='text' name='blog_password' /></td> </tr> <tr> <td>*Admin E-mail For Blog</td><td><input type='text' name='admin_email' /></td> </tr> </table> <div><br /><br /><input type='submit' name='install' value='Create Needed Tables' /></div> </form> </div><!--Install--> </body> </html>
May 27, 201115 yr Heres an example of one i built for a blog define('site',true);//Don't remove this, site will not work if done so define('is_installing',true);//Define that our system is installing /**Include bootstrap**/ require dirname(dirname(__FILE__)) . '/load.php'; /**Create needed vars**/ $blog_name = clean_data(/****/_post('blog_name')/****/); $admin_username = clean_data(/****/_post('blog_username')/****/); $admin_password = clean_data(/****/_post('blog_password')/****/); $admin_email = clean_data(/****/_post('admin_email')/****/); /**tables**/ /****POST TABLE***/ $blog_post_table = " CREATE TABLE IF NOT EXISTS `blog_post` ( `POST_ID` int(11) NOT NULL AUTO_INCREMENT, `date_posted` datetime NOT NULL, `title` text NOT NULL, `author` varchar(255) NOT NULL, `post` longtext NOT NULL, PRIMARY KEY (`POST_ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; /****COMMENTS TABLE***/ $blog_comments_table = " CREATE TABLE IF NOT EXISTS `blog_comments` ( `COMMENT_ID` int(11) NOT NULL AUTO_INCREMENT, `date_created` datetime NOT NULL, `author` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `pid` int(11) NOT NULL, `post_name` varchar(255) NOT NULL, `comment` longtext NOT NULL, `ip_address` varchar(255) NOT NULL, `admin_state` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`COMMENT_ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; /****REPLIES TABLE***/ $blog_replies_table = " CREATE TABLE IF NOT EXISTS `blog_replies` ( `REPLY_ID` int(11) NOT NULL AUTO_INCREMENT, `cid` int(22) NOT NULL, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `reply` text NOT NULL, `date_created` datetime NOT NULL, `ip_address` varchar(255) NOT NULL, `admin_state` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`REPLY_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; /****ADMIN TABLE***/ $blog_admin_table = " CREATE TABLE IF NOT EXISTS `admin` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; /****CONFIG TABLE***/ $blog_config_table = " CREATE TABLE IF NOT EXISTS `config` ( `SETTING_ID` int(11) NOT NULL AUTO_INCREMENT, `blog_title` varchar(255) NOT NULL, `admin_email` varchar(255) NOT NULL, PRIMARY KEY (`SETTING_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; /****PHOTO TABLE***/ $blog_photo_table = " CREATE TABLE IF NOT EXISTS `photos` ( `PHOTO_ID` int(11) NOT NULL AUTO_INCREMENT, `pic_url` text NOT NULL, PRIMARY KEY (`PHOTO_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; "; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <!--Description and keywords for your blog go below Generated by blogging system v1.0.0--> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Blogging system v1.0.0 Installation</title> <style type='text/css'> #INSTALL{ padding:10px 20px 10px 20px; margin:20px auto; width:50%; } .error{ color:#FF0033; font-weight:bold; } </style> </head> <body> <div id='INSTALL'> <?php /**Check if user wants to install**/ if(_post('install')){ if($blog_name && $admin_username && $admin_password){ /**BLOG POST TABLE CREATION**/ if(db()->bl_query($blog_post_table)){ echo "Your <b>BLOG POST</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG POST</b> table could not be created!</span><br /><br />"; } /**BLOG COMMENTS TABLE CREATION**/ if(db()->bl_query($blog_comments_table)){ echo "Your <b>BLOG COMMENTS</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG COMMENTS</b> table could not be created!</span><br /><br />"; } /**BLOG REPLIES TABLE CREATION**/ if(db()->bl_query($blog_replies_table)){ echo "Your <b>BLOG REPLIES</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG REPLIES</b> table could not be created!</span><br /><br />"; } /**BLOG ADMIN TABLE CREATION**/ if(db()->bl_query($blog_admin_table)){ /**Insert needed data**/ $pre_insert = db()->bl_query("INSERT INTO admin (`username`,`password`) VALUES('$admin_username','".pass_secure($admin_password)."')"); echo "Your <b>BLOG ADMIN</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG ADMIN</b> table could not be created!</span><br /><br />"; } /**CONFIG TABLE CREATION**/ if(db()->bl_query($blog_config_table)){ /**Insert needed data**/ $pre_insert2 = db()->bl_query("INSERT INTO config (`blog_title`,`admin_email`) VALUES('$blog_name','$admin_email')"); echo "Your <b>BLOG CONFIG</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG CONFIG</b> table could not be created!</span><br /><br />"; } /**PHOTO TABLE CREATION**/ if(db()->bl_query($blog_photo_table)){ /**Insert needed data**/ echo "Your <b>BLOG PHOTOS</b> table has been created<br /><br />"; }else{ echo "<span class='error'>Your <b>BLOG PHOTOS</b> table could not be created!</span><br /><br />"; } }else{ echo "<p>Please fill out all required fields!</p>"; } } ?> <h1>Install This Blog</h1> <h4>Configuration Details</h4> <p>Fill in your details for this installation below!</p> <form method='post' action='install.php'> <table cellpadding='3' cellspacing='3'> <tr> <td>*Name Of Blog</td><td><input type='text' name='blog_name' /></td> </tr> <tr> <td>*Admin Username For Blog</td><td><input type='text' name='blog_username' /></td> </tr> <tr> <td>*Admin Password For Blog</td><td><input type='text' name='blog_password' /></td> </tr> <tr> <td>*Admin E-mail For Blog</td><td><input type='text' name='admin_email' /></td> </tr> </table> <div><br /><br /><input type='submit' name='install' value='Create Needed Tables' /></div> </form> </div><!--Install--> </body> </html> Yh as shown above connect to the database then using the "mysqli_query" function insert the mysql querys into the database.
May 27, 201115 yr Alternatively, you could create an array of queries: <?php $q[] = "INSERT INTO ....": $q[] = "UPDATE ..."; // Loop through all queries foreach($q as $iq){ $sql->query($iq); }?>
Create an account or sign in to comment