Jump to content

Can anyone spot why is this function not working?


layman

Recommended Posts

Hello Everyone :)

 

I`m new to PHP, therefore I would really appreciate any help with this code.

 

The error message I get:

SQL 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 'where cid=B' at line 1

 

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

function update_student_course($sid, $cid, $grade, $comments) {
        $esc_sid = mysql_real_escape_string($sid, $this->conn);
        $esc_cid = mysql_real_escape_string($cid, $this->conn);
        $esc_grade = mysql_real_escape_string($grade, $this->conn);
        $esc_comments = mysql_real_escape_string($comments, $this->conn);
        
        $sql = "UPDATE studentcourses set grade='{$esc_grade}', comments='{$esc_comments}' where sid={$esc_sid} and cid={$esc_cid}";
        $result = mysql_query($sql, $this->conn);
        if (!$result)
            die("SQL Error: " . mysql_error());
        else {
            $numofrows = mysql_affected_rows($this->conn);
            return $numofrows;
        }
    }

 

And this would be the code:

 

<?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) { //page loads for the first time
        ?>

        <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 to update: <input type="text" name="cid" /><br />
            Enter new comment: <input type="text" name="cname" /><br />

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

            </form>
<?php
        } //end if
        else {
            $sid = $_POST['sid'];
            $cid = $_POST['cid'];
            $grade = $_POST['grade'];
            $comment = $_POST['comment'];
            $db1 = new student_course();
            $db1->openDB();
            $numofrows = $db1->update_student_course($sid, $cid, $grade, $comment);
            echo "Success. Number of rows affected:
            <strong>{$numofrows}<strong>";
            $db1->closeDB();
        }
         
?>

 

Any idea, what am I doing wrong???

Server version: 5.1.41

 

Thank you very much in advance, for taking the time and reading it!!!

layman

 

Link to comment
Share on other sites

What if it does not contains the values?

 

I got this back:

UPDATE studentcourses set grade='', comments='' where sid='' and cid=''Success. Number of rows affected:

 

Could you explain me in simple terms what am I doing wrong please?

Link to comment
Share on other sites

You might want to look at all the name= attributes in your form and make sure they are what you want and that your $_POST[  ] variable names match. You have at least two form field with the same name (the last one will 'win') and at least two of your $_POST[  ] variable names don't exist.

Link to comment
Share on other sites

I have corrected the name=attributes. Thanks!

 

at least two of your $_POST[  ] variable names don't exist.

I have changed the comment to comments. But I can`t find the other one. Could you give me some clues please?

 

You have at least two form field with the same name (the last one will 'win')

What do you mean by that, which ones are the same?

 

 

 

Link to comment
Share on other sites

Thanks. I just corrected them.

 

I do not understand why I am not able to update any data via this form.

 

I made all the corrections suggested here.

 

The function looks like now this:

function update_student_course($sid, $cid, $grade, $comments) {
        $esc_sid = mysql_real_escape_string($sid, $this->conn);
        $esc_cid = mysql_real_escape_string($cid, $this->conn);
        $esc_grade = mysql_real_escape_string($grade, $this->conn);
        $esc_comments = mysql_real_escape_string($comments, $this->conn);
        
        $sql = "UPDATE studentcourses set grade='{$esc_grade}', comments='{$esc_comments}' where sid='{$esc_sid}' and cid='{$esc_cid}'";
        $result = mysql_query($sql, $this->conn);
        if (!$result)
            die("SQL Error: " . mysql_error());
        else {
            $numofrows = mysql_affected_rows($this->conn);
            return $numofrows;
        }
    }

 

And the code after all the corrections looks like this:

<?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) { //page loads for the first time
        ?>

        <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 to update: <input type="text" name="grade" /><br />
            Enter new comment: <input type="text" name="comments" /><br />

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

            </form>
<?php
        } //end if
        else {
            $sid = $_POST['sid'];
            $cid = $_POST['cid'];
            $grade = $_POST['grade'];
            $comments = $_POST['comments'];
            $db1 = new student_course();
            $db1->openDB();
            //$numofrows = $db1->update_student_course($sid, $cid, $grade, $comment);

            $sql = "UPDATE studentcourses set grade='{$esc_grade}', comments='{$esc_comments}' where sid='{$esc_sid}' and cid='{$esc_cid}'";
            echo $sql;
            $result = mysql_query($sql);

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

 

Could anyone explain the problem in very simple terms, so I`ll understand it. Thank you.

 

Link to comment
Share on other sites

This was the output:

 

Debugging output:

Array

(

    [sid] => 4

    [cid] => 10

    [grade] => B

    [comments] => Comment

)

End debuggingUPDATE studentcourses set grade='', comments='' where sid='' and cid=''Success. Number of rows affected:

 

Do you know what does this mean?

I have sid 4 in the student table so there is a record which should have been updated.

Link to comment
Share on other sites

I see the current reason why your query does not contain data values.

 

However, it will help you much much more if you set error_reporting to E_ALL and display_errors to ON so that all the php detected errors will be reported and displayed. You will save a TON of time.

Link to comment
Share on other sites

I got the line, where the error is, but I can not see what is wrong here.

 

$sql = "UPDATE studentcourses set grade='{$esc_grade}', comments='{$esc_comments}' where sid='{$esc_sid}' and cid='{$esc_cid}'";

 

And also here:

echo "Success. Number of rows affected: 
                <strong>{$numofrows}</strong>";  //in this row

 

Could anyone point the it out for me what am I doing wrong? Thank you.

Link to comment
Share on other sites

Notice: Use of undefined constant getResult - assumed 'getResult' in C:\xampp\htdocs\cwk\Class_Student_Course_Proba.php on line 29

 

Notice: Use of undefined constant getResult - assumed 'getResult' in C:\xampp\htdocs\cwk\Class_Student_Course_Proba.php on line 29

 

Notice: Undefined variable: esc_grade in C:\xampp\htdocs\cwk\Student_Course_Update.php on line 66

 

Notice: Undefined variable: esc_comments in C:\xampp\htdocs\cwk\Student_Course_Update.php on line 66

 

Notice: Undefined variable: esc_sid in C:\xampp\htdocs\cwk\Student_Course_Update.php on line 66

 

Notice: Undefined variable: esc_cid in C:\xampp\htdocs\cwk\Student_Course_Update.php on line 66

UPDATE studentcourses set grade='', comments='' where sid='' and cid=''

Notice: Undefined variable: numofrows in C:\xampp\htdocs\cwk\Student_Course_Update.php on line 71

Success. Number of rows affected:

 

I have defined the constant on line 29.

Line 66 is:

$sql = "UPDATE studentcourses set grade='{$esc_grade}', comments='{$esc_comments}' where sid='{$esc_sid}' and cid='{$esc_cid}'";

 

Line 71 is:

echo "Success. Number of rows affected: 
                <strong>{$numofrows}</strong>";  //in this row

Link to comment
Share on other sites

If you are getting four different undefined variable messages for one line of code, wouldn't it be a good idea to look where those variable names are supposed to be getting defined at in your code and try to find out why the variable names you are using don't exist?

 

And why aren't you calling your update_student_course() function? You apparently copied the query statement being used inside that function and pasted it into your main code without accounting for the different variable names you were using inside the function definition (see the problem about having four undefined variable names above.)

Link to comment
Share on other sites

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.