Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2003, 10:59 AM
Registered User
 
Join Date: Aug 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shabboleth
Default Assigning value to string

Hello All,

I am trying to assign a value to a string in order to use it in a mysql_query statement. But I am having trouble. Here is my code:

    $username = $HTTP_POST_VARS['username'];
    echo $username;
    echo "<br>";
    $query = "INSERT INTO jpnquiz.users VALUES('";
    $query = $query + $username;
    $query = $query + "','5','0','0','0')";
        echo $query;
    echo "<br>";

When the above code runs, the value assigned to $username echoes properly but the value assigned to $query shows up as a zero. Again, all assistance is greatly appreciated.

Glen
 
Old December 29th, 2003, 03:40 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

It looks like you're trying to use JavaScript style concatenation.

In PHP the period is used to concatenate, not the plus sign.

$username = $HTTP_POST_VARS['username'];
    echo $username;
    echo "<br>";
    $query = "INSERT INTO jpnquiz.users VALUES('";
    $query = $query . $username;
    $query = $query . "','5','0','0','0')";
        echo $query;
    echo "<br>";

Is one way of doing it.

$username = $HTTP_POST_VARS['username'];
    echo $username;
    echo "<br>";
    $query = "INSERT INTO jpnquiz.users VALUES('";
    $query .= $username;
    $query .= "','5','0','0','0')";
        echo $query;
    echo "<br>";

The .= combination of operators is a shortcut for doing that.

And the best way would be:

$query = "INSERT INTO jpnquiz.users VALUES('".$username."','5','0','0','0')";

-OR- The apostrophe is not a valid variable name character, so $username would be recognized and substituted for the correct variable value within string context.

$query = "INSERT INTO jpnquiz.users VALUES('$username','5','0','0','0')";

-OR- Curly Brace Syntax

$query = "INSERT INTO jpnquiz.users VALUES('{$username}','5','0','0','0')";

All do pretty much the same thing.

See:
http://www.php.net/manual/en/language.types.string.php

Also you do not need to specify the database name when you are inserting into a single table.

$query = "INSERT INTO `users` VALUES('{$username}','5','0','0','0')";
Should also work. (Backticks optional)

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 29th, 2003, 03:50 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Also you should avoid using the long predefined variable names, like HTTP_*_VARS. These are deprecated and won't be available in PHP 5. Use the shorter superglobal counterparts.. for this example, $_POST["username"]; instead of $HTTP_POST_VARS["username"]; These have been available in PHP since PHP 4.1.0. If you are using a version of PHP older than that I would strongly suggest upgrading or urging your ISP/admin to upgrade. If all else fails you might consider writing a script that transfers values from the old longer names to the shorter ones for version portability purposes. If you would like to see an example just ask.

See:
http://www.php.net/manual/en/languag...predefined.php

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 29th, 2003, 04:03 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Quote:
quote:
Also you do not need to specify the database name when you are inserting into a single table.

$query = "INSERT INTO `users` VALUES('{$username}','5','0','0','0')";
Should also work. (Backticks optional)
What was I thinking? I think that doing this this way: jpnquiz.users would look for a table named 'jpnquiz' with a field named 'users' and attempt to insert data into the users field. So I do not think that would produce your expected result. The database must be selected before hand. (I am just assuming you are using MySQL here..). Based on the INSERT statement syntax from the MySQL manual my assumptions are correct.

Quote:
quote:from: http://www.mysql.com/doc/en/INSERT.html

    INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
        [INTO] tbl_name [(col_name,...)]
        VALUES ((expression | DEFAULT),...),(...),...
        [ ON DUPLICATE KEY UPDATE col_name=expression, ... ]
or INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
        [INTO] tbl_name [(col_name,...)]
        SELECT ...
or INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
        [INTO] tbl_name
        SET col_name=(expression | DEFAULT), ...
        [ ON DUPLICATE KEY UPDATE col_name=expression, ... ]
: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 29th, 2003, 05:24 PM
Registered User
 
Join Date: Aug 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shabboleth
Default

Thanks everyone, that got me on the right track.

Glen






Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning combobox value Manoah Javascript 2 July 29th, 2008 12:20 AM
Assigning value from variable cyberddindia Classic ASP Basics 3 November 7th, 2006 11:57 AM
Separating parts of a string and assigning them to anubandh ASP.NET 1.0 and 1.1 Basics 1 May 12th, 2006 12:47 PM
Assigning string to a number field p_gauri7 Crystal Reports 3 December 10th, 2004 10:43 AM
Assigning radiobuttons hydra14 C# 7 June 5th, 2004 12:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.