Jump to content

Recommended Posts

Hello, could anyone help me with this?

 

$sql = "SELECT STUDENTS.SID, STUDENTS.SNAME, STUDENTS.PHOTO,

              STUDENT_COURSE.CID, STUDENT_COURSE.GRADE

              FROM STUDENTS, STUDENT_COURSE

              WHERE (STUDENTS.SID LIKE '$SID%')";                            //line 50

 

After this there is a PHP code which inserts the data to the $grade, $comments;

The error I`m getting is:

"Warning: Missing argument 4 for student_course:insert_student_course(), called in htdocs\Student_Course_Insert.php on line 50

Success. Number of rows affected: 1"

 

That is the whole message, which is confusing, because it inserts data to the database, but to the wrong column! Grade is inserted to the SID and CID colomuns twice, and comments inserted to the grade column.

 

student_course table:

CREATE TABLE IF NOT EXISTS `student_course` (

`SID` varchar( 8 ) NOT NULL,

`CID` varchar(11) NOT NULL,

`GRADE` varchar(1) NOT NULL,

`COMMENTS` varchar(50) NOT NULL,

PRIMARY KEY (`SID`,`CID`),

KEY `CID` (`CID`),

KEY `SID` (`SID`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

student table

CREATE TABLE students (

sid int( 8 ) NOT NULL auto_increment,

sname varchar(50) NOT NULL default '',

address varchar(50) NOT NULL default '',

post_code varchar(10) NOT NULL default'',

photo blob,

PRIMARY KEY (sid)

) ENGINE=INNODB AUTO_INCREMENT=1;

 

mysql server version 5.1.41.

Thank you in advance!!!

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/223211-warning-missing-argument/
Share on other sites

Post code around line 50.  Just because it says line 50 means it's line 50.  The interpreter is making its best guess.  Also, post what's in insert_student_course() (and what calls it?).

 

Use the # (Insert Code) button from the toolbar above the smilies so the code is more readable.

 

Also, try running the query on line 50 on your own in phpMyAdmin to see whether it works or gives you an error.

 

This wouldn't happen to be for Moodle would it?

Hello, Thanks for taking the time and reading my post!

 

I inserted the code to phpMyAdmin, and it was working. But it returned an empty result...

SELECT STUDENTS.SID, STUDENTS.SNAME, STUDENTS.PHOTO,
               STUDENT_COURSE.CID, STUDENT_COURSE.GRADE
               FROM STUDENTS, STUDENT_COURSE
               WHERE (STUDENTS.SID LIKE '$SID%')

 

Here is the code around line 50:

(I did not dare to post it first, because it says in the guidelines, that I should not post PHP here :))

else {
            $grade = $_POST['grade'];
            $comments = $_POST['comments'];
            $db1 = new student_course();
            $db1->openDB();
            $numofrows = $db1->insert_student_course($sql = "SELECT STUDENTS.SID, STUDENTS.SNAME, STUDENTS.PHOTO,
               STUDENT_COURSE.CID, STUDENT_COURSE.GRADE
               FROM STUDENTS, STUDENT_COURSE
               WHERE (STUDENTS.SID LIKE '$SID%')", $grade, $comments);
            
            echo "Success. Number of rows affected:
            <strong>{$numofrows}<strong>";
            $db1->closeDB();

 

And here is the insert_student_course() function

 

function insert_student_course($id, $id, $grade, $comments) {
        $esc_grade = mysql_real_escape_string($grade, $this->conn);
        $esc_comments = mysql_real_escape_string($comments, $this->conn);
        $sql = "INSERT INTO student_course (sid, cid, grade, comments)
        VALUES ('{$id}', '{$id}', '{$esc_grade}', '{$esc_comments}')";
        $result = mysql_query($sql, $this->conn);
        if (!$result) {
            die("SQL Insertion error: " . mysql_error());
        } else {
            $numofrows = mysql_affected_rows($this->conn);
            return $numofrows;
        }
    }

 

What is Moodle? Sorry, I just started to learn PHP and I am new to this!

 

Moodle's an entire framework/application for teaching environments made from PHP and MySQL.  I thought maybe your code was from it.

 

Anyway, your error is telling you exactly what's wrong (for once  ::) ).

"Warning: Missing argument 4 for student_course:insert_student_course(), called in htdocs\Student_Course_Insert.php on line 50

Success. Number of rows affected: 1"

 

Your function insert_student_course() accepts 4 arguments,

function insert_student_course($id, $id, $grade, $comments) {

but you send 3

$numofrows = $db1->insert_student_course(
          $sql = "SELECT STUDENTS.SID, STUDENTS.SNAME, STUDENTS.PHOTO,
               STUDENT_COURSE.CID, STUDENT_COURSE.GRADE
               FROM STUDENTS, STUDENT_COURSE
               WHERE (STUDENTS.SID LIKE '$SID%')", 
          $grade,
          $comments);

 

Also, there's an ambiguity in your function.  There are two arguments that accept $id.  Which is the real $id?  I'm not sure what the rules are for PHP, if any exist, for that scenario.

Hello :)

 

I added one more argument, but now I`m getting this error:

 

Warning: Missing argument 4 for student_course::insert_student_course(), called in C:\xampp\htdocs\cwk\Student_Course_Insert.php on line 54 and defined in C:\xampp\htdocs\cwk\Class_Student_Course.php on line 10

SQL Insertion error: Duplicate entry 'A-A' for key 'PRIMARY'

 

The code is OK on phpMyAdmin (MySQL returned an empty result set (i.e. zero rows).

 

else {
            $grade = $_POST['grade'];
            $comments = $_POST['comments'];
            $db1 = new student_course();
            $db1->openDB();
            $numofrows = $db1->insert_student_course($sql = "SELECT STUDENTS.SID, STUDENTS.SNAME, STUDENTS.PHOTO,
               STUDENT_COURSE.CID, STUDENT_COURSE.GRADE
               FROM STUDENTS, STUDENT_COURSE
               WHERE (STUDENTS.SID LIKE '$SID%');
                    SELECT COURSES.CID, COURSES.CNAME,
                    STUDENT_COURSE.SID, STUDENT_COURSE.GRADE
                    FROM COURSES, STUDENT_COURSE 
                    WHERE (COURSES.CID LIKE '$CID%'); ", $grade, $comments);

 

Do you have any idea, what am I doing wrong?

I really appreciate your your answers!

 

Look at my post above.  Do you need 3 or 4 arguments?  Do you have two $id's by mistake or are they supposed to be there?  If you're supposed to have 4 arguments, rename of your $id's appropriately (like if you wanted a $student_id and a $teacher_id or something).  If you made a mistake in writing your function and wanted 3 arguments, remove an $id.

 

Adjust your code accordingly if you rename an $id.  The error is telling you exactly what's wrong again.  Use the color in your post to your own advantage.  It's there for a reason (and get Notepad ++ or Scite for coding if your editor doesn't do syntax highlighting (colors) and is black and white).

Sorry that I`m such a dunce...

 

I need 4 arguments, as I have 4 columns in that table.

 

Here is my table:

CREATE TABLE  `student_course` (
  `SID` int( NOT NULL,
  `CID` int(11) NOT NULL,
  `GRADE` varchar(1) NOT NULL,
  `COMMENTS` varchar(50) NOT NULL,
  PRIMARY KEY (`SID`,`CID`),
  KEY `Index_SID` (`SID`),
  KEY `Index_CID` (`CID`),
  CONSTRAINT `FK_student_course_2` FOREIGN KEY (`CID`) REFERENCES `courses` (`cid`) ON DELETE RESTRICT,
  CONSTRAINT `FK_student_course_1` FOREIGN KEY (`SID`) REFERENCES `students` (`sid`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

 

This is the function, I`m trying to use:

function insert_student_course($cid, $sid, $grade, $comments) {
        $esc_grade = mysql_real_escape_string($grade, $this->conn);             
        $esc_comments = mysql_real_escape_string($comments, $this->conn);
        $sql = "INSERT INTO student_course (sid, cid, grade, comments)         
        VALUES ('{$cid}', '{$sid}', '{$esc_grade}', '{$esc_comments}')";
        $result = mysql_query($sql, $this->conn);
        if (!$result) {
            die("SQL Insertion error: " . mysql_error());
        } else {
            $numofrows = mysql_affected_rows($this->conn);
            return $numofrows;
        }
    }

 

And this the code, and this still not working:

 

else {
            $grade = $_POST['grade'];
            $comments = $_POST['comments'];
            $db1 = new student_course();
            $db1->openDB();
            $numofrows = $db1->insert_student_course($sql = "SELECT STUDENTS.SID, STUDENTS.SNAME, STUDENTS.PHOTO,
               STUDENT_COURSE.CID, STUDENT_COURSE.GRADE
               FROM STUDENTS, STUDENT_COURSE
               WHERE (STUDENTS.SID LIKE '$SID%');
                    SELECT COURSES.CID, COURSES.CNAME,
                    STUDENT_COURSE.SID, STUDENT_COURSE.GRADE
                    FROM COURSES, STUDENT_COURSE 
                    WHERE (COURSES.CID LIKE '$CID%'); ", $grade, $comments);

            echo "Success. Number of rows affected:
            <strong>{$numofrows}<strong>";
            $db1->closeDB();

 

Could you please pinpoint my mistake?

 

I still getting error like:

Warning: Missing argument 4 for student_course::insert_student_course(), called in C:\xampp\htdocs\cwk\Student_Course_Insert.php on line 54 and defined in C:\xampp\htdocs\cwk\Class_Student_Course_Proba.php on line 10

SQL Insertion error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '// insert new record into table VALUES ('SELECT STUDENTS.SID, STUDENTS.S' at line 1

 

Thank you!

Seriously... and with all due respect... but you  better have to look/study the code that you have and analyze it before to try to run.... some clues:

 

This code is garbage:

 $numofrows = $db1->insert_student_course($sql = "SELECT STUDENTS.SID, STUDENTS.SNAME, STUDENTS.PHOTO,
               STUDENT_COURSE.CID, STUDENT_COURSE.GRADE
               FROM STUDENTS, STUDENT_COURSE
               WHERE (STUDENTS.SID LIKE '$SID%');
                    SELECT COURSES.CID, COURSES.CNAME,
                    STUDENT_COURSE.SID, STUDENT_COURSE.GRADE
                    FROM COURSES, STUDENT_COURSE 
                    WHERE (COURSES.CID LIKE '$CID%'); ", $grade, $comments);

 

why?... simple... just look your function definition:

 

function  insert_student_course($cid, $sid, $grade, $comments) 

 

you will notice that it is expecting 4 arguments... CID, SID, GRADE and COMMENTS ... right?

 

you are getting GRADE & COMMENTS using POST ($_POST['grade'] and $_POST['comments']) , therefore in the code below you need to provide CID & SID in some way.... do you think that those SELECT provide those values to the function?

 
            $numofrows = $db1->insert_student_course($sql = "SELECT STUDENTS.SID, STUDENTS.SNAME, STUDENTS.PHOTO,
               STUDENT_COURSE.CID, STUDENT_COURSE.GRADE
               FROM STUDENTS, STUDENT_COURSE
               WHERE (STUDENTS.SID LIKE '$SID%');
                    SELECT COURSES.CID, COURSES.CNAME,
                    STUDENT_COURSE.SID, STUDENT_COURSE.GRADE
                    FROM COURSES, STUDENT_COURSE 
                    WHERE (COURSES.CID LIKE '$CID%'); ", $grade, $comments);

...

 

so that code should looks like:

            $numofrows = $db1->insert_student_course($SID, $CID, $grade, $comments);

 

assuming that you are getting $CID and $SID values in some place.

 

And last but not least.... in your function you have your CID and SID values transposed .. it should be:

function  insert_student_course($sid, $cid, $grade, $comments) 

and inside of it also transpose the sid, cid position in the VALUES arguments for the INSERT clause

 

hope this help

Yeah, I know I am dumb (and total amateur)...  :-[

I have made the changes you suggested (in the function too).

Now the code looks like this:

else {
            $grade = $_POST['grade'];
            $comments = $_POST['comments'];
            $db1 = new student_course();
            $db1->openDB();
            $numofrows = $db1->insert_student_course($SID, $CID, $grade, $comments);

 

My problem is that the user has to provide the SID and CID from a drop down list (on the web form).

How is that possible if not like that I was trying to do?

Which command do I have to use?

 

Many thanks for any help!

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.