Welcome! Log In Create A New Profile

Advanced

Problem with the Practical Project

Posted by DesmondMpofu 
Announcements Last Post
Announcement myUnisa availability 21 to 24 March 2019 03/25/2019 12:25PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Problem with the Practical Project
April 23, 2010 04:17PM
I had such a problem trying to have my code insert a record onto my table. The problem (that I later on figured out) was on the line below. Can anyone see what is wrong with it???? Will let you know the bug soon
Language: PHP
$query = "INSERT INTO student VALUES ('$fname', '$sname', '$msname', '$init', '$title', '$dob', '$sex', '$lang', '$idno', '$telh', '$telw', '$cel', '$fax', '$email', $address', 'sno')";
Tny
Re: Problem with the Practical Project
April 23, 2010 08:09PM
Yes, you left out the table names for the values
avatar
Mac
Re: Problem with the Practical Project
April 26, 2010 08:16AM
You only include the table names when you do an update. $sno and not sno.
Re: Problem with the Practical Project
April 26, 2010 08:42AM
Just go through that line without thinking about anything else associated with the line and you will surely pick the error.
Tny
Re: Problem with the Practical Project
April 28, 2010 08:38PM
What I meant was the row names for the query above were left out that match up to the values entered eg:
Language: PHP
INSERT INTO course (cname) VALUES ('$course')
would insert a record.
Whereas
Language: PHP
INSERT INTO course VALUES ('$course')
would display the error
Language: PHP
#1136 - Column count doesn't match value count at row 1
avatar
Mac
Re: Problem with the Practical Project
April 29, 2010 07:06AM
Let me explain: For example, you have a table with 3 columns. You then use this query here as below:

Language: PHP
INSERT INTO course VALUES ('$1', '$2' )
This will generate the error " Column count doesn't match value count at row 1" because you have just two colums in your query while the should be three

It should be

Language: PHP
INSERT INTO course VALUES ('$1', '$2', '$3' )

As long as you make sure that you have included the correct number of variables in the brackets, and they are in the correct place as per your column order in the db, you can use the query as here above.

If you wish to insert just one variable, then you use

Language: PHP
INSERT INTO course VALUES ('$1', '', '' )

leaving the rest empty.

Desmond's query is thus fine, as long as he ensures he has the correct number of columns (and there are no syntax/typing errors).

Of course, you can use

Language: PHP
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

My viewpoint is that that is an overkill. You want streamlined code. Why type more when you can type less?
avatar Re: Problem with the Practical Project
April 29, 2010 08:14AM
I have lil problem i need clarity on since we have cid on table course and also in course_student can't we use only one of them as primary key and other as foreign key so if we were to update one table on course therefore it must auto update course_student [e.g. if i delete 1 student therefore the cid on course that is assigned to this particularly student must be removed in oder to avoid records that are not known to which field they belong to!

gbakamela [77234715]
Re: Problem with the Practical Project
April 29, 2010 09:32AM
I just left out The ' and typed $address' instead of '$address'. That simple error became a huge nightmare. Otherwise that works pretty fine
avatar
Mac
Re: Problem with the Practical Project
April 29, 2010 09:47AM
DesmondMpofu Wrote:
-------------------------------------------------------
> I just left out The ' and typed $address'
> instead of '$address'. That simple error became a
> huge nightmare. Otherwise that works pretty fine

and sno of course -> $sno (not an error but no value - just sno is inserted..

Typos will keep you awake at night....
avatar
Mac
Re: Problem with the Practical Project
April 29, 2010 10:02AM
gcobani Wrote:
-------------------------------------------------------
> I have lil problem i need clarity on since we have
> cid on table course and also in course_student
> can't we use only one of them as primary key and
> other as foreign key so if we were to update one
> table on course therefore it must auto update
> course_student [e.g. if i delete 1 student
> therefore the cid on course that is assigned to
> this particularly student must be removed in oder
> to avoid records that are not known to which field
> they belong to!

Please start a new thread next time smiling smiley Each thread is a single problem.
You can query (select) from two tables (a bit advanced for this course but read up on joins if you want) but when you insert and update, run sep queries toegther.
i.e.
UPDATE table 1
UPDATE table 2
DELETE FROM table3

The purpose of a foregein key is not for the purposes you want. There are ways to achive this (way above this course - triggers, mysqli) but in general.... always keep it simple. I'm watching you! smiling smiley
avatar Re: Problem with the Practical Project
April 29, 2010 12:39PM
thanks mac!eye popping smiley

gbakamela [77234715]
Sorry, only registered users may post in this forum.

Click here to login