Hi all :), im towards the end of the php for dummies book and it telling me how to build a e-commece style pet Catalog. I show the code
<?php
/* Program: addpet.php
Description: adds new pet to the databse.
A confirmation page is sent to the user.
*/
if(@$_POST['newbutton'] == "Cancel")
{
header("Location: choosepetcat.php");
}
require_once("config.php");
foreach($_POST as $field => $value)
{
if(empty($value))
{
if($field == "petName" or $field == "petDescription")
{
$blank_array[] = $field;
}
}
else
{
if($field != "category")
{
if(!preg_match("/^[A-Za-z0-9., _ -]+$/",$value))
{
$error_array[] = $field;
}
if($field == "newCat")
{
$clean_data['petType']=trim(strip_tags($value));
}
else
{
$clean_data[$field] = trim(strip_tags($value));
}
}
}
}
if(@sizeof($blank_array)>0 or @sizeof($error_array)>0)
{
if(@sizeof(blank_array) > 0)
{
echo "<p><b> The following fields have incorrect information.
Only letters, numbers, spaces, underscores and hyphens
are allowed:</b><br />\n";
foreach($error_array as $value)
{
echo" $value<br />\n";
}
}
extract($clean_data);
include("NewName_form.inc");
exit();
}
foreach($clean_data as $field => $value)
{
if(!empty ($value) and $field != "petColor")
{
$fields_form[$field] =
ucfirst(strtolower(strip_tags(trim($value))));
$fields_form[$field] =
mysqli_real_escape_string($cxn,$fields_form[$field]);
if($field == "price")
{
$fields_form[$field]=
(float) $fields_form[$field];
}
}
if(!empty($_POST['petColor']))
{
$petColor = strip_tags(trim($_POST['petColor']));
$petColor = ucfirst(strtolower($petColor));
$petColor = mysqli_real_escape_string($cxn, $petColor);
}
}
?>
<html>
<head>
<title>Add Pet</title>
<body>
<?php
$field_array = array_keys($fields_form);
$fields=implode(",",$field_array);
$query = "INSERT INTO Pet ($fields) VALUES (";
foreach($fields_form as $field => $value)
{
if($field == "price")
{
$query .="$value ,";
}
else
{
$query .="$value ,";
}
}
$query .= ") ";
$query = preg_replace("/.\)/",")", $query);
$result = mysqli_query($cxn, $query)
or die ("could not execute query");
$petID = mysqli_insert_id($cxn);
$query = "SELECT * from Pet WHERE petID='$petID'";
$result = mysqli_query($cxn, $query)
or die("could not execute query");
$row = mysqli_fetch_assoc($result);
extract($row);
echo"The following pet has been added to the Pet catalog:<br />
<ul>
<li>Category: $petType</li>
<li>Pet Name: $petName</li>
<li>Pet Description: $petDescription</li>
<li>Price: \$$price</li>
<li>Picture file: $pix</li>\n";
if (@$petColor != "")
{
$query = "SELECT petName FROM Color
WHERE petName='$petName'
AND petColor='$petColor'";
$result = mysqli_query($cxn, $query)
or die ("Could not execute the F'ing query!!!");
$num = mysqli_num_rows($result);
if ($num < 1)
{
$query = "INSERT INTO Color (petName, petColor,pix)
VALUES ('$petName', '$petColor', '$pix')";
$result = mysqli_query($cxn, $query)
or die ("could not execute the shitty query, shitty programming F'ing twat!!.".
mysqli_error($cxn));
echo "<li>Color: $petColor</li>\n";
}
}
echo "</ul>\n";
echo "<a href='choosepetcat.php'>Add Another Pet</a>\n";
?>
</body></head>
The error
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\petstore2\addpet.php on line 70
Warning: array_keys() expects parameter 1 to be array, null given in C:\xampp\htdocs\petstore2\addpet.php on line 105
Warning: implode() [function.implode]: Invalid arguments passed in C:\xampp\htdocs\petstore2\addpet.php on line 106
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\petstore2\addpet.php on line 108
could not execute query
I definatly know its related to my databse connection, I have been having this problem for a long time now and the die messages aren't so good from the book.
I hope you guys can solve this problem, just nearly to the end :)
cheeers