Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help: mysql is not working or is it my code/PHP

    So i have been trying out 1-3 guides on how to connect to my database and iam useing XAMPP but i cant get anyone of them to work.


    PHP Code:
    <?php

    $host 
    "localhost";
    $user "root";
    $pass "123";

    $accounts mysql_connect("$host","$user","$pass") or die(mysql_error());
             
    mysql_select_db("accounts_data"$accounts);

    $sql "INSERT INTO users_profile (username, password, first name, last name) 
    VALUES('$_POST[name]','$_POST[password]','$_POST[name]','$_POST[first name]',$_POST[last name]')"
    ;

    mysql_query($sql$accounts);
    mysql_close($accounts);

    ?>
    </body>
    </html>
    In my database i have created accounts_data/users_profile so i got the name right i hope. In my users_profile i have 5 columns with id, username, password, first name, last name.

    when i try to run it i got

    Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting ']' in E:\XAMPP\htdocs\webBeta\connect.php on line 32

    my line 32 is the VALUES('$_POST[name]','$_POST[password]','$_POST[name]','$_POST[first name]',$_POST[last name]')";

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That's a PHP error not a MySQL error. Since the code you have here has no parse error, post your entire code.
    BTW since you're just starting, don't use the MySQL library. Use the MySQLi or PDO libraries instead; the MySQL library is deprecated and will be removed in the future.

    Edit:
    Sorry you do have a parse error, I didn't notice these names. You can't have spaces in that associate array like that. Using it will confuse your parser when you try to read it.
    Remove the spaces.
    Last edited by Fou-Lu; 08-11-2013 at 06:51 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    That's a PHP error not a MySQL error. Since the code you have here has no parse error, post your entire code.
    BTW since you're just starting, don't use the MySQL library. Use the MySQLi or PDO libraries instead; the MySQL library is deprecated and will be removed in the future.
    thanks for the info i will repost my problem there

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    See my edit as well.
    No I will move this thread there.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    That's a PHP error not a MySQL error. Since the code you have here has no parse error, post your entire code.
    BTW since you're just starting, don't use the MySQL library. Use the MySQLi or PDO libraries instead; the MySQL library is deprecated and will be removed in the future.

    Edit:
    Sorry you do have a parse error, I didn't notice these names. You can't have spaces in that associate array like that. Using it will confuse your parser when you try to read it.
    Remove the spaces.
    do you mean spaces in: first name and last name ?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That's correct.
    Perhaps I should actually define what the problem is you are seeing.
    Spaces are not illegal in the associate offset, the problem is that the parser cannot tell where to leave off your array dereference properly. Since the string is arranged technically with a fallback (associative indexes are strings, but since you are within a string it assumes that it is also a string and not a constant) instead of explicit offset dereference, you're seeing an unusual behaviour. Note as well that this is actually *backwards* from how PHP usually deals with variables which is as greedy as possible.

    My personal suggestion is to never have spaces within the associative name if you don't need to have it there.
    You can fix it by either:
    1. Renaming the associative arrays (the fields in your form as well as the use in the PHP code to remove the spaces
    2. Use complex evaluation so as to force the string to arrange it properly. That goes as such:
      PHP Code:
      $sql "INSERT INTO users_profile (username, password, first name, last name) 
      VALUES('{$_POST['name']}','{$_POST['password']}','{$_POST['name']}','{$_POST['first name']}','{$_POST['last name']}')"

      Note the curly braces and additional apostrophes. I've also corrected an error in the last value which would have caused an error on the mysql end since it was missing a ' in there.
    3. Use concatination to assemble the string. I'll show a trimmed type:
      PHP Code:
      $variable "VALUES ('" $_POST['name'] . "', '" $_POST['first name'] . "')"
    4. Assemble with sprintf:
      PHP Code:
      $variable sprintf("VALUES ('%s', '%s')"$_POST['name'], $_POST['first name']); 

    I think that covers them all :x.

    Note that you should use the mysqli as well (or the PDO, but mysqli has a procedural interface while the PDO does not). That would make use of a statement for which you would bind instead of placing within:
    PHP Code:
    if ($stmt mysqli_prepare($accounts"INSERT INTO user_profiles (`username`, `password`, `first name`, `last name`) VALUES (?, ?, ?, ?)"))
    {
        
    $username $_POST['name'];
        
    $password $_POST['password'];
        
    $firstName $_POST['first name'];
        
    $lastName $_POST['last name'];
        
    mysqli_stmt_bind_param($stmt'ssss'$username$password$firstName$lastName);
        
    mysqli_stmt_execute($stmt);
        
    mysql_stmt_close($stmt);

    I think that's right (I typically use OO code). Just check the php.net api for the mysqli constructor.

    Oh, and I also added the backticks to the properties in the db. You can also have spaces there, but you *must* use the backtick operator on it (every time). So you may want to consider removing the spaces from there as well and replacing them with either nothing or an underscore.

    Stick to no spaces everywhere except in data and you're good to go. Languages don't handle spaces in names well.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    That's correct.
    Perhaps I should actually define what the problem is you are seeing.
    Spaces are not illegal in the associate offset, the problem is that the parser cannot tell where to leave off your array dereference properly. Since the string is arranged technically with a fallback (associative indexes are strings, but since you are within a string it assumes that it is also a string and not a constant) instead of explicit offset dereference, you're seeing an unusual behaviour. Note as well that this is actually *backwards* from how PHP usually deals with variables which is as greedy as possible.

    My personal suggestion is to never have spaces within the associative name if you don't need to have it there.
    You can fix it by either:
    1. Renaming the associative arrays (the fields in your form as well as the use in the PHP code to remove the spaces
    2. Use complex evaluation so as to force the string to arrange it properly. That goes as such:
      PHP Code:
      $sql "INSERT INTO users_profile (username, password, first name, last name) 
      VALUES('{$_POST['name']}','{$_POST['password']}','{$_POST['name']}','{$_POST['first name']}','{$_POST['last name']}')"

      Note the curly braces and additional apostrophes. I've also corrected an error in the last value which would have caused an error on the mysql end since it was missing a ' in there.
    3. Use concatination to assemble the string. I'll show a trimmed type:
      PHP Code:
      $variable "VALUES ('" $_POST['name'] . "', '" $_POST['first name'] . "')"
    4. Assemble with sprintf:
      PHP Code:
      $variable sprintf("VALUES ('%s', '%s')"$_POST['name'], $_POST['first name']); 

    I think that covers them all :x.

    Note that you should use the mysqli as well (or the PDO, but mysqli has a procedural interface while the PDO does not). That would make use of a statement for which you would bind instead of placing within:
    PHP Code:
    if ($stmt mysqli_prepare($accounts"INSERT INTO user_profiles (`username`, `password`, `first name`, `last name`) VALUES (?, ?, ?, ?)"))
    {
        
    $username $_POST['name'];
        
    $password $_POST['password'];
        
    $firstName $_POST['first name'];
        
    $lastName $_POST['last name'];
        
    mysqli_stmt_bind_param($stmt'ssss'$username$password$firstName$lastName);
        
    mysqli_stmt_execute($stmt);
        
    mysql_stmt_close($stmt);

    I think that's right (I typically use OO code). Just check the php.net api for the mysqli constructor.

    Oh, and I also added the backticks to the properties in the db. You can also have spaces there, but you *must* use the backtick operator on it (every time). So you may want to consider removing the spaces from there as well and replacing them with either nothing or an underscore.

    Stick to no spaces everywhere except in data and you're good to go. Languages don't handle spaces in names well.
    iam trying to do what you are showing me but i dont get it so much because iam still very new to coding and iam just following the guides that i fund so i have it very hard to understanding what you are doing with my code

    i have end up with:

    PHP Code:
    <html>
    <head>
    <title></title>
    </head>
    <body>

    <form action="connect.php" method="post" enctype="multipart/form-data">

            Name:<input type="text" name="name"> <br/>
            Password:<input type="text" name="password"><br/>
            First name:<input type="text" name="firstname"><br/>
            Last name:<input type="text" name="lastname"><br/>
            <input type="submit" name="submit">
    </form>
    <?php

    $host 
    "localhost";
    $user "root";
    $pass "123";

    $accounts mysql_connect("$host","$user","$pass") or die(mysql_error());
                
    mysql_select_db("accounts_data"$accounts);

        
    $variable "VALUES ('" $_POST['name'] . "', '" $_POST['password'] . "','" $_POST['firstname'] . "', '" $_POST['lastname'] . "')";          
                
    if (
    $stmt mysqli_prepare($accounts"INSERT INTO user_profiles (`username`, `password`, `first name`, `last name`) VALUES (username,  password,  firstName,  lastName)"))"
    {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $firstName = $_POST['firstname'];
        $lastName = $_POST['lastname'];
        
        mysqli_stmt_bind_param($stmt, 'ssss', $username, $password, $firstName, $lastName);
        mysqli_stmt_execute($stmt);
        mysql_stmt_close($stmt);
    }  
    ?>
    </body>
    </html>

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    you're mixing and matching code blocks I've provided as possible replacements.
    Mysqli is the way to go though. Assuming you have it, take a look here for information on constructing the new $account type as a mysqli: http://php.ca/manual/en/mysqli.construct.php; you'll probably want to look at the procedural style.
    As for the statement, you do want the ? in there. They are how the statement determines placeholders used during the bind_param call. The ? in my example is literally what should be used.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •