Jump to content

Recommended Posts

So on my game, you can purchase horses. You can select colors for your horse and I'm trying to code it so whatever color you pick, is the color of the horse image you will see. This is the code I have:

 

<?php

if($_POST[breed] == "none"){print "You forgot to select a breed!";include "footer.php";exit;}
$sql="INSERT INTO horsedata (Name, Owner, Height, Breed, Gender, Registrations, Exp, Points, Records, Speed, Scope, Power, Obedience, Sociability, Sire, Dam, Foals, Awards, Grading, Hay, Grain, Vet, Shoes, Exercise, Grooming, DOB, Endurance, Intelligence, Color, temperament, legs, back, neck, head, pedigree)
VALUES
('$_POST[Name]','$id','$_POST[Height]','$_POST[breed]','$_POST[Gender]','Not Registered','0','0','none','15','15','15','15','15','The All Great One','The Phantom Mare','None yet','This horse has not won any awards','Not Graded','No','No','No','No','No','No','3','15','15','$_POST[Color]','$_POST[temperament]','$_POST[legs]','$_POST[back]','$_POST[neck]','$_POST[head]','$_POST[pedigree]' )";

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }
echo "<table class='table'><tr><td class='tablegreen'>Congratulations</td></tr><tr><td class='tabletd'><BR>Your horse has been purchased";

?>

<?php

if($_POST[Color] == 'Bay')
$sql="INSERT INTO horsedata (Image)
VALUES
('horsecolors/Bay.jpg')";
?>

 

This is where it's supose to insert the colors onto the horsedata table but its not:

if($_POST[Color] == 'Bay')
$sql="INSERT INTO horsedata (Image)
VALUES
('horsecolors/Bay.jpg')";

 

 

You're not performing mysql_query() on the query.

 

<?php

if($_POST[Color] == 'Bay')
$sql="INSERT INTO horsedata (Image)
VALUES
('horsecolors/Bay.jpg')";
   mysql_query($sql) or die(mysql_query());
?>

 

I'm very inexperienced with coding :/ I bought a game not knowing php...stupid move but oh well.

ahaha now its inserting the image but nothing else.

 

What do you mean?  That's the only thing you tell it to INSERT. 

 

Try (I cleaned up your code a bit, it's essentially the same):

 

if($_POST[breed] == "none")
{
   print "You forgot to select a breed!";
   include "footer.php";
   exit;
}

$sql="INSERT INTO horsedata (Name, Owner, Height, Breed, Gender, Registrations, Exp, Points, Records, Speed, Scope, Power, Obedience, Sociability, Sire, Dam, Foals, Awards, Grading, Hay, Grain, Vet, Shoes, Exercise, Grooming, DOB, Endurance, Intelligence, Color, temperament, legs, back, neck, head, pedigree)
VALUES
('$_POST[Name]','$id','$_POST[Height]','$_POST[breed]','$_POST[Gender]','Not Registered','0','0','none','15','15','15','15','15','The All Great One','The Phantom Mare','None yet','This horse has not won any awards','Not Graded','No','No','No','No','No','No','3','15','15','$_POST[Color]','$_POST[temperament]','$_POST[legs]','$_POST[back]','$_POST[neck]','$_POST[head]','$_POST[pedigree]' )";

mysql_query($sql) or die(mysql_error());

echo "</pre>
<table class="'table'">
       
    Congratulations


   
  
Your horse has been purchased";

if($_POST[Color] == 'Bay')
{
   $sql="INSERT INTO horsedata (Image) VALUES ('horsecolors/Bay.jpg')";
   mysql_query($sql) or die(mysql_error());
}

?>
</

 

 

ahaha now its inserting the image but nothing else.

 

What do you mean?  That's the only thing you tell it to INSERT. 

 

Try (I cleaned up your code a bit, it's essentially the same):

 

<?php

if($_POST[breed] == "none")
{
   print "You forgot to select a breed!";
   include "footer.php";
   exit;
}

$sql="INSERT INTO horsedata (Name, Owner, Height, Breed, Gender, Registrations, Exp, Points, Records, Speed, Scope, Power, Obedience, Sociability, Sire, Dam, Foals, Awards, Grading, Hay, Grain, Vet, Shoes, Exercise, Grooming, DOB, Endurance, Intelligence, Color, temperament, legs, back, neck, head, pedigree)
VALUES
('$_POST[Name]','$id','$_POST[Height]','$_POST[breed]','$_POST[Gender]','Not Registered','0','0','none','15','15','15','15','15','The All Great One','The Phantom Mare','None yet','This horse has not won any awards','Not Graded','No','No','No','No','No','No','3','15','15','$_POST[Color]','$_POST[temperament]','$_POST[legs]','$_POST[back]','$_POST[neck]','$_POST[head]','$_POST[pedigree]' )";

mysql_query($sql) or die(mysql_error());

echo "<table class='table'>
       <tr>
    <td class='tablegreen'>Congratulations</td>
	 </tr>
	<tr>
   <td class='tabletd'>
  <br/>Your horse has been purchased";

if($_POST[Color] == 'Bay')
{
   $sql="INSERT INTO horsedata (Image) VALUES ('horsecolors/Bay.jpg')";
   mysql_query($sql) or die(mysql_error());
}

?>

 

Thank you! It looks much better.

 

It's supose to be inserting the color to the same table as all the other info. With the cleaned up code its essentially making 2 'horses' instead of inserting the color onto the same 'horse' as the first set of info.

you are using the same table to store horse data and image data?

 

If yes, you can insert the image also in the main query where u r updating other information, u dont need seperate queries

 

If no, please change the table name in image query.

 

yes im trying to get it into the main query, but i need about...30 of the if color = whatevercolor and i wasnt sure how to add that to the same query.

something like this..not tested.

<?php

if($_POST[breed] == "none")
{
   print "You forgot to select a breed!";
   include "footer.php";
   exit;
}

$color = $_POST[Color];

switch($color) {
"Bay": $img = "horsecolors/Bay.jpg";break;
"White": $img = "horsecolors/White.jpg";break;
"Black": $img = "horsecolors/Black.jpg";break;
default: $img = "";break;
}


$sql="INSERT INTO horsedata (Name, Owner, Height, Breed, Gender, Registrations, Exp, Points, Records, Speed, Scope, Power, Obedience, Sociability, Sire, Dam, Foals, Awards, Grading, Hay, Grain, Vet, Shoes, Exercise, Grooming, DOB, Endurance, Intelligence, Color, temperament, legs, back, neck, head, pedigree, Image)
VALUES
('$_POST[Name]','$id','$_POST[Height]','$_POST[breed]','$_POST[Gender]','Not Registered','0','0','none','15','15','15','15','15','The All Great One','The Phantom Mare','None yet','This horse has not won any awards','Not Graded','No','No','No','No','No','No','3','15','15','$_POST[Color]','$_POST[temperament]','$_POST[legs]','$_POST[back]','$_POST[neck]','$_POST[head]','$_POST[pedigree]', '".$img."' )";

mysql_query($sql) or die(mysql_error());

echo "<table class='table'>
       <tr>
    <td class='tablegreen'>Congratulations</td>
	 </tr>
	<tr>
   <td class='tabletd'>
  <br/>Your horse has been purchased";

/*if($_POST[Color] == 'Bay')
{
   $sql="INSERT INTO horsedata (Image) VALUES ('horsecolors/Bay.jpg')";
   mysql_query($sql) or die(mysql_error());
}*/

?>

something like this..not tested.

<?php

if($_POST[breed] == "none")
{
   print "You forgot to select a breed!";
   include "footer.php";
   exit;
}

$color = $_POST[Color];

switch($color) {
"Bay": $img = "horsecolors/Bay.jpg";break;
"White": $img = "horsecolors/White.jpg";break;
"Black": $img = "horsecolors/Black.jpg";break;
default: $img = "";break;
}


$sql="INSERT INTO horsedata (Name, Owner, Height, Breed, Gender, Registrations, Exp, Points, Records, Speed, Scope, Power, Obedience, Sociability, Sire, Dam, Foals, Awards, Grading, Hay, Grain, Vet, Shoes, Exercise, Grooming, DOB, Endurance, Intelligence, Color, temperament, legs, back, neck, head, pedigree, Image)
VALUES
('$_POST[Name]','$id','$_POST[Height]','$_POST[breed]','$_POST[Gender]','Not Registered','0','0','none','15','15','15','15','15','The All Great One','The Phantom Mare','None yet','This horse has not won any awards','Not Graded','No','No','No','No','No','No','3','15','15','$_POST[Color]','$_POST[temperament]','$_POST[legs]','$_POST[back]','$_POST[neck]','$_POST[head]','$_POST[pedigree]', '".$img."' )";

mysql_query($sql) or die(mysql_error());

echo "<table class='table'>
       <tr>
    <td class='tablegreen'>Congratulations</td>
	 </tr>
	<tr>
   <td class='tabletd'>
  <br/>Your horse has been purchased";

/*if($_POST[Color] == 'Bay')
{
   $sql="INSERT INTO horsedata (Image) VALUES ('horsecolors/Bay.jpg')";
   mysql_query($sql) or die(mysql_error());
}*/

?>

 

 

That code came up with:

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_CASE or T_DEFAULT or '}' in /home/collegh9/public_html/one-stride/horse_reg2.php on line 17

oops sorry forgot the case keyword...

<?php

if($_POST[breed] == "none")
{
   print "You forgot to select a breed!";
   include "footer.php";
   exit;
}

$color = $_POST[Color];

switch($color) {
case "Bay": $img = "horsecolors/Bay.jpg";break;
case "White": $img = "horsecolors/White.jpg";break;
case "Black": $img = "horsecolors/Black.jpg";break;
default: $img = "";break;
}


$sql="INSERT INTO horsedata (Name, Owner, Height, Breed, Gender, Registrations, Exp, Points, Records, Speed, Scope, Power, Obedience, Sociability, Sire, Dam, Foals, Awards, Grading, Hay, Grain, Vet, Shoes, Exercise, Grooming, DOB, Endurance, Intelligence, Color, temperament, legs, back, neck, head, pedigree, Image)
VALUES
('$_POST[Name]','$id','$_POST[Height]','$_POST[breed]','$_POST[Gender]','Not Registered','0','0','none','15','15','15','15','15','The All Great One','The Phantom Mare','None yet','This horse has not won any awards','Not Graded','No','No','No','No','No','No','3','15','15','$_POST[Color]','$_POST[temperament]','$_POST[legs]','$_POST[back]','$_POST[neck]','$_POST[head]','$_POST[pedigree]', '".$img."' )";

mysql_query($sql) or die(mysql_error());

echo "<table class='table'>
       <tr>
    <td class='tablegreen'>Congratulations</td>
	 </tr>
	<tr>
   <td class='tabletd'>
  <br/>Your horse has been purchased";

/*if($_POST[Color] == 'Bay')
{
   $sql="INSERT INTO horsedata (Image) VALUES ('horsecolors/Bay.jpg')";
   mysql_query($sql) or die(mysql_error());
}*/

?>


oops sorry forgot the case keyword...

<?php

if($_POST[breed] == "none")
{
   print "You forgot to select a breed!";
   include "footer.php";
   exit;
}

$color = $_POST[Color];

switch($color) {
case "Bay": $img = "horsecolors/Bay.jpg";break;
case "White": $img = "horsecolors/White.jpg";break;
case "Black": $img = "horsecolors/Black.jpg";break;
default: $img = "";break;
}


$sql="INSERT INTO horsedata (Name, Owner, Height, Breed, Gender, Registrations, Exp, Points, Records, Speed, Scope, Power, Obedience, Sociability, Sire, Dam, Foals, Awards, Grading, Hay, Grain, Vet, Shoes, Exercise, Grooming, DOB, Endurance, Intelligence, Color, temperament, legs, back, neck, head, pedigree, Image)
VALUES
('$_POST[Name]','$id','$_POST[Height]','$_POST[breed]','$_POST[Gender]','Not Registered','0','0','none','15','15','15','15','15','The All Great One','The Phantom Mare','None yet','This horse has not won any awards','Not Graded','No','No','No','No','No','No','3','15','15','$_POST[Color]','$_POST[temperament]','$_POST[legs]','$_POST[back]','$_POST[neck]','$_POST[head]','$_POST[pedigree]', '".$img."' )";

mysql_query($sql) or die(mysql_error());

echo "<table class='table'>
       <tr>
    <td class='tablegreen'>Congratulations</td>
	 </tr>
	<tr>
   <td class='tabletd'>
  <br/>Your horse has been purchased";

/*if($_POST[Color] == 'Bay')
{
   $sql="INSERT INTO horsedata (Image) VALUES ('horsecolors/Bay.jpg')";
   mysql_query($sql) or die(mysql_error());
}*/

?>


 

 

thank you so much!

 

I think this quarter at my college im going to take a php class because that was a pretty basic thing i didnt know right?

With the cleaned up code its essentially making 2 'horses' instead of inserting the color onto the same 'horse' as the first set of info.

 

Yes, that's because you have an unconditional INSERT and whenever $_POST == 'Bay').

 

Like samshel pointed out, you should do this in 1 query.

 

In this example if the user chose Bay it will insert "horsecolors/Bay.jpg" if they don't chose that then it will give the Image a default (horsecolors/default.jpg).  Don't forget, put 'Image' in the appropriate position in the query string.

 

if($_POST[breed] == "none")
{
   print "You forgot to select a breed!";
   include "footer.php";
   exit;
} else {

$color = $_POST['Color']=="Bay" ? "horsecolors/default.jpg" : "horsecolors/{$_POST['Color']}.jpg";
$sql="INSERT INTO horsedata (Image, Name, Owner, Height, Breed, Gender, Registrations, Exp, Points, Records, Speed, Scope, Power, Obedience, Sociability, Sire, Dam, Foals, Awards, Grading, Hay, Grain, Vet, Shoes, Exercise, Grooming, DOB, Endurance, Intelligence, Color, temperament, legs, back, neck, head, pedigree)
VALUES
('$color', '$_POST[Name]','$id','$_POST[Height]','$_POST[breed]','$_POST[Gender]','Not Registered','0','0','none','15','15','15','15','15','The All Great One','The Phantom Mare','None yet','This horse has not won any awards','Not Graded','No','No','No','No','No','No','3','15','15','$_POST[Color]','$_POST[temperament]','$_POST[legs]','$_POST[back]','$_POST[neck]','$_POST[head]','$_POST[pedigree]' )";

mysql_query($sql) or die(mysql_error());

echo "</pre>
<table class="'table'">
       
       Congratulations
       
      
      
     
Your horse has been purchased";
}
?>

</

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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