kendallkamikaze Posted March 17, 2009 Share Posted March 17, 2009 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')"; Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/ Share on other sites More sharing options...
Maq Posted March 17, 2009 Share Posted March 17, 2009 You're not performing mysql_query() on the query. Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787226 Share on other sites More sharing options...
kendallkamikaze Posted March 17, 2009 Author Share Posted March 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787230 Share on other sites More sharing options...
kendallkamikaze Posted March 17, 2009 Author Share Posted March 17, 2009 ahaha now its inserting the image but nothing else. Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787232 Share on other sites More sharing options...
Maq Posted March 17, 2009 Share Posted March 17, 2009 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()); } ?> </ Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787241 Share on other sites More sharing options...
kendallkamikaze Posted March 17, 2009 Author Share Posted March 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787244 Share on other sites More sharing options...
samshel Posted March 17, 2009 Share Posted March 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787246 Share on other sites More sharing options...
kendallkamikaze Posted March 17, 2009 Author Share Posted March 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787248 Share on other sites More sharing options...
samshel Posted March 17, 2009 Share Posted March 17, 2009 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()); }*/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787250 Share on other sites More sharing options...
kendallkamikaze Posted March 17, 2009 Author Share Posted March 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787251 Share on other sites More sharing options...
samshel Posted March 18, 2009 Share Posted March 18, 2009 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()); }*/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787254 Share on other sites More sharing options...
kendallkamikaze Posted March 18, 2009 Author Share Posted March 18, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787257 Share on other sites More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 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"; } ?> </ Quote Link to comment https://forums.phpfreaks.com/topic/149899-solved-having-trouble-inserting-onto-mysql/#findComment-787259 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.