Jump to content

Fields empty


evaluv

Recommended Posts

I'm trying to make it so you can buy a simulated dog from the store.When you go to the kennel page, the "dog" isn't there.So you goto the table, and everything but the dog's ID's are blank.Also, the player's ID needs to be "associated" or the same as the owner ID I believe, but I don't know how to accomplish that. I was hoping I could get advice on this, plus easy improvements to the scripts/SQLI'm very new at this, so here is everything I've been using:buydog.php

<?phpinclude ('connect.php');?><?phperror_reporting(E_ALL ^ E_NOTICE);?><form name="buydog" method="post" action="buydog.php">Name:<input name="name" type="text"><br>Breed:<select name="breed" type="text"><option value="golden_retriever">Golden Retriever</option><option value="german_shepherd_dog">German Shepherd Dog</option><option value="bernese_mountain_dog">Bernese Mountain Dog</option><option value="american_pitbull_terrier">American Pitbull Terrier</option></select><br>Gender:<select name="gender"><option value="female">Female</option><option value="male">Male</option></select><br><input type="submit" name="Submit" value="Buy Dog"></form><?php$dogname = trim($_POST['dogname']);$dogbreed = trim($_POST['dogbreed']);$doggender = trim($_POST['doggender']);$ownerid = $_SESSION['id'];$dog = @mysql_query("INSERT INTO dogs (ownerid, dogname, dogbreed, doggender)VALUES ('$ownerid', '$dogname', '$dogbreed', '$doggender')") or die("Error:".mysql_error());?>

kennel.php

<?php include ('connect.php');$query = "SELECT dogname, dogbreed, doggender FROM dogs WHERE ownerid = '".$_SESSION['id']."'";$result = mysql_query($query)or die ("Couldn't execute query.");// Start while loopwhile ($dog= mysql_fetch_object ($result)){// prevent notices$dogname = $dog->dogname;$dogbreed = $dog->dogbreed;$doggender = $dog->doggender;echo"Name: $dogname<br>Breed: $dogbreed<br>Gender: $doggender<br><br>";//end while loop}?>

register.php

<?phpinclude('connect.php');if($loggedin == '1')die("You can't register another account while you're logged in.");// Register Script// So, the register script is a sample of the basic elements of// coding used in a simple sim game, namely, putting new data// into databases.if(isset($_POST['submit'])){// trim removes the whitespaces from the beginning and end of text// this keeps someone from having a username of " "$uname = trim($_POST['username']);// Make sure all forms were filled out.if((!isset($_POST['username'])) || (!isset($_POST['pass']))|| ($uname == '') || ($_POST['pass'] == ''))die("Please fill out the form completely. <br><br><a href=register.php>Continue</a>");// Make sure name hasn't already been used.$check = @mysql_query("SELECT id FROM players WHERE username = '$uname'");$check = @mysql_num_rows($check);if($check > 0)die("Sorry, that username has already been taken. Please try again.<br><br><a href=register.php>Continue</a>");// Encrypt password$pass = md5($_POST['pass']);$date = date("m/d/y");// Finally, create the record.$newPlayer = @mysql_query("INSERT INTO players (username, password, registered) VALUES ('$uname', '$pass', '$date')") or die("Error: ".mysql_error());echo 'You have been registered! You may now <a href=index.php>Log in</a>.';}else{// A simple example of a form.echo '<form action=register.php method=post>Username: <input type=text name=username><br>Password: <input type=password name=pass><br><input type=submit name=submit value=Submit></form>';}?>

connect.php

<?php// This script will connect the user to your mySQL database.// It will also check it they're logged in or not.// It should be included on every page you create.// Set the variables below to match your settings$dbhost = 'localhost';$dbuser = '_________';$dbpass = '________';$dbname = '_________';$link = mysql_pconnect($dbhost, $dbuser, $dbpass)or die("Could not connect to server.");$selectdb = mysql_select_db($dbname, $link)or die("Could not connect to database.");// Check to see if user is logged insession_start();if((!isset($_SESSION['id'])) || (!isset($_SESSION['username'])) ||(!isset($_SESSION['password']))){unset($_SESSION['username']);unset($_SESSION['password']);unset($_SESSION['id']);$loggedin = 0;}else{$loggedin = 1;}?>

dogs table: SQL query: `dogs` CHANGE `petid` `petid` INT( 11 ) NOT NULL AUTO_INCREMENT ,`ownerid` `ownerid` INT( 11 ) NOT NULL DEFAULT '0',`dogname` `dogname` VARCHAR( 150 ) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL ,`dogbreed` `dogbreed` VARCHAR( 150 ) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL ,`doggender` `doggender` VARCHAR( 150 ) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL players table:SQL query: TABLE `players` CHANGE `id` `id` INT( 11 ) NOT NULL AUTO_INCREMENT ,`username` `username` VARCHAR( 150 ) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL ,`password` `password` VARCHAR( 150 ) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL ,`registered` `registered` VARCHAR( 15 ) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL ,`lastlogin` `lastlogin` VARCHAR( 15 ) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL

Link to comment
Share on other sites

I don't have time to look through all of your code, but in quickly looking at it I think you are using the wrong names to get values. When you are getting the value of the breed you wrote this $dogbreed = trim($_POST['dogbreed']); I think you meant to write $dogbreed = trim($_POST['breed']); since you named the <select> field "breed" not "dogbreed". Same with gender you wrote $_POST['doggender']; , but named the field "gender" probably the other fields have the same problems.

Link to comment
Share on other sites

  • 2 weeks later...

Would I change this:

$dog = @mysql_query("INSERT INTO dogs (ownerid, dogname, dogbreed, doggender)VALUES ('$ownerid', '$dogname', '$dogbreed', '$doggender')") or die("Error:".mysql_error());

to this:

$dog = @mysql_query("INSERT INTO dogs (id, name, breed, gender)VALUES ('$ownerid', '$dogname', '$dogbreed', '$doggender')") or die("Error:".mysql_error());

As well? Or vice versa?

Link to comment
Share on other sites

The field names here:$dog = @mysql_query("INSERT INTO dogs (ownerid, dogname, dogbreed, doggender)VALUES ('$ownerid', '$dogname', '$dogbreed', '$doggender')") or die("Error:".mysql_error());Need to match what you already have set up in the database, so they need to match these:`ownerid` `ownerid` INT( 11 ) NOT NULL DEFAULT '0',`dogname` `dogname` VARCHAR( 150 ) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL ,`dogbreed` `dogbreed` VARCHAR( 150 ) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL ,`doggender` `doggender` VARCHAR( 150 ) CHARACTER SET latin1 COLLATE latin1_german2_ci NOT NULL

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...