Hi, I was wondering if someone could look over my coding? x3 I'm really, really new to PHP (just learning, in fact) and a friend gave me this sample code, but it doesn't seem to work... could someone please help? Thank you so much. <3 Here is the full code:

<?php
include ('connect.php');
?>

<form name="form1" method="post" action="dogstore.php">
Dog's Name:
<input name="dogname" type="text">
<br>
Dog's Breed:
<select name="dogbreed">
<option value="breed1">Breed</option>
<option value="breed2">Breed</option>
<option value="breed3">Breed</option>
<option value="breed4">Breed</option>
<option value="breed5">Breed</option>
<option value="breed6">Breed</option>
<option value="breed7">Breed</option>
</select>
<br>
Dog's Gender:
<select name="doggender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br>
<input type="submit" name="Submit" value="Buy Dog">
</form>

<?php
//trim removes the white spaces from the beginning and end of the text
$dogname = trim($_POST['dogname']);
$dogbreed = trim($_POST['dogbreed']);
$doggender = trim($_POST['doggender']);
?>

<?php
//Make sure the name form is filled out.
if ((isset($_POST['dogname']))
|| ($_POST['dogname'] == "))
die("Oops! You forgot to fill in the name!");
?>

<?php
$ownerid = $_SESSION['id']
?>

<?php
if ((!isset($_POST['Buy Dog']))
{
echo "Congratulations! You've purchased a dog.";
}
else
{
echo "Congratulations! You've purchased a dog.";}
}
?>

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

Recommended Answers

All 22 Replies

Just wondering, where is line 40?

But some errors I spotted:
1. Your "$ownerid = $_SESSION" needs a semicolon.
1. I think that your if ((!isset($_POST)) has an extra parentheses at the beginning.
3. And finally, you don't need to keep on opening and closing the php tags. Unless you have HTML or something else in between them, you should just open the tag at the beginning, and then close it only at the end.

I just tried your suggestions, and received this error:

Parse error: parse error, unexpected T_BOOLEAN_OR in C:\Program Files\EasyPHP 2.0b1\www\dogstore.php on line 39

Where's line 39?

If I counted correctly:

die("Oops! You forgot to fill in the name!");

Wrap your code around tags, and post, and they should have automatic line counts. And highlighting, which is tons easier to read.[code=php] tags, and post, and they should have automatic line counts. And highlighting, which is tons easier to read.

I tried it so that I had [*code=php] at the top and [*/code] at the bottom, and it gave me:

Parse error: parse error, unexpected T_BOOLEAN_OR in C:\Program Files\EasyPHP 2.0b1\www\dogstore.php on line 41

Argh, evil code, work already. x.X

Edit: Obvious the code was without the *

No, no. Sorry for miscommunication: post that here. Put

(no space) and then put the code in between there. Then post all that here so we can see it all better.

Or better yet, edit your original post to do that.

In any case, your if structure:

<?php
//Make sure the name form is filled out.
if ((isset($_POST['dogname']))
|| ($_POST['dogname'] == "))
die("Oops! You forgot to fill in the name!");
?>

is wrong. You need to put braces around the die function. Also, you need to have two doubles quotes, i.e. || ($_POST == ""))

Eeeep, I don't understand. x.x Okay, let me fix the if structure like you said, and I'll edit my original post to show what I've got.

I'm sorry this is so difficult, I'm really new to coding, and I really appreciate your help. <3

<?php
include ('connect.php');
?>

<form name="form1" method="post" action="dogstore.php">
Dog's Name:
<input name="dogname" type="text">
<br>
Dog's Breed:
<select name="dogbreed">
<option value="breed1">Breed</option>
<option value="breed2">Breed</option>
<option value="breed3">Breed</option>
<option value="breed4">Breed</option>
<option value="breed5">Breed</option>
<option value="breed6">Breed</option>
<option value="breed7">Breed</option>
</select>
<br>
Dog's Gender:
<select name="doggender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br>
<input type="submit" name="Submit" value="Buy Dog">
</form>

<?php
//trim removes the white spaces from the beginning and end of the text
$dogname = trim($_POST['dogname']);
$dogbreed = trim($_POST['dogbreed']);
$doggender = trim($_POST['doggender']);
?>

<?php
//Make sure the name form is filled out.
if (isset($_POST['dogname']))
|| ($_POST['dogname'] == ""))
[die("Oops! You forgot to fill in the name!")];
?>

<?php
$ownerid = $_SESSION['id'];
?>

<?php
if ((!isset($_POST['Buy Dog']))
{
echo "Congratulations! You've purchased a dog.";
}
else
{
echo "Congratulations! You've purchased a dog.";}
}
?>

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

Never mind.

Member Avatar for langsor

Welcome to DaniWeb -- I'm pretty new here myself.
By the way, you can use the code-tags to separate out your code snippets.

A couple pointers to start ...
Use curly brackets {} in your code, even when you can skip them
Use line indentation for successively nested blocks of code
Both of these will help you read your code better and find common problems faster

I found some missing end of line semi-colons (;). Programming is unforgiving of such minuscule errors.

There is no reason to chop up your PHP code with multiple <?php ... ?> blocks, unless there is regular html or other plain text in between these blocks.

Your first !isset test condition was missing the (!)

Your mysql query was missing '$dogbreed',

I think that's all I found that looked wrong in this code...but I might have missed something too. ;-)

<?php
include ('connect.php');
?>

<html>
<head>
</head>
<body>
<form name="form1" method="post" action="dogstore.php">
  Dog's Name:
  <input name="dogname" type="text">
  <br>
  Dog's Breed:
  <select name="dogbreed">
    <option value="breed1">Breed</option>
    <option value="breed2">Breed</option>
    <option value="breed3">Breed</option>
    <option value="breed4">Breed</option>
    <option value="breed5">Breed</option>
    <option value="breed6">Breed</option>
    <option value="breed7">Breed</option>
  </select>
  <br>
  Dog's Gender:
  <select name="doggender">
    <option value="Male">Male</option>
    <option value="Female">Female</option>
  </select>
  <br>
  <input type="submit" name="Submit" value="Buy Dog">
</form>
</body>
</html>

<?php

//trim removes the white spaces from the beginning and end of the text
$dogname = trim( $_POST['dogname'] );
$dogbreed = trim( $_POST['dogbreed'] );
$doggender = trim( $_POST['doggender'] );

//Make sure the name form is filled out.
if ( ( !isset( $_POST['dogname'] ) ) || ( $_POST['dogname'] == '' ) ) { // !
  die( "Oops! You forgot to fill in the name!" );
}

$ownerid = $_SESSION['id']; // ;

if ( ( !isset( $_POST['Buy Dog'] ) ) {
  echo "Congratulations! You've purchased a dog."; // not really ...
} else {
  echo "Congratulations! You've purchased a dog.";
}

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

?> 

Hope this helps, and keep playing and practicing ...

I used your code langsor (thank you so much <3) but this error came up:

Parse error: parse error, unexpected '{' in C:\Program Files\EasyPHP 2.0b1\www\dogstore.php on line 49

Where is line 49?

} else {

That little block there, I believe.

Try eliminating the opening parentheses in "if ( ( !isset( $_POST ) )". And check to make sure you've got all your braces in there correctly.

Eliminating the parentheses got me a parse error saying unexpected '!', expected '(' so I put it back in, and now it gave me:

Parse error: parse error, unexpected T_BOOLEAN_OR in C:\Program Files\EasyPHP 2.0b1\www\dogstore.php on line 43

But I dunno what a boolean is. ._.; Gosh this code is messed up, I'll have to have a word with my friend. xD

OK, try this:

if (!isset($_POST['dogname'])  || ($_POST['dogname'] == '' ) ) 
{	
  die( "Oops! You forgot to fill in the name!" );
}

Works for me. I think there was an extra parentheses stuck there in the middle.

By the way, a Boolean is basically a true/false value. PHP uses that for its comparisons.

Member Avatar for langsor

Sorry to introduce an error here ...

In this line

if ( ( !isset( $_POST['Buy Dog'] ) ) {

remove one of the leading brackets ( like this

if ( !isset( $_POST['Buy Dog'] ) ) {

No more error :-)

Yay! It works now! <333 Thanks for your help, everyone! =)

But... what is an undefined index? o.O; Because now it's giving me that as a notice. Does that just mean it's going to give me that because I don't have any information for the database?

Member Avatar for langsor

I'm not sure where that error is coming from ... is it a mysql error message?

It could be from the connect.php included file, or it could be caused by the database table structure (?maybe) ... have you got anymore info on that error?

The first thing that popped to mind was you need to pass NULL to the database table for the table unique key iterator to populate that key field, if a default value is not set for that field ... but that's just a wild guess.

:-)

try this code; gope this can help you, that's power only i got. im newbie in php to

<?php
include ('connect.php');
?>


<form name="form1" method="post" action="dogstore.php">
Dog's Name:
<input name="dogname" type="text">
<br>
Dog's Breed:
<select name="dogbreed">
<option value="breed1">Breed</option>
<option value="breed2">Breed</option>
<option value="breed3">Breed</option>
<option value="breed4">Breed</option>
<option value="breed5">Breed</option>
<option value="breed6">Breed</option>
<option value="breed7">Breed</option>
</select>
<br>
Dog's Gender:
<select name="doggender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br>
<input type="submit" name="Submit" value="Buy Dog">
</form>


<?php
//trim removes the white spaces from the beginning and end of the text
$dogname = trim($_POST);
$dogbreed = trim($_POST);
$doggender = trim($_POST);


//Make sure the name form is filled out.
if (isset($_POST)) {
echo "good";
}
else {


echo "Oops! You forgot to fill in the name!";
}


$ownerid = $_SESSION;


if (!isset($_POST)) {
echo "Congratulations! You've purchased a dog.";
}
else {
echo "Congratulations! You've purchased a dog.";
}
$dog = @mysql_query("INSERT INTO dogs (ownerid, dogname, dogbreed, doggender)
VALUES ('$ownerid', '$dogname', '$doggender')") or die("Error:
".mysql_error());
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.