Help me please..Why can I only enter one data in my database

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
jimgym1989
Forum Newbie
Posts: 10
Joined: Thu May 14, 2009 8:31 am

Help me please..Why can I only enter one data in my database

Post by jimgym1989 »

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:

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>
Last edited by jimgym1989 on Thu May 14, 2009 9:07 am, edited 2 times in total.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Help me please..Why can I only enter one data in my database

Post by crazycoders »

Ok first of all, try to format your code using [ code=php ] and [ /code ] tags, it will help us read your code, second, you problem is hard to diagnose because we don't have the output SQL string...
  • Please format your code
  • Give us the exact error you get
  • Show us the value of "$query = "INSERT INTO pet ($fields) VALUES (\"$values\")";" before sending it to the server
  • Optional but useful, show us the structure of your database...
Thanks
jimgym1989
Forum Newbie
Posts: 10
Joined: Thu May 14, 2009 8:31 am

Re: Help me please..Why can I only enter one data in my database

Post by jimgym1989 »

is it ok If I give you my code? or maybe i'll get you IM/YM ID so that I could explain it better to you. :D
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Help me please..Why can I only enter one data in my database

Post by crazycoders »

Ok... so now just make sure you also have written code=php in the [] or else the color coding doesn't get applied.

Second, i asked for the result of the $query...

just do a echo $query; right after the $query = 'something';

This will show us what is in the $query and maybe tell us how to fix this!
Post Reply