SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    record cannot insert

    <?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

    $dbname = 'gohhs';
    mysql_select_db($gohhs);
    $query = "INSERT INTO gohhscontact (name, address, tel, sex, company) VALUES ('EDWARD LIM', '35 jalan tanjong tokong', '048900222', 'Y', 'VT DISTRIBUTION COPORATION')";

    mysql_query($query) or die('Error, insert query failed');

    include 'closedb.php';
    ?>


    the browser return with 'Error, insert query failed'
    anything wrong with the coding ?
    I'm using php5.1.6, apache 2.0.59. database gohhs created with table gohhscontact and the field by phpmyadmin. 2.8.2.4.


    please help, many thanks.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    familiarize yourself with this function:
    mysql_error()

  3. #3
    SitePoint Enthusiast EddM's Avatar
    Join Date
    Oct 2006
    Location
    Reading, UK
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's probably not an issue here, but I've made a habit of enclosing all of my table and field names in `'s. This started back when I spent about 24 hours straight trying to diagnose a problem in my code -- turned out one of my field names was a reserved word and was causing a MySQL syntax error. I think you should get into the habit of it too.

    Code:
    INSERT INTO `gohhscontact` (`name`, `address`, `tel`, `sex`, `company`) VALUES ('EDWARD LIM', '35 jalan tanjong tokong', '048900222', 'Y', 'VT DISTRIBUTION COPORATION')
    As I said, it's probably not an issue here. Just maybe something to follow in the future.

    And yeah, get yourself some mysql error output.
    Edd Morgan
    Cake Media Studios, Wokingham UK

    Ashes to ashes, zeros and ones.

  4. #4
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One error you may get alot, is if you forget the actual names of your columns. Make sure you go back in and check out the columns to make sure you are trying to insert them correctly..

    And like clamcrusher said... mysql_error(); great function.

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tested with mysql_error();
    return the following message :
    Warning: mysql_errno(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\test.php on line 11

    Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\test.php on line 11
    : Error, insert query failed2

    i can't really understand what it means. please help me to explain,
    I have checkthe table and field, naming correct, annoying why cannot insert.

    please help me

  6. #6
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I see the problem:
    PHP Code:
    $dbname 'gohhs';
    mysql_select_db($gohhs); 
    should be
    PHP Code:
    $dbname 'gohhs';
    mysql_select_db($dbname); 

    Somewhat OT:
    Quote Originally Posted by EddM
    It's probably not an issue here, but I've made a habit of enclosing all of my table and field names in `'s. This started back when I spent about 24 hours straight trying to diagnose a problem in my code -- turned out one of my field names was a reserved word and was causing a MySQL syntax error. I think you should get into the habit of it too.
    Actually, I'm going to disagree here, and in fact suggest that EddM gets out of that habit. The use of the backtick (`) character around database and column names is a MySQLism that in fact violates the SQL standard. In short, it means that every single SQL statement you write that uses the backtick (`) will most likely work only on MySQL databases. Of course, if you only care about MySQL, then you won't have any problems, however artificially restricting yourself like this can do you no good and in fact will likely hurt you in the long run. I very highly recommend that you do not use this syntax. If you find yourself in the situation EddM described, simply changing the name of the offending column would also work and would not get you into this (arguably) bad habit.

  7. #7
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    -gives a cookie to kromey- Glad you were actually able to figure it out.. After looking back over the code, I think that really is the problem. PROPS TO Kromey!! Now lets see if it actually works

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by kromey
    Actually, I'm going to disagree here, and in fact suggest that EddM gets out of that habit. The use of the backtick (`) character around database and column names is a MySQLism that in fact violates the SQL standard. In short, it means that every single SQL statement you write that uses the backtick (`) will most likely work only on MySQL databases. Of course, if you only care about MySQL, then you won't have any problems, however artificially restricting yourself like this can do you no good and in fact will likely hurt you in the long run. I very highly recommend that you do not use this syntax. If you find yourself in the situation EddM described, simply changing the name of the offending column would also work and would not get you into this (arguably) bad habit.
    what he said
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Woot! I got a cookie!! But even better than that, I got a "what he said " from Rudy!! This really is my day!!! (Of course, to be totally honest, what I said originally came from Rudy, but even still! )

  10. #10
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    r937 is messing me up all over the place, he got more props then me, and then he outdid me in SQL... well he is a consultant, but still, makes me all pouty and stuff

  11. #11
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeraych
    r937 is messing me up all over the place, he got more props then me, and then he outdid me in SQL... well he is a consultant, but still, makes me all pouty and stuff
    Aw. Here, this magic brownie will make you feel better!

  12. #12
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    mmm, magic brownies...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  13. #13
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

    $dbname = 'gohhs';
    mysql_select_db($dbname);
    $query = "INSERT INTO productcode (productname,productseries) VALUES ('EDWARD LIM', '35 tokong')";
    echo mysql_errno($dbname) . ": " . mysql_error($dbname);
    mysql_query($query) or die('Error, insert query failed2');

    include 'closedb.php';
    ?>


    I thought it was the naming standard issue, actually not, system still return me the following error :

    Warning: mysql_errno(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\test.php on line 11

    Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\test.php on line 11
    : Error, insert query failed2


    All php guru, please help me , I'm stuck here.

  14. #14
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    does the query work when you run it outside of php?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  15. #15
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query "INSERT INTO productcode (productname,productseries) VALUES ('EDWARD LIM', '35 tokong')";
    echo 
    mysql_errno($dbname) . ": " mysql_error($dbname);
    mysql_query($query) or die('Error, insert query failed2'); 
    Change to:
    PHP Code:
    $query "INSERT INTO productcode (productname,productseries) VALUES ('EDWARD LIM', '35 tokong')";
    mysql_query($query)
        or die(
    'Error, insert query failed2: '.mysql_error()); 

  16. #16
    SitePoint Enthusiast EddM's Avatar
    Join Date
    Oct 2006
    Location
    Reading, UK
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kromey
    Actually, I'm going to disagree here, and in fact suggest that EddM gets out of that habit. The use of the backtick (`) character around database and column names is a MySQLism that in fact violates the SQL standard. In short, it means that every single SQL statement you write that uses the backtick (`) will most likely work only on MySQL databases. Of course, if you only care about MySQL, then you won't have any problems, however artificially restricting yourself like this can do you no good and in fact will likely hurt you in the long run. I very highly recommend that you do not use this syntax. If you find yourself in the situation EddM described, simply changing the name of the offending column would also work and would not get you into this (arguably) bad habit.
    Having only worked with MySQL (through the web, anyway), I had no idea that using ` was specific to MySQL. Thanks for telling me :P.
    Edd Morgan
    Cake Media Studios, Wokingham UK

    Ashes to ashes, zeros and ones.

  17. #17
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think one of the reasons why this 'feature' (backticks) has been initiated and/or is used is that that way reserved words can be used - which imho actually is a pretty bad thing.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  18. #18
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Doesn't help with the question, but this made me smile ...

    $query = "INSERT INTO gohhscontact (name, address, tel, sex, company) VALUES ('EDWARD LIM', '35 jalan tanjong tokong', '048900222', 'Y', 'VT DISTRIBUTION COPORATION')";
    Ian Anderson
    www.siteguru.co.uk

  19. #19
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gohhs
    All php guru, please help me , I'm stuck here.
    no guru

    PHP Code:
    $dbhost "localhost";
    $dbname "root";
    $dbpass "my_pw";
    $database "my_db";
     
    $link = @mysql_connect($dbhost$dbname$dbpass);
     
    if (! 
    $link) {
        die(
    "Error connecting to server.");
    }
     
    if (! 
    mysql_select_db($database$link)) {
        die(
    "Error connecting to database: $database");
    }
     
    $query "INSERT INTO productcode (productname, productseries) VALUES ('EDWARD LIM', '35 tokong')";
     
    $result mysql_query($query);
     
    if (! 
    $result) {
        die(
    "Error in query: $query");
    }
     
    printf("%d row inserted\n"mysql_affected_rows()); 
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  20. #20
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, the record is inserted into the database,
    but error remain.

    Warning: mysql_errno(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\test.php on line 11

    Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\test.php on line 11
    : Error, insert query failed2

    I can't understand why the error is there.......

  21. #21
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    youre trying to feed a string database name to the mysql_error() function. the function does not accept a string. the manual provides example usage.

  22. #22
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My last code snippet above will remove the error messages you're seeing and replace them with a "valid" MySQL error message only on error. The warnings that you are seeing right now are being issued for exactly the reason clamcrusher points out. The PHP manual is an excellent source of info and examples.

    mysql_errno - although really you don't need this one in your code at all
    mysql_error - take note of the usage in the examples


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
  •