peuge Posted March 31, 2008 Share Posted March 31, 2008 I have created something which allows me to add a new table to a database. I have added the table and now want to add the users defined field names and types. I was able to make the user names work but now I need to incorporate the field type into this. I can do either on on their own but am not sure on how to combine the two? Here is my code for altering the table "$tname". $name_array is an array called in from a form of the field names as is $type_array. Any help is much apprectiated. I have tried the following but it doesn't work, it was just an attempt and now I am stuck. Thanks // PUT THE NEW FIELD NAMES AND TYPES IN THE TABLE foreach (($name_array as $n)AND($type_array as $m)) { mysql_query("ALTER TABLE $tname add $n $m ") or die("ERROR: " . mysql_error()) ; } Quote Link to comment https://forums.phpfreaks.com/topic/98786-adding-field-names-and-types-to-table/ Share on other sites More sharing options...
MadTechie Posted March 31, 2008 Share Posted March 31, 2008 what are you posting ? if $n= `Test` and $m = "VARCHAR( 100 )" to the total is ALTER TABLE `table` ADD VARCHAR( 100 ) NOT NULL that should work what error are you getting Quote Link to comment https://forums.phpfreaks.com/topic/98786-adding-field-names-and-types-to-table/#findComment-505489 Share on other sites More sharing options...
peuge Posted March 31, 2008 Author Share Posted March 31, 2008 This is my code for where the user inputs the data: echo "<form action=tblsuccess.php method=post>"; echo "Enter Table Name: "; echo "<input type=text name=tname>"; echo "<br />"; while ($i<=$num) { echo "Field Name " . $i; echo "<input type=text name=fname[$i] id=$i>"; echo "<select> <option name=ftype[$i] value=varchar(30)>varchar</option> <option name=ftype[$i] value=int(30)>int</option> <option name=ftype[$i] value=str(30)>str</option> </select>"; echo "<br />"; $i++; } echo "<input type=submit value=submit>"; echo "</form>"; This is the code for where I get the data and add the table and its relevant field names and types. $con = mysql_connect("localhost", "root", ""); if (!$con) { die('Could not connect: ' . mysql_error()); } session_start(); $dbinfo = $_SESSION['dbinfo']; mysql_select_db($dbinfo,$con) or die(mysql_error()); $name_array = $_POST['fname']; $type_array = $_POST['ftype']; $tname = $_POST['tname']; //CREATE THE TABLE WITH THE SPECIFIED NAME mysql_query("CREATE TABLE $tname( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id))") or die("ERROR: " . mysql_error()); // PUT THE NEW FIELD NAMES AND TYPES IN THE TABLE foreach (($name_array as $n)AND($type_array as $m)) { echo $n; mysql_query("ALTER TABLE $tname add $n $m ") or die("ERROR: " . mysql_error()) ; } The error I get is this: Parse error: parse error, unexpected T_AS in D:\Programs\EasyPHP 2.0b1\www\DataBase\tblsuccess.php on line 21 thanks Quote Link to comment https://forums.phpfreaks.com/topic/98786-adding-field-names-and-types-to-table/#findComment-505496 Share on other sites More sharing options...
MadTechie Posted March 31, 2008 Share Posted March 31, 2008 Okay well the STR in the drop down won't work What are you trying to do here ? foreach (($name_array as $n)AND($type_array as $m)) can you do a print_r($_POST); so i can see what data you have i think you mean this! foreach ($name_array as $k => $n) { $m = $type_array[$k]; Quote Link to comment https://forums.phpfreaks.com/topic/98786-adding-field-names-and-types-to-table/#findComment-505510 Share on other sites More sharing options...
peuge Posted March 31, 2008 Author Share Posted March 31, 2008 This is the output: Notice: Undefined index: ftype in D:\Programs\EasyPHP 2.0b1\www\DataBase\tblsuccess.php on line 11 Array ( [tname] => TESTING [fname] => Array ( [1] => test [2] => test [3] => test ) ) With the code below I am trying to add the field $n with the type $m. foreach (($name_array as $n)AND($type_array as $m)) { mysql_query("ALTER TABLE $tname add $n $m ") or die("ERROR: " . mysql_error()) ; } So for example: I choose 3 field names: A, B and C with the type varchar(30). Now I want to add those fields with those types to the specified table. Oh and the str wasn't meant to be there. Quote Link to comment https://forums.phpfreaks.com/topic/98786-adding-field-names-and-types-to-table/#findComment-505517 Share on other sites More sharing options...
MadTechie Posted March 31, 2008 Share Posted March 31, 2008 try echo "<select name=ftype[$i] > <option value=varchar(30)>varchar</option> <option value=int(30)>int</option> <option value=str(30)>str</option> </select>"; echo "<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/98786-adding-field-names-and-types-to-table/#findComment-505521 Share on other sites More sharing options...
peuge Posted March 31, 2008 Author Share Posted March 31, 2008 Sweet that works Much appreciated. Have a good day Quote Link to comment https://forums.phpfreaks.com/topic/98786-adding-field-names-and-types-to-table/#findComment-505527 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.