Help me please..Why can I only enter one data in my database
Posted: Thu May 14, 2009 8:40 am
Why can I enter only one value in my database?
I can only one data on my database but after I want to add another data It won't allow me to enter another,
there's an error it says "Couldn't Execute Query
I'm creating a PetCatalog
Here's my code:
I can only one data on my database but after I want to add another data It won't allow me to enter another,
there's an error it says "Couldn't Execute Query
I'm creating a PetCatalog
Here's my code:
Code: Select all
<?php
/* Program: AddPet.php
* Desc: Adds new pet to the database. A confirmation
* screen is sent to the user.
*/
if (@$_POST['newbutton'] == "Cancel")
{
header("Location: ChoosePetCat.php");
}
include("misc.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("Couldn't connect to server");
foreach($_POST as $field => $value)
{
if($field != "newName" and $field != "newbutton"
and $field != "petColor")
{
if($field == "petName")
{
if($value == "new")
{
if($_POST['newName'] == "")
{
include("NewName_form.inc");
exit();
}
else
{
$value=$_POST['newName'];
}
}
}
if($field == "category")
{
$field = "petType";
}
if(!empty($value))
{
$fields_form[$field] =
ucfirst(strtolower(strip_tags(trim($value))));
$fields_form[$field] =
mysqli_real_escape_string($cxn,
$fields_form[$field]);
}
if(!empty($_POST['petColor']))
{
$petColor = strip_tags(trim($_POST['petColor']));
$petColor = ucfirst(strtolower($petColor));
$petColor =
mysqli_real_escape_string($cxn,$petColor);
}
}//end of first IF statement
}//end of foreach
?>
<html>
<head><title>Add Pet</title></head>
<body>
<?php
$field_array = array_keys($fields_form);
$fields=implode("," ,$field_array);
$values=implode(' "," ',$fields_form);
$query = "INSERT INTO pet ($fields) VALUES (\"$values\")";
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query."); /*I THINK THIS IS THE ERROR EVERYTIME I ADD ANOTHER(2ND TIME I ADD DATA) DATA, THIS ERROR WILL COME OUT, BUT WHEN THE FIRST TIME I ENTER A DATA THERE'S NO ERROR*/
$petID = mysqli_insert_id($cxn);
$query = "SELECT * from Pet WHERE petID='$petID'";
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query.");
$row = mysqli_fetch_assoc($result);
extract($row);
$category=$petType;
echo "The following pet has been added to the
Pet Catalog:<br>
<ul>
<li>Category: $category
<li>Pet Name: $petName
<li>Pet Description: $petDescription
<li>Price: $price
<li>Picture file: $pix \n";
if (@$petColor != "")
{
$query = "SELECT petName FROM Color
WHERE petName='$petName'
AND petColor='$petColor'";
$result = mysqli_query($cxn,$query)
or die("Couldn’t execute 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("Couldn’t execute query.");
echo "<li>Color: $petColor\n";
}
}
echo "</ul>";
echo "<a href='ChoosePetCat.php'>Add Another Pet</a>\n";
?>
</body></html>