Reply
Old 11-21-2006, 09:58 PM Question
Junior Talker

Posts: 2
Name: Joe
I'm a little new to PHP and I have a question.

This is the snipet I'm having problems with:

Code:
 CODE

    $name   =   $_POST['name'];
    $price  =   $_POST['price'];
    $item   =   $_POST['item'];
    $date   =   $_POST['date'];
    $query = "INSERT INTO `secondlifetrans` ( `Name` , `Item` , `Price` , `Date` )
VALUES (
'$name', '$item', '$price', '$date'
);";

This, for some reason, outputs empty strings into the sql database, but I'm sure all the variables are completly defined. Any help, anyone?
PHP Newby is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 11-21-2006, 11:21 PM Re: Question
Super Talker

Posts: 124
Name: Chris
Well there is two possibilities here. Your server might not allow the use of $_POST you might have to use $HTTP_POST_VARS .

This is highly unlikely unless you are using an older version of php. Any versions before 4.1.0 will use this method but anything after are setup to use $_POST.

The second thing is to add a debug line in there. try doing print_r($_POST); to see if the data Is truely being passed through. If that comes out empty you have a problem somewhere!
__________________
Chris - Trying to help others and learn myself!
http://www.nvision-media.com
ctess is offline
Reply With Quote
View Public Profile
 
Old 11-21-2006, 11:23 PM Re: Question
Super Talker

Posts: 124
Name: Chris
Oh I also just caught this:

Code:
$query = "INSERT INTO `secondlifetrans` ( `Name` , `Item` , `Price` , `Date` )
VALUES (
'$name', '$item', '$price', '$date'
);";
It Should be:

Code:
$query = "INSERT INTO `secondlifetrans` ( `Name` , `Item` , `Price` , `Date` )
VALUES (
'$name', '$item', '$price', '$date'
)";
You added an extra ; in your query line. That will throw an error if you have all yoru error messages on. This is probably the most likely answer for yoru problem.
__________________
Chris - Trying to help others and learn myself!
http://www.nvision-media.com
ctess is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 08:24 AM Re: Question
Ultra Talker

Posts: 481
ctess: semi colons at the end of a query are not a problem (in fact, I think technically they're meant to be there).

The problem could also be: are you sure you're using POST and not GET? What happens if you replace $_POST with $_REQUEST ?
__________________
Free PHP Obfuscator
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 08:29 AM Re: Question
Experienced Talker

Posts: 40
Why do you mix these single quotes ' and these ` ?
kgun is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 08:51 AM Re: Question
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 551
Name: surajit ray
Location: inside the heart of my friends
Quote:
Why do you mix these single quotes ' and these ` ?
No problem with that kgun.
I think TwistMyArm is right. The problem is not in this part and we need to know a bit more to answer.
__________________
Think+, work +, but not HIV + :)
jito is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 08:57 AM Re: Question
Experienced Talker

Posts: 40
Ok, I have never used those ` quotes and are consitent in my use and use ' and ". Without taking the time to test it, will it say that ` == ' ?
kgun is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 09:05 AM Re: Question
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 551
Name: surajit ray
Location: inside the heart of my friends
No. (``)s are for table names and field names and ('') are for values.
__________________
Think+, work +, but not HIV + :)
jito is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 09:08 AM Re: Question
Experienced Talker

Posts: 40
Look at my question again.
kgun is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 09:13 AM Re: Question
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 551
Name: surajit ray
Location: inside the heart of my friends
if ur question is "IS ` and ' are same?" my answer is no.
May be i interprited it wrongly, as my english knowledge is not great.
__________________
Think+, work +, but not HIV + :)
jito is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 09:20 AM Re: Question
Ultra Talker

Posts: 481
jito is right. I, too, thought you were asking if they were the same and they are not. Normally, you don't see problems with using single quotes all the time, but you are meant to use backticks (`) when referring to table names and columns.

Take, as a really bad example, a table with column 'A' and column 'B'. Imagine you want to set the value of column B to be the same as the value in column A.

By doing something like:
'B' = 'A'
you end up making the value in column B set to 'A' (as opposed to the value in column A).

Instead, you need to use:
`B` = `A`

Strange little things like that... normally MySQL can work out what you're doing, but it does get confused more often than you would expect.
__________________
Free PHP Obfuscator
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 09:25 AM Re: Question
Experienced Talker

Posts: 40
Did you edit your post? I asked is:

`=='

not

`=="

The devil is in the details :-)

I have never used the ` quote in PHP. I use " for values and ' for text strings.

$x1 = 'OOP PHP is cool';

I know the difference between

echo "x1";

and

echo 'x1';

To be more precise is

echo 'x1'; == to echo `x1`;

?

' and ` should have different ASCII codes.
kgun is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 09:26 AM Re: Question
Experienced Talker

Posts: 40
We doubly posted.
kgun is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 09:40 AM Re: Question
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 551
Name: surajit ray
Location: inside the heart of my friends
i think u have the answer already . think we should stop here and wait for that guy's reply.
__________________
Think+, work +, but not HIV + :)
jito is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 09:42 AM Re: Question
Ultra Talker

Posts: 481
Firstly, kgun, you say:
Quote:
Did you edit your post? I asked is:

`=='

not

`=="

The devil is in the details :-)
But unless jito did edit his post, I don't see either one of us referring to double-quotes. The closest he says is:
Quote:
No. (``)s are for table names and field names and ('') are for values.
But that last thing in brackets is two single quotes next to each other, not a double quote. The devil is definitely in the details .

Anyway, the backtick is not for strings in PHP. They're for MySQL commands / queries (that, in this context, just happen to be made by the PHP language). Hopefully that clarifies things.

For completeness, backticks are used in PHP for executing shell commands (see http://php.net/language.operators.execution ) but the differentiation that we are trying to make above is purely in regards to MySQL queries. Hopefully that makes a bit more sense!
__________________
Free PHP Obfuscator
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 09:42 AM Re: Question
Experienced Talker

Posts: 40
We learn as long as we live or the opposite. We doubly posted and you answered before I posted my long post.

'' == " ?

There is a difference.

I don't have laser eyes :-)

This is a great thread. Made two new links on my OopSchool, even if it is not very OO. For those interested:

Name of links:

Execution operator.

The devil is in the details.

Now the top two links in the PHP section of the index page.

Last edited by kgun : 11-22-2006 at 10:00 AM. Reason: Putting link to WMT on OopSchool
kgun is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 09:43 AM Re: Question
Ultra Talker

Posts: 481
Eek! Too much activity and path crossing

I'm putting myself in "time out"!
__________________
Free PHP Obfuscator
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 10:59 AM Re: Question
mad_willsy's Avatar
Super Spam Talker

Posts: 774
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
` is COMPLETLY dirrerent to both " and ' !
__________________
Wont :P
SoldierExteme.com - Free MMORPG - sign-up today!
Backthebulls.com - Free online games
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Old 11-22-2006, 11:30 AM Re: Question
Junior Talker

Posts: 2
Name: Joe
Thanks for all of these replys. I'll try it and see what I get.
PHP Newby is offline
Reply With Quote
View Public Profile
 
Old 11-22-2006, 11:39 AM Re: Question
Super Talker

Posts: 124
Name: Chris
Proper MYSQL Syntax uses backticks to specify the difference between tablenames/rows, etc from variables. I have actually had errors before because I didn't use backticks on some row names because they were the same as a few of my global variables and PHP tried to interpret it funny (I haven't had this problem in php5 only past php versions).

Basicly its for proper syntax. It's like saying why do you use: $array["bleh"] or $array['bleh'] when $array[bleh] works. It's improper syntax and poor coding to use the later version.

As for the original problem You obviously don't see why the semicolon is an issue but lets take a look at his string:

Code:
$query = "INSERT INTO `secondlifetrans` ( `Name` , `Item` , `Price` , `Date` )
VALUES (
'$name', '$item', '$price', '$date'
);";
Ok we have the original query, now let's use that query string:

Code:
mysql_query("INSERT INTO `secondlifetrans` ( `Name` , `Item` , `Price` , `Date` ) VALUES ('$name', '$item', '$price', '$date');", $db);
See the problem with the extra semicolon now? That will give you nothing.

Also I noticed a formatting problem. General formatting you should always use lowercase for your row names and never capitalize....but it depends on the coder.
__________________
Chris - Trying to help others and learn myself!
http://www.nvision-media.com
ctess is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Question

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off