Jump to content

Recommended Posts

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()) ;

}

Link to comment
https://forums.phpfreaks.com/topic/98786-adding-field-names-and-types-to-table/
Share on other sites

 

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

 

 

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];

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.

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.