PHP MySQL error code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
copachumpy
Forum Newbie
Posts: 11
Joined: Thu Apr 08, 2004 5:10 am

Post 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>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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&#1111;]" value = "Name">
Note the square brackets.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
AznFuman
Forum Newbie
Posts: 6
Joined: Fri Apr 02, 2004 3:20 pm

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
AznFuman
Forum Newbie
Posts: 6
Joined: Fri Apr 02, 2004 3:20 pm

Post by AznFuman »

<input type="something" name="something[]" value ="something">
notice the [] !
hope this helps latez
copachumpy
Forum Newbie
Posts: 11
Joined: Thu Apr 08, 2004 5:10 am

Post 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?
copachumpy
Forum Newbie
Posts: 11
Joined: Thu Apr 08, 2004 5:10 am

Post 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!!!!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
copachumpy
Forum Newbie
Posts: 11
Joined: Thu Apr 08, 2004 5:10 am

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

what is line 22 exactly?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
copachumpy
Forum Newbie
Posts: 11
Joined: Thu Apr 08, 2004 5:10 am

Post 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 = ",";
		} 

?>
User avatar
sutejok
Forum Commoner
Posts: 37
Joined: Wed Mar 24, 2004 4:08 pm

Post 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..
Post Reply