Jump to content

Error with array? Anyone can help? Php gods?


l4cky

Recommended Posts

Hi guys, I am pretty new to php and mysql, so please don't only tell me what to do without showing me. Would be very appreciated. Thanks in advance!!

Here is what I am trying to do. I am trying to use php to print the fields a table in Mysql into a form page called index.php. I think I sucessfully did it, but not the best way (mixing php and html, because I couldn't figure it out how to do it seperately) Everything looks good, except when I press submit and now we go to the insert_stats.php where the error:

 

Warning: Invalid argument supplied for foreach() in /insert_stats.php on line 8

 

insert_stats.php

 

<?
include("sql.php");

$paddler = $_POST['paddler'] ;
$practice = $_POST['practice'] ;
$header = $_POST['header'] ;

[b]foreach ($header as $value) [/b]
{
$insert="INSERT INTO pushup ($header) VALUES ('$value')";

mysql_query($insert) OR die(mysql_error()) ;

}


$insert2="INSERT INTO pushup (paddler_id , practice_id) VALUES ('$paddler' , '$practice')";
mysql_query($insert2) OR die(mysql_error()) ;

mysql_close();


?>

 

index.php

<table width="250" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="pushup" name="pushup" method="post" action="insert_stats.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3" bgcolor="#E6E6E6"><strong>IDENTIFICATION</strong> </td>
</tr>
<tr>
<td width="80%">Paddler ID</td>
<td width="2%">:</td>
<td width="18%"><input name="paddler" type="text" id="paddler" size="1" /></td>
</tr>
<tr>
<td>Practice #</td>
<td>:</td>
<td><input name="practice" type="text" id="practice" size="1" /></td>
</tr>
<tr>
<td colspan="3" bgcolor="#E6E6E6"><strong>ADD FITNESS STATS</strong> </td>
</tr>
<?
include("sql.php");

$result = mysql_query("SELECT * FROM pushup");


$printed_headers = false; 

while ( $row = mysql_fetch_array($result) ) 
{ 
    if ( !$printed_headers ) 
    { 
        //print the headers once: 
        echo "<tr>"; 
        foreach ( array_keys($row) AS $header ) 
        { 
            if($header == 'paddler_id' || $header == 'practice_id' || $header == 'p_id') continue; 
       
            if ( !is_int($header) ) 
            { 
                echo "<td>$header</td><td>:</td><td><input name='".$header."' type=\"text\" id='".$header."' size=\"1\" /></td></tr>";  
            } 
        } 
        $printed_headers = true; 
    } 
     
    
} 

mysql_free_result($result);
mysql_close();
?>

<tr>
<td><input type="reset" name="Submit2" value="Reset" /></td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /></td>

</table>
</td>
</form>
</tr>
</table>

 

 

There are a few things to correct. Rather than have us go back and forth fixing the little issues, how about I give you something to try instead?

 

index.php

</pre>
<table width="250" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">





IDENTIFICATION 


Paddler ID
:



Practice #
:



ADD FITNESS STATS 


require "sql.php"; // if, for some reason, sql.php couldn't be executed, the script should die

// step 1: figure out what's in the table
// you can use an EXPLAIN query
$result = mysql_query("EXPLAIN pushup");
// creates a resultset with columns: Field [name], Type [field type], Null [YES/NO],
//   Key [PRI/MUL/UNI/empty], Default [NULL/default value/empty], Extra [eg, auto_increment]
// you could get advanced and look at the field type (specifically, everything up to the first space)
// an int(10) would be a number, a char(?) or varchar(?) would be a string (and you could vary the
// size of the textbox according to the ?), display a date picker for date/datetime types...

// step 2: print out the fields and an  for each
while ($row = mysql_fetch_assoc($result)) {
// ignore these two fields
if (in_array($row["Field"], array("paddler_id", "practice_id"))) continue;

echo "";
echo "", htmlentities($row["Field"]), "";
echo ":";
// use arrays in the HTML
// this way $_POST["data"] will be an array with key=field and value=field value 
echo "";
echo "";
}

mysql_free_result($row);
mysql_close();

?>


 






</

insert_stats.php

require "sql.php";

$data = (array)$_POST["data"];

// data to put into the INSERT query
$insert_data = array(
"paddler_id" => (int)$_POST["paddler"],
"practice_id" => (int)$_POST["practice"]
);

// to prevent someone abusing the form, we need to check that the keys in $data are actually
// fields in the table. that means re-running the EXPLAIN query
$result = mysql_query("EXPLAIN pushup");
$fields = array();
while ($row = mysql_fetch_assoc($result)) $fields[] = $row["Field"];

// with $fields an array, we can use in_array on the field names in $data
// for each one, if it checks out then add it and its value to $insert_data
foreach ($data as $field => $value) {
if (in_array($field, $fields)) {
	$insert_data[$field] = mysql_real_escape_string($value);
}
}

// normally you'd check that $insert_data isn't empty, but since we gave two values at the start
// we know there's definitely something there - even if just those two

// now we can do an INSERT
// implode() helps here
$query = "INSERT INTO pushup ";
$query .= "(`" . implode("`, `", array_keys($insert_data)) . "`) "; // field names
$query .= "VALUES (";
$query .= "'" . implode("', '", $insert_data) . "'";
$query .= ")";

mysql_query($query);
mysql_close();

Hi wow!!

Thank you for so fast reply! Your skills really impressed me, and I got to check out some stuffs I don't understand yet on what you write so I can learn :)

 

I gave me an error on mysql_free_result($row); on index.php page, I changed it to mysql_free_result($result); and error is gone. Waiting for you to confirm.

 

I also add the ?> of the end of insert page.

 

Wow big thanks to you, I can keep  continuing my work !! Thanks so much!!

I gave me an error on mysql_free_result($row); on index.php page, I changed it to mysql_free_result($result); and error is gone. Waiting for you to confirm.

Yeah, that's right. My mistake.

 

The closing ?> is optional - I tend to leave them out, but there's nothing wrong with putting them in.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.