Results 1 to 14 of 14

Thread: Add record to MySQL with PHP...[Resolved]

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Add record to MySQL with PHP...[Resolved]

    I have a table with an auto_increment value. And I want to add a record but how do I choose what fields goes where, and how does PHP know what field are just going to be incremented by MSQL?

    I guess it will be something like

    $query = "insert into arrangement values('".$aBy."', '".$aDato."','".$aNavn."','".$aInfo."','".$aStort."')";

    But before aBy I have a field called aArrID that will be autoincremented....what does the syntax look like then?
    Last edited by NoteMe; Apr 4th, 2004 at 01:44 PM.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    Just insert it as a null string and MySQL will figure out what to do with it. PHP doesn't know what's being auto_increment'd, MySQL will handle that. Do it like so:
    Code:
      $query = mysql_query("INSERT INTO arrangement VALUES('', '$aBy', '$aDato', '$aNavn', '$aInfo', '$aStort')");
    Like Archer? Check out some Sterling Archer quotes.

  3. #3
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Its also good to tell MySQL what fields and order your using, this way if the DB structure is chnaged it will still work. And for this you just don't include the Auto_incremented field in the list .
    PHP Code:
    $query "INSERT INTO `TableName` (`Field2`, `Field3`, `Field4`, `Field5`, `Field6`) ";
    $query.= "VALUES ('$aBy', $aDato', '$aNavn', '$aInfo', '$aStort')"
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  4. #4

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Thanks both of you....now it looks more like how we did it when we used SSH to log on to our MySQL account at school...

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Electroman
    Its also good to tell MySQL what fields and order your using, this way if the DB structure is chnaged it will still work. And for this you just don't include the Auto_incremented field in the list .
    PHP Code:
    $query "INSERT INTO `TableName` (`Field2`, `Field3`, `Field4`, `Field5`, `Field6`) ";
    $query.= "VALUES ('$aBy', $aDato', '$aNavn', '$aInfo', '$aStort')"

    I have tried this up and down....but I can't get this to work...are you sure the syntax is right....not the other way around or something???

    But that other way that kows showed me works....even if it is more scary....

    Thanks to both of you...

  6. #6
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    What errors are you getting?
    One thing it might be is that I'm using ` not ' for the field names and table name, but if I use the other way round it doesn't work.

    Other thing it could be is if any of the data things are not strings becuase in that case you shouldn't use any ' around the data. e.g.
    PHP Code:
    $query "INSERT INTO `TableName` (`String`, `Number`) "
    $query.= "VALUES ('$StringData', $NumberData)"
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I was first using Varchars and one Text field....it said something like


    [quote can't insert "Here was what I had written" into fild list[/quote]


    But then I changed the Text field to Blob. and it still didn't work. Then I used kows syntaks and then it worked.....what is actually a BLOB anyway?

  8. #8
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    I actually use this with VarChars .

    Here is a piece of php that I recently used to add a record to Download table when I couldn't be bothered to use phpMyAdmin (which uses this method as well, mind it gives the field in the list but leaves the data empty):
    PHP Code:
        $query "INSERT INTO `download` (`name`, `size`, `down_count`) ";
        
    $query.= "VALUES ('SWIV Decimation By Ayden (Zip File)', '8.14MB', 0)";
        
    $results mysql_query($query) or die("Insert Failed!"); 
    I noticed phpMyAdmin even does put ' around number items.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  9. #9

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Thats weird...I have no idea what that can be.....at least it is working now....


    But do you know what a Blob is?

  10. #10
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Originally posted by NoteMe
    But do you know what a Blob is?
    I believe its for storing a lump of data. For example you could use it to store the binary data taken from a file. Haven't really used it. Do you use phpMyAdmin at all?
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  11. #11

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Yeah I am using it tonight....but using SSH at school. Why do you ask?


    I figgured out what a BLOB was anyway...

  12. #12
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Just because it shows you the SQL used to do anything you do in it. So if you do an Insert using it then see what SQL it shows you and see if that works but I would expect it to be the same as mine as thats where I kinda copied mine from .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  13. #13
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    just want to make sure; you did change the values of inside of Electro's query [which was (`Field1`, `Field2`)] to the appropriate field names within your table, right? I've came across a few people that didn't know they had to do that.

    A blob is a smaller text field.
    Like Archer? Check out some Sterling Archer quotes.

  14. #14

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Yeah I changed it. Nothing wrong there. Just a big mysteri...but I atualy don't care that much. It is working now. And I am happy.

Posting Permissions

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



Click Here to Expand Forum to Full Width