Complex MySQL Insert, what's wrong?

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

Post Reply
triphp
Forum Newbie
Posts: 5
Joined: Sun Mar 06, 2005 4:32 am

Complex MySQL Insert, what's wrong?

Post by triphp »

HI,
I have the mysql insert code below and it never does what it is intend to be. Amazingly it does not show any error or warings. What's wrong with this code? Please help me this one.

Code: Select all

<?php
require("config.php");
// Generate Dynamic Forms
session_start();
if ($action == "clear_field") 
&#123;
	$_SESSION&#1111;'field'] = "";
&#125;

if ($action == "add_field") 
&#123;
	if ($_SESSION&#1111;'field']) 
	&#123;
		$count_field = count($field) + 1;
		$field&#1111;] = "$count_field";
	&#125; 
	else 
	&#123;
		$_SESSION&#1111;'field'] = $field;
		$count_field = count($field);
		$field&#1111;] = "$count_field";
	&#125;
&#125;
?>
<form method=get action=<?php echo $_SERVER&#1111;'PHP_SELF']; ?>>
parent name: <input type="text" name="parentname"><br>
children: <input type="text" name="child">&nbsp; Age:<input type="text" name="ageforchild">
<?php
if ($_SESSION&#1111;'field']) 
&#123;
	foreach ($field as $count) 
	&#123;
		echo "<br>children $count: <input type="text" name="child$count" value=""> 
		Age $count:<input type="text" name="ageforchild">"; 
	&#125;
&#125;
echo "<br><a href="$PHP_SELF?action=add_field">Add Child Field</a>
<br><a href="$PHP_SELF?action=clear_field">Clear Field</a>
<br><input type="submit" name="submit" value="submit">";
?>
</form>
<?php
if(isset($_POST&#1111;'submit']))
&#123;

	if ($_SESSION&#1111;'field']) 
	&#123;
		foreach ($field as $count) 
		&#123;
			$parent = $_POST&#1111;"parentname"];
			$parentqry = "INSERT INTO parent (Name) VALUES ('$parent')";
			mysql_query($parentqry, ServerConnect);
			
			$child_name = "child$count";
			$child_age = "ageforchild$count";
			$qry="insert into child (ChildName, Age) values ('$&#123;$child_name&#125;', $&#123;$child_age&#125;)";
			mysql_query($qry, $ServerConnect) or die (mysql_error());
	
		&#125;
	&#125;
&#125;
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

1. Try this:

Code: Select all

<?php
echo 'Display Errors: '.(ini_get('display_errors') == '1' ? 'On' : 'Off')."\n";
echo 'Error Level: '.(ini_get('error_reporting') == '2047' ? 'E_ALL' : 'Not E_ALL')."\n"; 
?>
If 'Error Level' is not E_ALL, you might want to see if that can be bumped up so that errors are shown.

2. Verify that the SQL's are indeed correct, with echo'ing out $qry in plain text.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

looks like register_globals may be on, or something assumes they are. The following will tell you:

Code: Select all

<?php
echo 'Register globals are ' . (ini_get('register_globals') ? 'on' : 'off');
?>
triphp
Forum Newbie
Posts: 5
Joined: Sun Mar 06, 2005 4:32 am

Post by triphp »

Here's what I get: Display Errors: On Error Level: Not E_ALL Register globals are on.

Problem is getting worse coz it doesn't dispaly any query when I try to echo it.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

On top of the script add:

Code: Select all

ini_set('error_reporting', E_ALL);
You could also add:

Code: Select all

print_r($_POST);
// ...and...
print_r($_SESSION);
...or similiar on strategic places within various places of the script and from that see where it fubars.
triphp
Forum Newbie
Posts: 5
Joined: Sun Mar 06, 2005 4:32 am

Post by triphp »

It seems confusing.

Code: Select all

Array ( ) 
Notice: Undefined variable: _SESSION in C:\Inetpub\xampp\htdocs\Test\index2.php on line 4
Notice: Undefined variable: action in C:\Inetpub\xampp\htdocs\Test\index2.php on line 15

Notice: Undefined variable: action in C:\Inetpub\xampp\htdocs\Test\index2.php on line 20
What is meant by Array() on top?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

The Array () is from trying to print_r($_POST)/print_r($_SESSION). If those are empty, an empty array is returned...

You are mixing good and bad coding in this script. $_SESSION's are somewhat allright, but how you pass 'actions' are messy.

Code: Select all

if ($action == "add_field")
should be something similiar to...

Code: Select all

if ($_GET&#1111;'action'] == "add_field")
...if I'd mention one thing.

$_POST for form="post" forms...
$_GET for variables passed in the URI ( index.php?foo=bar ) and form="get" forms

Try to read up more on predefined variables before venturing more indepth.
triphp
Forum Newbie
Posts: 5
Joined: Sun Mar 06, 2005 4:32 am

Post by triphp »

I modify the code to make it a litle easier but still I can't make it work:

Code: Select all

<?php
require("config.php");	
if (isset($_GET&#1111;'children_count'])) 
&#123;
	$children_count = $_GET&#1111;'children_count'];
&#125;
else 
&#123;
	$children_count = 1;
&#125;
if (isset($_GET&#1111;'action']) && $_GET&#1111;'action'] == 'add_child_field') &#123;$children_count++;&#125;

echo'
<form action="index.php" method="POST">
<b>parent</b> name: <input type="text" name="parentname"><br>';

for ($i = 1; $i <= $children_count; $i++)
&#123;
    echo '<b>child ' . $i . '</b> name: <input type="text" name="children&#1111;' . $i . ']&#1111;name]"> age:<input type="text" name="children&#1111;' . $i . ']&#1111;age]"><br>'; 
&#125;
echo '<a href="' . $_SERVER&#1111;'PHP_SELF'] . '?children_count=' . $children_count . '&action=add_child_field">add child field</a><br>
<input type="submit" name="submit" value="submit"></form>';
?> 
<?php
if(isset($_POST&#1111;'submit']))
&#123;
	$parent = $_POST&#1111;'parentname'];
	$sql= "INSERT INTO parent (Name) VALUES($parent)";
	mysql_query($sql, $ServerConnect);
	echo $sql;
	
	foreach ($_POST&#1111;'children'] as $key => $value)
	&#123;
		if ($value&#1111;'Name'] && $value&#1111;'Age'])
		&#123;
			$sql2= "INSERT INTO child (Name, Age) VALUES($value)";
			mysql_query($sql2, $ServerConnect);
		&#125;
	&#125;
&#125;
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

quote your column and variable names appropriatly..
$sql= "INSERT INTO parent (`Name`) VALUES('$parent')";
and also add a die statement to output errors on each mysql_query()

Code: Select all

mysql_query($sql2, $ServerConnect) or die(mysql_error());
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$value is an array. It will print 'Array' when echo'd.. you need to slap both values contained into a string for insertion. implode() can do this.
triphp
Forum Newbie
Posts: 5
Joined: Sun Mar 06, 2005 4:32 am

Post by triphp »

the first query now work but the second would not.

Code: Select all

if(isset($_POST&#1111;'submit']))
&#123;
	$parent = $_POST&#1111;'parentname'];
	$sql= "INSERT INTO parent (Name) VALUES('$parent')";
	mysql_query($sql, $ServerConnect);
	echo $sql;
	
	foreach ($_POST&#1111;'children'] as $key => $value)
	&#123;
		if ($value&#1111;'Name'] && $value&#1111;'Age'])
		&#123;
			$sql2= "INSERT INTO child (Name, Age) VALUES('$value')";
			mysql_query($sql2, $ServerConnect) or die(mysql_error());
		&#125;
	&#125;
&#125;
Post Reply