SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb noob wants answers

    <?php
    $db_host = "dont mather";
    $db_username = "dont mather";
    $db_pass = "dont mather";
    $db_name = "dont mather";

    @mysql_connect("$db_host","$db_username","$db_pass") or die ("kommer inte åt mysql");
    @mysql_select_db("$db_name") or die ("kommer inte åt databas");

    echo ("it works");

    if( isset($_post['förnamn'],$_post['efternamn'],$_post['ålder']) )

    mysql_query("insert into `tommy` set
    `förnamn` = '{$_post['förnamn']}',
    `efternamn` = '{$_post['efternamn']}',
    `ålder` = '{$_post['ålder']}'")

    ?>
    <html>
    <body>
    <from action="" method="get">
    <input type="text" name="förnamn" /><br />
    <input type="text" name="efternamn" /><br />
    <input type="text" name="ålder" /><br />
    <input type="submit" value="post" />
    </form>
    </body>
    </html>

    The text (it works shows up so that is not the problem).

    I cant get the data to my database. Watched this video but it still doesn't work. YouTube - &#x202a;PHP Tutorials: MySQL Basics (Part 3/6)&#x202c;&rlm;

    (phpmyadmin) when i click on tommy then (id,förnamn,efternamn,ålder) shows upp.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should be this format:

    insert into <table name> (column name, column name> values <'value', 'value')
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by tangoforce View Post
    It should be this format:

    insert into <table name> (column name, column name> values <'value', 'value')
    The OP's format is fine too: MySQL :: MySQL 5.5 Reference Manual :: 12.2.5 INSERT Syntax (see format #2)

    The first error I notice is that the form method is GET, and in the php code you use $_POST. Change the method to POST, or use $_GET in the php code.

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    also note that $_post is not the same as $_POST.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    As well as the previous comments ...
    Quote Originally Posted by thaman941 View Post
    PHP Code:
    echo ("it works"); 
    using this instead of echo would tell you a lot more:
    PHP Code:
    var_dump$_POST); 
    Also, you probably have this working, but using chars like this for field names mean you must always remember to escape them with backticks `
    Quote Originally Posted by thaman941 View Post
    `ålder` = '{$_post['ålder']}'")
    For that reason I'd avoid using å - but it is quite up to you.

  6. #6
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Try this:

    PHP Code:
    if(isset($_POST['förnamn'], $_POST['efternamn'], $_POST['ålder'])) {
        
    mysql_query("INSERT `tommy` SET
            `förnamn` = '" 
    mysql_real_escape_string($_post['förnamn']) . "',
            `efternamn` = '" 
    mysql_real_escape_string($_POST['efternamn']) . "',
            `ålder` = '" 
    mysql_real_escape_string($_POST['ålder'] . "'");
        
        if (
    mysql_errno()) { 
            
    var_dump(mysql_error());
            die();
        }

    If an error has occured, it will display it.
    I also added some simple code to escape your data before it is inserted, to stop bad people from destroying your database by sending bad data.


Bookmarks

Posting Permissions

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