php wont insert into ...

Discussion in 'PHP' started by pepe_lepew1962, Feb 1, 2009.

  1. #1
    Hello:

    I am trying to get my insert into mysql to work and it just plain does not work. I have copied the insert part into a text file and manually executed the code via source and it works but for some reason it won't via php. Is there someting I am missing or not getting?

    <?php
    // Loop to pass the variables from previous script
    //
    foreach (array_keys($_POST) as $key)
    {
    $$key = $_POST[$key];
    //print "$key is ${$key}<br />";
    print "$key ==> ${$key}<br />";
    }
    //
    //
    mysql_connect("localhost", "root", "xxxxxxxxx") or die('Could not connect: ');
    mysql_select_db("test") or die('Could not connect again: ');
    //
    $sql = ("INSERT INTO tblnadda (tblNADA_User01,tblNADA_User02) VALUES ('namef','namem')") or die('Could not add records ');

    $result = mysql_query("SELECT * FROM tblnadda");

    $num_rows = mysql_num_rows($result);

    echo "$num_rows Rows\n";
    //
    ?>
     
    pepe_lepew1962, Feb 1, 2009 IP
  2. traffic.web

    traffic.web Guest

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you have invalid code,try this :

     
    traffic.web, Feb 1, 2009 IP
  3. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Please dont use complicated code if target can be achieved simply.

    Just follow these steps.

    1) For example your form have two fields, first one is username and second one is password. then you just need to use this simple technqiue.

    $username=$_POST['username'];
    $password=$_POST['password'];
    $sql="insert into tblnadda values('$username','$password')";
    $result=mysql_query($sql);
    if($result)
    {
    echo "Records entered successfully";
    }
    else{
    mysql_error();
    }
     
    mrmaf, Feb 2, 2009 IP