Jump to content

Can anyone spot why is this code not working?


layman

Recommended Posts

I inserted the whole code section. Now I don`t get it...

 

The error message I got:

INSERT INTO student_course (SID, CID, GRADE, COMMENTS) VALUES ('1', '1', '', '')

Fatal error: Using $this when not in object context in C:\xampp\htdocs\cwk\Student_Course_Insert_Tan.php on line 62

 

else {
                $sid = $_POST['sid'];
                $cid = $_POST['cid'];
                $grade = $_POST['grade'];
                $comments = $_POST['comments'];
                $db1 = new student_course();
                $db1->openDB();
                $sql = "INSERT INTO student_course (SID, CID, GRADE, COMMENTS) VALUES ('{$sid}', '{$cid}', '{$esc_grade}', '{$esc_comments}')";
                echo $sql;
                $result = mysql_query($sql, $this->conn);                                               //line 62

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

And the function looks exactly like this:

 

function insert_student_course($sid, $cid, $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 ('{$sid}', '{$cid}', '{$esc_grade}', '{$esc_comments}')";

        echo $sql;

        $result = mysql_query($sql, $this->conn);
        if (!$result) {
            die("SQL Insertion error: " . mysql_error());
        } else {
            $numofrows = mysql_affected_rows($this->conn);
            return $numofrows;
        }
    }

With this script, are you planning to open multiple databases? If not, the database connection reference ($this->con) isn't required. If the reference isn't there, PHP defaults to the last opened database.

 

For example see:

http://php.net/manual/en/function.mysql-query.php

This is the class student_course {

 

The function

function insert_student_course($sid, $cid, $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 ('{$sid}', '{$cid}', '{$esc_grade}', '{$esc_comments}')";

        echo $sql;

        $result = mysql_query($sql, $this->conn);
        if (!$result) {
            die("SQL Insertion error: " . mysql_error());
        } else {
            $numofrows = mysql_affected_rows($this->conn);
            return $numofrows;
        }
    }

 

and the code

else {
                $sid = $_POST['sid'];
                $cid = $_POST['cid'];
                $grade = $_POST['grade'];
                $comments = $_POST['comments'];
                $db1 = new student_course();
                $db1->openDB();
                $sql = "INSERT INTO student_course (SID, CID, GRADE, COMMENTS) VALUES ('{$sid}', '{$cid}', '{$esc_grade}', '{$esc_comments}')";
                echo $sql;
                $result = mysql_query($sql, $db1->conn);

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

 

cyberRobot, I really appreciate your help! :)

I haven`t a clue anymore, what`s wrong...

I took it out, and guess what happened!

 

Not fatal error anymore, just this:

INSERT INTO student_course (SID, CID, GRADE, COMMENTS) VALUES ('1', '1', '', '')Success. Number of rows affected:

 

But in the table, the row was not inserted...

This is the code. Hope you can spot the mistake!

 

<?php
        $db1 = new student_course();
        $db1->openDB();

        $sql = "SELECT sid FROM STUDENTS";
        $result1 = $db1->getResult($sql);

        $sql = "SELECT cid FROM COURSES";
        $result2 = $db1->getResult($sql);

       if (!$_POST) {
        ?>
            <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
                Choose student ID: <select name = "sid">
                <?php
                while ($row = mysql_fetch_array($result1))
                echo "<option value='{$row['sid']}'>{$row['sid']}</option>";
             
                ?>
            </select><br/>
                Choose course ID: <select name = "cid">
                <?php
                while ($row = mysql_fetch_array($result2))
                    echo "<option value ='{$row['cid']}'>{$row['cid']} </option>";
                ?>
            </select><br/>

            Enter grade:<input type="text" name="grade" /><br />
            Enter comment:<input type="text" name="comments" /><br />

            <input type="submit" value="Save" />  

        </form>

        <?php
            } else {
                $sid = $_POST['sid'];
                $cid = $_POST['cid'];
                $grade = $_POST['grade'];
                $comments = $_POST['comments'];
                $db1 = new student_course();
                $db1->openDB();
                $sql = "INSERT INTO student_course (SID, CID, GRADE, COMMENTS) VALUES ('{$sid}', '{$cid}', '{$esc_grade}', '{$esc_comments}')";
                echo $sql;
                $result = mysql_query($sql);

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

 

<?php

require("db_config.php");

class student_course {

    private $conn;

    function insert_student_course($sid, $cid, $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 ('{$sid}', '{$cid}', '{$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;
            
        }
    }

  
   function getResult($sql) {
     $getResult = mysql_query($sql, $this->conn);
     if (!getResult) {
          die("SQL Insertion error: " . mysql_error());
     } else {
          return $getResult;
     }
}
   

    function openDB() {
        $this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        if (!$this->conn) {
            die("SQL Connection error: " . mysql_error());
        }
        $db_selected = mysql_select_db(DB_NAME, $this->conn);
        if (!$db_selected) {
            die("SQL Selection error: " . mysql_error());
        }
    }

I managed to sort of insert SOME data to my studentcourse!!!

I have changed the table. The message I get back is:

 

INSERT INTO studentcourses (SID, CID, GRADE, COMMENTS) VALUES ('5', '3', '', '')Success. Number of rows affected:

 

It inserts data to the SID and CID but NOTHING to the grade & comments.

 

Any idea why? I think I`m so close! :)

Grade and comments don't work due to the incorrect variable names. ($grade vs $esc_grade)

 

...
$grade = $_POST['grade'];
$comments = $_POST['comments'];
...
$sql = "INSERT INTO student_course (SID, CID, GRADE, COMMENTS) VALUES ('{$sid}', '{$cid}', '{$esc_grade}', '{$esc_comments}')";

 

 

Of course those variable names would work if keep the two mysql_real_escape_string() statements.

 

...
$esc_grade = mysql_real_escape_string($grade, $this->conn);             
$esc_comments = mysql_real_escape_string($comments, $this->conn);       
...

Dear cyberRobot,

 

Yeah, got them mixed up, corrected it, and voila! IT IS WORKING!!!

Thank you very much for your time and answers, I really appreciate, what you did for me!

 

You know I only started to learn PHP a month ago, I know, I am an amateur..., but with your help, I have learned a lot recently!

 

Thanks again!

 

All the very best to you,

layman

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.