layman Posted January 6, 2011 Author Share Posted January 6, 2011 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(); Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155484 Share on other sites More sharing options...
layman Posted January 6, 2011 Author Share Posted January 6, 2011 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155486 Share on other sites More sharing options...
cyberRobot Posted January 6, 2011 Share Posted January 6, 2011 Seems kind of weird that the "$this" reference stopped working after adding the echo statement. Did you change anything else? I'm just guessing, but does it work if you change the "$this->" references to "$db1->"? Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155488 Share on other sites More sharing options...
layman Posted January 6, 2011 Author Share Posted January 6, 2011 I haven`t changed anything else. I checked with $this->$db1 , but no difference, same error message. Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155491 Share on other sites More sharing options...
cyberRobot Posted January 6, 2011 Share Posted January 6, 2011 So you changed the "$this->conn" to "$db1->conn"? Is the function inside a class object? Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155496 Share on other sites More sharing options...
layman Posted January 6, 2011 Author Share Posted January 6, 2011 Yes, I have changed, its $db1->conn now. The function in inside the class student_course. Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155499 Share on other sites More sharing options...
cyberRobot Posted January 6, 2011 Share Posted January 6, 2011 The function in inside the class student_course. Hmmm...with it being in a class "$this->conn" should work. Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155507 Share on other sites More sharing options...
cyberRobot Posted January 6, 2011 Share Posted January 6, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155509 Share on other sites More sharing options...
layman Posted January 6, 2011 Author Share Posted January 6, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155510 Share on other sites More sharing options...
layman Posted January 6, 2011 Author Share Posted January 6, 2011 No, I have 1 database, so there are no multiple databases. Just checking the link you sent. Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155511 Share on other sites More sharing options...
layman Posted January 6, 2011 Author Share Posted January 6, 2011 If not, the database connection reference ($this->con) isn't required. If the reference isn't there, PHP defaults to the last opened database. Should I try to take it out then? Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155515 Share on other sites More sharing options...
cyberRobot Posted January 6, 2011 Share Posted January 6, 2011 Should I try to take it out then? I would if you don't plan to open multiple database. It's one less thing you need to type. Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155518 Share on other sites More sharing options...
layman Posted January 6, 2011 Author Share Posted January 6, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155519 Share on other sites More sharing options...
cyberRobot Posted January 6, 2011 Share Posted January 6, 2011 Could you try posting your current code again (function to insert the row and the code that calls the function)? Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155523 Share on other sites More sharing options...
layman Posted January 6, 2011 Author Share Posted January 6, 2011 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()); } } Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155537 Share on other sites More sharing options...
layman Posted January 6, 2011 Author Share Posted January 6, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155627 Share on other sites More sharing options...
cyberRobot Posted January 6, 2011 Share Posted January 6, 2011 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); ... Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155650 Share on other sites More sharing options...
layman Posted January 6, 2011 Author Share Posted January 6, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155714 Share on other sites More sharing options...
cyberRobot Posted January 6, 2011 Share Posted January 6, 2011 No problem, glad everything worked out in the end. Quote Link to comment https://forums.phpfreaks.com/topic/223480-can-anyone-spot-why-is-this-code-not-working/page/2/#findComment-1155717 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.