![]() |
|
#1
|
|||
|
|||
MySQL error when uploading CSV fileHi there!
I'm still a beginner coding in both PHP and SQL (theoretical stuff mostly) and I was hoping someone could possibly help or nudge me in the right direction with the 'application' I'm trying to create. So basically I'd like to upload a CSV file and enter the information into a MySQL database. This is my PHP code for reading and creating the SQL to insert: PHP Code:
There are 50 unit, mark, grade, period_studied fields (each), however only 2 units need to be entered and the rest (3-50) can be null. I'm trying to enter some test information (through CSV) like below: Code:
I've been able to upload the file and 'read it' but get this error when trying to insert it into the database. Quote:
I've searched around a bit and have found out that I may not have enough values to be entered. Could it be that even though the following commas have been added that the database does not like this? Would updating the way the data is entered or the insert sql line help overcome this? Any help would be greatly appreciated. Thanks. |
|||
|
#2
|
||||
|
||||
Re: MySQL error when uploading csv fileAre you trying to put everything in a single table? If so, why?
|
|
#3
|
|||
|
|||
Re: MySQL error when uploading csv fileYep, I'm putting it in one table - probably not the best design but I'm not sure how exactly I'm going to get all this information linking to one particular student through several tables.
I'm doing it to try to make a kind of reporting system. Like these students received the highest score in this unit etc. I've got it down to the following tables: student (which is what I'm trying to put the above information into), course and unit. If you've got a better design suggestion for the structure, I'd love to hear it. It'd be good to get other people's views. =) |
|
#4
|
||||
|
||||
Re: MySQL error when uploading csv fileStart by thinking of a single entity -- student. Now think of all the things you want to know about a student. For example, you might want to know a student's name, grade point average (GPA), commencement date, courses taken, assignments, grades, and other things.
For each of those things, consider whether the student can be related to one or many of that item. A student might have only one name, GPA, and commencement date; so these attributes can be stored in the same table. A student has many courses, so you can think of a course as a separate entity. Each entity should have its own table. You could store everything in the same table, but one point of a relational database like MySQL is to reduce redundancy. In a single-table structure, if you have two students who take the same class, the name of that class will be stored twice. If you change the name of the class for one student, you will have to change it for the other. This is not only tedious; it can lead to data inconsistency. Now consider the relationship between students and courses. A student can have many courses, but also a course can have many students. When you have a many-to-many relationship like this, there are actually three entities involved: students, courses, and the relationship between them. This means you need three tables: student, course, and student_course. If students and courses are each identified by a unique ID, the student_course table would store rows that contain only two fields: student_id and course_id. That way, you turn the many-to-many relationship into two one-to-many relationships. You want your tables to have one-to-many relationships. So, come up with lists of attributes that describe each of your entities and then I will help you build queries to link them together. |
|
#5
|
|||
|
|||
Re: MySQL error when uploading CSV fileHi, thanks for you help on this, I really appreciate it. I think I'm getting what you're saying.
So the information that I want would be grouped something like below then?: Student
Course
Unit
Grade_Record
I think maybe I got a little confused with this because .. the student could have many units but I wasn't sure how I'd store that into the one db field. |
|
#6
|
||||
|
||||
Re: MySQL error when uploading CSV fileYour tables are looking good.
The Grade_Record table takes care of linking the Student and Unit tables. I think it's a little unusual that a student would have only one course, but I can deal with that. Your structure provides a way to relate Course and Unit. Some courses (like language or math) could be shared among different degrees, so you might reconsider whether degree_type belongs in the Course table. When you use a foreign key, the key from the "one" side of the relationship is stored as a foreign key in the "many" table, but the "many" key is not stored in "one" table. That means there should be no grade_id in the Student table. Can a student have more than one grade per unit? If yes, then you need the grade_id field in the Grade_Record table. Otherwise, if a student can have only one grade per unit, you can use student_id and unit_id together as the primary key. Your database server considers a table's primary key when you try to insert a record (row) into that table. If Grade_Record's primary key is grade_id, you are allowed to insert two separate rows with the same student_id and unit_id (two grades for the same unit for the same student). If your primary key is student_id + unit_id, you can insert only one grade per unit per student. Can the final_mark be calculated from the final_grade? I am assuming final_grade is something like "90%" and final_mark is something like "A". The problem with having both fields in your database is that if you change "90%" to "80%", you also have to change "A" to "B". You could trade storage for processing and convert "90%" to "A" in your program. This solution introduces a different problem: all programs that access the database have to agree on how to convert final_grade to final_mark. Student
Do you have control over the data in your CSV file or is it from an external source? Do you have phpMyAdmin installed on your server? |
|
#7
|
|||
|
|||
Re: MySQL error when uploading CSV fileAhh, course in this context would be like degree eg. Bachelor of Business and Commerce or (a double degree) the Bachelor of Information Technology/Bachelor of Business and Commerce or the Master of Accounting.
Unit in this context would be the subjects studied. A student can only study one course at a time (and we would only want the currently studied course). And degree type either undergraduate or postgraduate (degree). And! you're pretty close with your assumption. final_mark (eg 95/100) calculates the final_grade (eg A or High Distinction (HD)). But you're right in perhaps letting the program handle that instead conversion itself. A student can only have one type of final_mark or final_grade per unit however a student can study the same unit twice or more (if the student fails). I think this may make a bit of a problem with the grade_record table. Period_studied (eg. Aut/2007 or Spr/2009) and unit_id (together) would never be the same - compared to unit_id and student_id which could be duplicated (studying twice). Student
Code:
and yep! I have phpmyadmin. |
|
#8
|
||||
|
||||
Re: MySQL error when uploading CSV fileBased on your description, I suggest:
Grade_Record
You might have problems with the "Spr/2009" format for period_studied. It will be difficult to sort the records by year. If you use a format like "2009-Spr" you will be able to sort the records by year, but it will still be difficult to sort chronologically because "Aut" comes before "Spr" alphabetically. If you want to be able to sort records chronologically or retrieve records for only Spring or only 2009, a better way is to use two fields: year_studied and season_studied (or similar). Another solution is to use the starting month of the period, like "2008-09" or "2009-01". If you use a points system where each unit is worth a certain number of points and the overall grade for the unit is calculated based on earned points divided by possible points, you may need to split the grade into two fields. Then again, maybe not. Once you are satisfied with the fields you have selected, you need to decide on data types and other properties for each field. A description of the different data types can be found at http://dev.mysql.com/doc/refman/5.0/en/data-types.html. If you want, you can create your database and decide on data types as you create your tables. phpMyAdmin is helpful with this process. IDs are usually INT. GPA could be VARCHAR(5) or FLOAT(4,3). Dates are DATE or DATETIME. Names are VARCHAR. Degree type could be VARCHAR, ENUM('Undergraduate', 'Postgraduate'), or ENUM('U', 'P'). Grade could be FLOAT(5,2). It's your database, though, so use the data types that are right for you. INT IDs commonly have the auto_increment property. When you set up a table in phpMyAdmin, you can find it in the "Extra" drop-down list. The UNSIGNED attribute means that a number can only be positive. It has no sign. Don't associate the term "unsigned" with a security certificate. I suppose I should confirm your terminology on another thing. What is process_category in the Student table? Last edited by MisterChucker : 10-Apr-2009 at 11:26.
Reason: Wrong arguments for float
|
|
#9
|
|||
|
|||
Re: MySQL error when uploading CSV file=] Here in Australia, Autumn comes before Spring (as well as alphabetically) so I don't think it'll be a problem. But it might be a good idea to separate the two as you said - year_studied and semester_studied.
process_category is something which will help us identify what kind of student (administration wise) we have - eg international, local, fee paying, scholarship etc. We only want the code which the users will already understand, so its ok to keep this as a single attibute and not expand it into a table. Thank you for all your help, I'll go ahead and develop the tables and take a look at the coding and update it and let you know if I encounter any difficulties! =] |
|
#10
|
|||
|
|||
Re: MySQL error when uploading CSV fileHi!
I'm having some trouble when I try to enter a date into the table. This is how I'm putting the information in: PHP Code:
This is the input line: Code:
The query [that is echo'ed] comes out like this: Code:
commencement_date's type in the table is date without a format etc being specified. But when I look in the phpmyadmin interface the commencement_date is: 0000-00-00. When I edit it through there with 2006/02/20 it saves fine. Is there something I'm missing/doing wrong to enter the date? Advanced thanks! Last edited by lanz : 23-Apr-2009 at 08:08.
Reason: Adding commencement_date type (from table)
|
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| After execution - Error cannot locate /Skin File? | WSCH | C++ Forum | 1 | 05-Mar-2005 20:03 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 10:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 11:28 |
| CD Buring Failed | skanth2000 | Computer Hardware Forum | 1 | 15-Nov-2003 03:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The