Jump to content

Creating a MYSQL table from a form submission


justinjkiss

Recommended Posts

The code below is the php. I put ... in to represent layout html above and below. There is an include at the top i didnt include in code.

 

I got rid of all errors so the page will load. Now i cant get the form to submit to the database and create a table. The table names are the same as im using for other tables. I just want to be able to create the tables quicker when we add a new dealer.

 

I feel the problem lies withing the $sql line. Mostly to the $user not being read correctly. I am new and i know the order of single/double quotes make a difference but im not sure which way they should be.

 

Does the $sql?

 

...
<?
/**
* The user is already logged in and not allowed to register unless admin.
*/
if($session->isAdmin()){
echo "[<a href=\"../dealercenter.php\">Dealer Center Home</a>]   ";
echo "[<a href=\"process.php\">Logout</a>]";
  ?>

<br /><br /><p>Add Warranty Table</p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr>
      <td>Username:</td><td><input type="text" name="user" maxlength="30"></td>
</tr>
<tr>
      <td><input type="submit" value="Add Warranty table!"></td>
</tr>
</table>
</form>

<?
$user=$_POST['user'];
$sql = "CREATE TABLE `custkis6_login`.`$user` ( `model` varchar( 10 ) NOT NULL ,
`serial` int( 10 ) NOT NULL ,
`servdate` varchar( 10 ) NOT NULL ,
`faildate` varchar( 10 ) NOT NULL ,
`repairdate` varchar( 10 ) NOT NULL ,
`comment` longtext NOT NULL ,
`traveltime` mediumint( 3 ) NOT NULL ,
`repairtime` mediumint( 3 ) NOT NULL ,
`claimhours` mediumint( 3 ) NOT NULL ,
`hourrate` varchar( 3 ) NOT NULL ,
`claimname` varchar( 50 ) NOT NULL ,
PRIMARY KEY ( `serial` ) ) ENGINE = MyISAM DEFAULT CHARSET = latin1;[...]";

   }
   else if($session->logged_in){
   	echo "<p>Access Denied</p>";
echo "<p>We're sorry <b>$session->username</b>, but you've not allowed to view this page. "
   		."<a href=\"../dealercenter.php\">Main</a>.</p>";


}
...

Link to comment
Share on other sites

try to echo out your $sql string and see if the statement looks correct. If your $user variable is empty, you'll have to put in some error checking to make sure it can't be that way, and then find out why it is empty.

Link to comment
Share on other sites

no errors are popping up when i hit submit on the form.

 

user has all rights. I rechecked and tried again but still nothing.

 

When i enter killer as the user i get this when i echo $sql

 

CREATE TABLE `custkis6_login` `killer` ( `model` varchar( 10 ) NOT NULL , `serial` int( 10 ) NOT NULL , `servdate` varchar( 10 ) NOT NULL , `faildate` varchar( 10 ) NOT NULL , `repairdate` varchar( 10 ) NOT NULL , `comment` longtext NOT NULL , `traveltime` mediumint( 3 ) NOT NULL , `repairtime` mediumint( 3 ) NOT NULL , `claimhours` mediumint( 3 ) NOT NULL , `hourrate` varchar( 3 ) NOT NULL , `claimname` varchar( 50 ) NOT NULL , PRIMARY KEY ( `serial` ) ) ENGINE = MyISAM DEFAULT CHARSET = latin1;[...]

 

Link to comment
Share on other sites

since you already selected the database in your connection string, just use the table name you want.

 

$sql = "CREATE TABLE `$user` ( `model` varchar( 10 ) NOT NULL ,
`serial` int( 10 ) NOT NULL ,
`servdate` varchar( 10 ) NOT NULL ,
`faildate` varchar( 10 ) NOT NULL ,
`repairdate` varchar( 10 ) NOT NULL ,
`comment` longtext NOT NULL ,
`traveltime` mediumint( 3 ) NOT NULL ,
`repairtime` mediumint( 3 ) NOT NULL ,
`claimhours` mediumint( 3 ) NOT NULL ,
`hourrate` varchar( 3 ) NOT NULL ,
`claimname` varchar( 50 ) NOT NULL ,
PRIMARY KEY ( `serial` ) ) ENGINE = MyISAM DEFAULT CHARSET = latin1;[...]";
$res = mysql_query($sql) or die(mysql_error());

 

make sure you have the "or die()" to give you any errors encountered.

 

Ray

Link to comment
Share on other sites

putting the or die causes a problem like i thought it would.

 

The connection to the database is from the include but the include also keeps the connection open until logout is hit because it is a protected section with a login.

 

would i be better just putting this into a separate php file and have the form process it instead. Then just use a separate login and so i can close the connection each time.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.