Page 2 of 2
Posted: Fri Apr 09, 2004 6:16 am
by feyd
print_r($_POST) will show you the layout of the variable. If it doesn't show something like:
Code: Select all
[field_name] => Array(blah,blah,blah)
somewhere in there, then it's definitely not an array. How are you setting field_name? Can you post that code?
Posted: Fri Apr 09, 2004 6:29 am
by copachumpy
Here is the complete code for the database project:
Step 1, input table name, and number of fields:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 1</TITLE>
</HEAD>
<BODY>
<br />
<h2><b>DATABASE TABLE GENERATOR</b></h2>
<FORM method="POST" action="do_showfielddef.php">
<P>Table Name:<br><INPUT type="text" name="table_name" size=30></p>
<P>Number of Fields:<br><INPUT type="text" name="num_fields" size=5></p>
<INPUT type="submit" value="Go to Step 2">
</FORM>
</BODY>
</HTML>
Step 2 Define fields for table:
Code: Select all
<?php
//validate important input
if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
header("Location:http://............../show_createtable.html");
exit;
}
//begin creating form for display
$form_block ="
<FORM METHOD="POST " ACTION="do_createtable.php">
<INPUT TYPE="hidden " NAME="table_name" VALUE="$_POST[table_name]">
<TABLE CELLSPACING=5 CELLPADDING=5>
<TR>
<TH>FIELD NAME</TH><TH>FIELD TYPE</TH><TH>FIELD LENGTH</
TH><TH>PRIMARY KEY?</TH><TH>AUTO-INCREMENT?</TH></TR>";
//count from 0 until you reach the number of fields
for ($i =0;$i < $_POST[num_fields];$i++){
//add to the form,one row for each field
$form_block .="
<TR>
<TD ALIGN=CENTER><INPUT TYPE="text" NAME="field_name[]" SIZE="30"></TD>
<TD ALIGN=CENTER>
<SELECT NAME="field_type[]">
<OPTION VALUE="char ">char</OPTION>
<OPTION VALUE="date ">date</OPTION>
<OPTION VALUE="float ">float</OPTION>
<OPTION VALUE="int ">int</OPTION>
<OPTION VALUE="text ">text</OPTION>
<OPTION VALUE="varchar ">varchar</OPTION>
</SELECT>
</TD>
<TD ALIGN=CENTER><INPUT TYPE="text" NAME="field_length[]" SIZE="5 "></TD>
<TD ALIGN=CENTER><INPUT TYPE="checkbox" NAME="primary[]" VALUE="Y "></TD>
<TD ALIGN=CENTER><INPUT TYPE="checkbox" NAME="auto_increment[]" VALUE="Y "></TD>
</TR>";
}
//finish up the form
$form_block .="
<TR>
<TD ALIGN=CENTER COLSPAN=3><INPUT TYPE="submit" VALUE="Create Table"></TD>
</TR>
</TABLE>
</FORM>";
?>
<HTML>
<HEAD>
<TITLE>Create a Database Table:Step 2</TITLE>
</HEAD>
<BODY>
<H1>Please define fields for <? echo "$_POST[table_name]"; ?></H1>
<? echo "$form_block"; ?>
</HTML>
Step 3, Create the table:
Code: Select all
<?php
// create connection
$conn = mysql_connect("host","user","pass") or die($sql);
// select database
$db = mysql_select_db("testDB", $conn) or die($sql);
$sql = "CREATE TABLE".$_POST['table_name']."";
for ($i = 0; $i < count($_POST['field_name']); $i++) {
$sql .= "$_POST[field_name][$i] $_POST[field_type][$i]";
if ($_POST['auto_increment'][$i] = "Y") {
$additional = "NOT NULL auto_increment";
} else {
$additional = ",";
}
if ($_POST['primary'][$i] == "Y") {
$additional .= ", primary key (".$_POST[field_name][$i].")";
} else {
$additional = ",";
}
if ($_POST['field_length'][$i] != "") {
$sql .= "$_POST[field_length][$i]";
} else {
$sql .= ",";
}
}
$sql = substr($sql, 0,-1);
$sql .= ")";
// execute SQL query and get result
$sql_result = mysql_query($sql,$conn) or die($sql);
//print success message
if ($sql_result) {
echo "<P>$_POST[table_name] has been created!</p>";
}
?>
<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 3</TITLE></HEAD>
<BODY>
<h1>Adding table to
<?php echo $_POST[table_name]; ?>
</h1>
<? echo "$msg"; ?>
</BODY>
</HTML>
Posted: Tue Apr 13, 2004 9:30 am
by pickle
To set up a post variable as an array, declare the variable as, for example,
Code: Select all
<input type = "text" name = "field_nameї]" value = "Name">
Note the square brackets.
Posted: Tue Apr 13, 2004 9:40 am
by AznFuman
i thought u only use the [] for "select"
but i havent used text for a while, so I'll take ur word on it
Posted: Tue Apr 13, 2004 9:56 am
by pickle
Well, generally that's the only time you'd use the []. My next question was why you were putting text field values into an array, and not giving each text field it's own unique name?
Posted: Tue Apr 13, 2004 10:03 am
by AznFuman
<input type="something" name="something[]" value ="something">
notice the [] !
hope this helps latez
Posted: Tue Apr 13, 2004 11:46 am
by copachumpy
pickle wrote:Well, generally that's the only time you'd use the []. My next question was why you were putting text field values into an array, and not giving each text field it's own unique name?
As I'm very new to this, I was just implementing PHP the way the sources that I'm learning from are saying.
Is this going to be a problem?
Posted: Tue Apr 13, 2004 12:32 pm
by copachumpy
I have now turned error reporting on, and I get an error stating that:
Notice: Undefined index: primary in /var/www/html/Development/do_createtable.php on line 22
so in my first code I have defined the primary key:
Code: Select all
<TD ALIGN=CENTER><INPUT TYPE="checkbox" NAME="'primary_key'[]" VALUE="Y "></TD>
and in the follow up page: (Lines 22 on)
Code: Select all
if ($_POST['primary_key'][$i] == "Y") {
$additional .= ", primary_key (".$_POST[field_name][$i].")";
} else {
$additional = ",";
}
I dont understand, please Help me, I'm going crazy!!!!
Posted: Tue Apr 13, 2004 12:44 pm
by pickle
The [] is part of the variable name so this:
Code: Select all
<INPUT TYPE="checkbox" NAME="'primary_key'[]" VALUE="Y ">
should be:
Code: Select all
<input type= "checkbox" name = "primary_key[]" value = "Y">
Also, have you considered using heredocs? They greatly reduce the need to put in escape characters. For example, your line could be as simple as
Code: Select all
echo <<<INPUT
<input type = "checkbox" name = "primary_key[]" value = "Y">
INPUT;
I use them all the time because they make it easier to see exactly what is being output.
Posted: Tue Apr 13, 2004 12:59 pm
by copachumpy
Thanks for the help, and sorry to be a pain!
I have changed the line so the [] is a part of the variable, yet I still get the error:
Code: Select all
<TD ALIGN=CENTER><INPUT TYPE="checkbox" NAME="primary_key[]" VALUE="Y"></TD>
any further ideas?
Posted: Tue Apr 13, 2004 1:03 pm
by pickle
what is line 22 exactly?
Posted: Tue Apr 13, 2004 1:08 pm
by copachumpy
Line 22 is:
Code: Select all
if ($_POST['primary_key'][$i] == "Y") {
the rest of the loop continues:
Code: Select all
$additional .= ", 'primary_key' (".$_POST[field_name][$i].")";
} else {
$additional = ",";
}
?>
Posted: Sun Apr 18, 2004 12:23 pm
by sutejok
probably the error is in this line:
<TD ALIGN=CENTER><INPUT TYPE="checkbox\" NAME=\"'primary_key'[]\" VALUE=\"Y \"></TD>
and maybe you should change it to:
<TD ALIGN=CENTER><INPUT TYPE=\"checkbox\" NAME=\"'primary_key'[]\" VALUE=\"Y \"></TD>
I'm not sure if i'm answering the question cos i did not really read the whole thread and i see this line pretty weird..