Northern Flame Posted July 13, 2007 Share Posted July 13, 2007 This is the first project that I have ever done alone in PHP and I am stuck in 1 part. Auto creating a MySQL table. On the install.php file the user is asked to name his table. Once he names the table, it will be sent to a script called send_var.php which will then write it into an empty file called /var/tables.php It writes everything that I ask it to write, but it doesn't create the table. Am I suppose to somehow make the page create the table? Because I thought it would automatically create itself. heres the part of my code that writes the MySQL query to create the tables. if (!empty($errors)){ echo "<h1><font color=red>Error</font></h1><br>"; foreach ($errors as $key => $val){ echo $val; } } else { echo 'Alright, it is installed!<br> To start working on this website, just click <a href="/Admin/index.php">here!</a>'; $db = "var/tables.php"; $fh = fopen($db, 'w') or die("cant open file"); $stringData = ' <?php $dbhost = "'.$dbhost.'"; $dbuser = "'.$dbuser.'"; $dbpass = "'.$dbpass.'"; $dbname = "'.$dbname.'"; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to mysql"); mysql_select_db($dbname); $query =<?php "CREATE TABLE '.$table.'( '. 'username VARCHAR(20), '. 'encryptpass VARCHAR(255) NOT NULL, '. 'password VARCHAR(20) NOT NULL, '. 'website_title VARCHAR(30) NOT NULL, '. 'meta_key VARCHAR(100) NOT NULL, '. 'meta_des VARCHAR (100) NOT NULL,'. 'links BLOB(500) NOT NULL, '. 'top_h1 VARCHAR(100) NOT NULL, '. 'top_p BLOB(500) NOT NULL, '. 'site_name VARCHAR(30) NOT NULL, '. 'bottom_h2 VARCHAR(100) NOT NULL, '. 'bottom_p BLOB(500) NOT NULL)"; mysql_query($query); ?> '; fwrite($fh, $stringData); fclose($fh); IF ANYONE CAN HELP I'D REALLY APPRECIATE IT!!!! Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 13, 2007 Share Posted July 13, 2007 $query =<?php see that remove that <?php Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 o lol, i didn't notice that, thanks for the help I will try right now to see if the file work now. Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 it still didn't create the table Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 13, 2007 Share Posted July 13, 2007 Alright, you can try run those query on your phpmyadmin to ensure the query are right, then Try use this for your query, $query = "CREATE TABLE ".$table."( '. 'username VARCHAR(20), '. 'encryptpass VARCHAR(255) NOT NULL, '. 'password VARCHAR(20) NOT NULL, '. 'website_title VARCHAR(30) NOT NULL, '. 'meta_key VARCHAR(100) NOT NULL, '. 'meta_des VARCHAR (100) NOT NULL,'. 'links BLOB(500) NOT NULL, '. 'top_h1 VARCHAR(100) NOT NULL, '. 'top_p BLOB(500) NOT NULL, '. 'site_name VARCHAR(30) NOT NULL, '. 'bottom_h2 VARCHAR(100) NOT NULL, '. 'bottom_p BLOB(500) NOT NULL)"; Edit: Take note of the double quote used on $query and $table. Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 hmm.. thats a good idea lol, alright ill try running it on phpmyadmin and see what happens Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 I just ran it on phpmyadmin and the sql code worked, yet it still doesn't work on my project I even replaced CREATE TABLE '.$table.' with CREATE TABLE test and the table "test" wasn't created Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 13, 2007 Share Posted July 13, 2007 Ok, now let's find the error on your php script, replace mysql_query($query); with mysql_query($query)or die("MySQL Error: ".mysql_error()); Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 Im so confused... I did that but no error was displayed Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 or.... when plugging in the server for MySQL, do I need to include "http:"? becuase all I typed in was "p50mysql11.secureserver.net" Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 13, 2007 Share Posted July 13, 2007 You don't need to put http for it, try this $query =" CREATE TABLE `".$table."` ( `username` VARCHAR(20), `encryptpass` VARCHAR(255) NOT NULL, `password` VARCHAR(20) NOT NULL, `website_title` VARCHAR(30) NOT NULL, `meta_key` VARCHAR(100) NOT NULL, `meta_des` VARCHAR (100) NOT NULL, `links` BLOB(500) NOT NULL, `top_h1` VARCHAR(100) NOT NULL, `top_p` BLOB(500) NOT NULL, `site_name` VARCHAR(30) NOT NULL, `bottom_h2` VARCHAR(100) NOT NULL, `bottom_p` BLOB(500) NOT NULL ) "; Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 sorry to say it, but nope, it didnt work, i dont understand why ??? maybe we have to somehow activate /var/tables.php to work, or call it somehow, or does it automatically run itself? Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 because it does write what I tell it to write, it just doesnt create the table.... Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 13, 2007 Share Posted July 13, 2007 So the above script is /var/tables.php? Are you sure you got everything written correctly? Yes, you will need to run it once. P.S: Next time please use the edit button, thanks. Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 no, the above script is send_var.php and it writes the sql code into /var/tables.php Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 13, 2007 Share Posted July 13, 2007 Hmm ok, so it depends on where those script was written, then you will need to run it once for it to create the database. Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 oh, how can I run it? Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 13, 2007 Share Posted July 13, 2007 I would suggest the file only contains those query needed script, example like connecting to MySQL, $query and mysql_query(); Then on your installer file just require_once(); Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted July 13, 2007 Author Share Posted July 13, 2007 alright then thanks for helping me through this all, I really appreciate you staying here and helping me. Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 13, 2007 Share Posted July 13, 2007 You're welcome!(: Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.