|
-
Apr 4th, 2004, 01:03 PM
#1
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.
-
Apr 4th, 2004, 01:30 PM
#2
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')");
-
Apr 4th, 2004, 01:41 PM
#3
Ex-Super Mod'rater
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.

-
Apr 4th, 2004, 01:44 PM
#4
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...
-
Apr 4th, 2004, 05:43 PM
#5
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...
-
Apr 4th, 2004, 05:48 PM
#6
Ex-Super Mod'rater
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.

-
Apr 4th, 2004, 05:58 PM
#7
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?
-
Apr 4th, 2004, 06:08 PM
#8
Ex-Super Mod'rater
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.

-
Apr 4th, 2004, 06:11 PM
#9
Thats weird...I have no idea what that can be.....at least it is working now....
But do you know what a Blob is?
-
Apr 4th, 2004, 06:15 PM
#10
Ex-Super Mod'rater
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.

-
Apr 4th, 2004, 06:16 PM
#11
Yeah I am using it tonight....but using SSH at school. Why do you ask?
I figgured out what a BLOB was anyway...
-
Apr 4th, 2004, 06:22 PM
#12
Ex-Super Mod'rater
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.

-
Apr 4th, 2004, 06:56 PM
#13
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.
-
Apr 5th, 2004, 12:40 AM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|