 |
|
11-21-2006, 10:58 PM
|
Question
|
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?
|
|
|
|
11-22-2006, 12:21 AM
|
Re: Question
|
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!
|
|
|
|
11-22-2006, 12:23 AM
|
Re: Question
|
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.
|
|
|
|
11-22-2006, 09:24 AM
|
Re: Question
|
Posts: 484
|
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 ?
|
|
|
|
11-22-2006, 09:29 AM
|
Re: Question
|
Posts: 40
|
Why do you mix these single quotes ' and these ` ?
|
|
|
|
11-22-2006, 09:51 AM
|
Re: Question
|
Posts: 565
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.
__________________
I am not smart, that's why i don't act smart
|
|
|
|
11-22-2006, 09:57 AM
|
Re: Question
|
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 ` == ' ?
|
|
|
|
11-22-2006, 10:05 AM
|
Re: Question
|
Posts: 565
Name: surajit ray
Location: inside the heart of my friends
|
No. (``)s are for table names and field names and ('') are for values.
__________________
I am not smart, that's why i don't act smart
|
|
|
|
11-22-2006, 10:08 AM
|
Re: Question
|
Posts: 40
|
Look at my question again.
|
|
|
|
11-22-2006, 10:13 AM
|
Re: Question
|
Posts: 565
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.
__________________
I am not smart, that's why i don't act smart
|
|
|
|
11-22-2006, 10:20 AM
|
Re: Question
|
Posts: 484
|
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.
|
|
|
|
11-22-2006, 10:25 AM
|
Re: Question
|
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.
|
|
|
|
11-22-2006, 10:26 AM
|
Re: Question
|
Posts: 40
|
We doubly posted.
|
|
|
|
11-22-2006, 10:40 AM
|
Re: Question
|
Posts: 565
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.
__________________
I am not smart, that's why i don't act smart
|
|
|
|
11-22-2006, 10:42 AM
|
Re: Question
|
Posts: 484
|
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!
|
|
|
|
11-22-2006, 10:42 AM
|
Re: Question
|
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 11:00 AM..
Reason: Putting link to WMT on OopSchool
|
|
|
|
11-22-2006, 10:43 AM
|
Re: Question
|
Posts: 484
|
Eek! Too much activity and path crossing
I'm putting myself in "time out"!
|
|
|
|
11-22-2006, 11:59 AM
|
Re: Question
|
Posts: 789
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
|
` is COMPLETLY dirrerent to both " and ' !
__________________
Wont :P
Back The Bulls - Unofficial Hereford United Portal
BTB Games - Flash games portal by Back The Bulls
|
|
|
|
11-22-2006, 12:30 PM
|
Re: Question
|
Posts: 2
Name: Joe
|
Thanks for all of these replys. I'll try it and see what I get.
|
|
|
|
11-22-2006, 12:39 PM
|
Re: Question
|
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.
|
|
|
|
|
« Reply to Question
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|