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
triphp
Forum Newbie
Posts: 5 Joined: Sun Mar 06, 2005 4:32 am
Post
by triphp » Sun Mar 06, 2005 4:39 am
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")
{
$_SESSIONї'field'] = "";
}
if ($action == "add_field")
{
if ($_SESSIONї'field'])
{
$count_field = count($field) + 1;
$fieldї] = "$count_field";
}
else
{
$_SESSIONї'field'] = $field;
$count_field = count($field);
$fieldї] = "$count_field";
}
}
?>
<form method=get action=<?php echo $_SERVERї'PHP_SELF']; ?>>
parent name: <input type="text" name="parentname"><br>
children: <input type="text" name="child"> Age:<input type="text" name="ageforchild">
<?php
if ($_SESSIONї'field'])
{
foreach ($field as $count)
{
echo "<br>children $count: <input type="text" name="child$count" value="">
Age $count:<input type="text" name="ageforchild">";
}
}
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ї'submit']))
{
if ($_SESSIONї'field'])
{
foreach ($field as $count)
{
$parent = $_POSTї"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 ('${$child_name}', ${$child_age})";
mysql_query($qry, $ServerConnect) or die (mysql_error());
}
}
}
?>
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sun Mar 06, 2005 6:37 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Mar 06, 2005 7:15 am
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 » Sun Mar 06, 2005 7:52 am
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.
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sun Mar 06, 2005 7:55 am
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 » Sun Mar 06, 2005 7:58 am
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?
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sun Mar 06, 2005 8:08 am
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.
should be something similiar to...
Code: Select all
if ($_GETї'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 » Sun Mar 06, 2005 9:27 am
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ї'children_count']))
{
$children_count = $_GETї'children_count'];
}
else
{
$children_count = 1;
}
if (isset($_GETї'action']) && $_GETї'action'] == 'add_child_field') {$children_count++;}
echo'
<form action="index.php" method="POST">
<b>parent</b> name: <input type="text" name="parentname"><br>';
for ($i = 1; $i <= $children_count; $i++)
{
echo '<b>child ' . $i . '</b> name: <input type="text" name="childrenї' . $i . ']їname]"> age:<input type="text" name="childrenї' . $i . ']їage]"><br>';
}
echo '<a href="' . $_SERVERї'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ї'submit']))
{
$parent = $_POSTї'parentname'];
$sql= "INSERT INTO parent (Name) VALUES($parent)";
mysql_query($sql, $ServerConnect);
echo $sql;
foreach ($_POSTї'children'] as $key => $value)
{
if ($valueї'Name'] && $valueї'Age'])
{
$sql2= "INSERT INTO child (Name, Age) VALUES($value)";
mysql_query($sql2, $ServerConnect);
}
}
}
?>
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Mar 06, 2005 9:34 am
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());
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Mar 06, 2005 9:42 am
$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 » Sun Mar 06, 2005 9:44 am
the first query now work but the second would not.
Code: Select all
if(isset($_POSTї'submit']))
{
$parent = $_POSTї'parentname'];
$sql= "INSERT INTO parent (Name) VALUES('$parent')";
mysql_query($sql, $ServerConnect);
echo $sql;
foreach ($_POSTї'children'] as $key => $value)
{
if ($valueї'Name'] && $valueї'Age'])
{
$sql2= "INSERT INTO child (Name, Age) VALUES('$value')";
mysql_query($sql2, $ServerConnect) or die(mysql_error());
}
}
}