PHP / SQL Problem

Discussion in 'PHP' started by Teessider_2000, Jan 9, 2007.

  1. #1
    Hi guys

    I am relatively new to php / sql so please forgive me for being stupid. I am trying to insert data into a database table but every time i check phpMyAdmin it doesnt seem to have worked.

    The code is as follows:

    $dbname= 'database1';
    $dbcnx = mysql_connect('111.111.111.111', 'username', 'password');
    or die("Connection Failure to Database");
    mysql_select_db($dbname, $dbcnx) or die ($dbname . " Database not found. ");
    $sql ="INSERT INTO `test` ( `id` , `name` , `answer1` ) VALUES ( NULL , $user , $q1);";
    mysql_db_query($dbname, $sql) or die("Failed Query of " . $sql);

    Is that enough code to do the job or do i need some more? Your help would be greatly appreciated!

    Cheers in advance.
     
    Teessider_2000, Jan 9, 2007 IP
  2. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use

    or die(mysql_error());

    with the query call and post the output.
     
    Icheb, Jan 9, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try putting single quotes round non-numeric values, i.e. change your query to:
    $sql ="INSERT INTO `test` ( `id` , `name` , `answer1` ) VALUES ( NULL , '$user' , '$q1');";
    PHP:
     
    rodney88, Jan 9, 2007 IP
  4. ConnorWilson

    ConnorWilson Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think you could do it with less code...
    
    $dbname= 'database1';
    $dbcnx = mysql_connect('111.111.111.111', 'username', 'password');
    or die("Connection Failure to Database");
    mysql_select_db($dbname) or die ($dbname . " Database not found. ");
    $sql ="INSERT INTO `test` ( `id` , `name` , `answer1` ) VALUES ('', '$user' , '$q1');";
    mysql_query($sql) or die("Failed Query of " . $sql);
    
    PHP:
     
    ConnorWilson, Jan 9, 2007 IP