PDA

Click to See Complete Forum and Search --> : Add record to MySQL with PHP...[Resolved]


NoteMe
Apr 4th, 2004, 01:03 PM
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?

kows
Apr 4th, 2004, 01:30 PM
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:
$query = mysql_query("INSERT INTO arrangement VALUES('', '$aBy', '$aDato', '$aNavn', '$aInfo', '$aStort')");

Electroman
Apr 4th, 2004, 01:41 PM
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 :D.$query = "INSERT INTO `TableName` (`Field2`, `Field3`, `Field4`, `Field5`, `Field6`) ";
$query.= "VALUES ('$aBy', $aDato', '$aNavn', '$aInfo', '$aStort')";

NoteMe
Apr 4th, 2004, 01:44 PM
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...:D

NoteMe
Apr 4th, 2004, 05:43 PM
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 :D.$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...

Electroman
Apr 4th, 2004, 05:48 PM
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. $query = "INSERT INTO `TableName` (`String`, `Number`) ";
$query.= "VALUES ('$StringData', $NumberData)";

NoteMe
Apr 4th, 2004, 05:58 PM
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?

Electroman
Apr 4th, 2004, 06:08 PM
I actually use this with VarChars :confused:.

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): $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.

NoteMe
Apr 4th, 2004, 06:11 PM
Thats weird...I have no idea what that can be.....at least it is working now....


But do you know what a Blob is?

Electroman
Apr 4th, 2004, 06:15 PM
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?

NoteMe
Apr 4th, 2004, 06:16 PM
Yeah I am using it tonight....but using SSH at school. Why do you ask?


I figgured out what a BLOB was anyway...;)

Electroman
Apr 4th, 2004, 06:22 PM
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 :D.

kows
Apr 4th, 2004, 06:56 PM
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.

NoteMe
Apr 5th, 2004, 12:40 AM
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.