0

I'm using this form and query to pull out information stored in a database, the problem that I'm having is that if the studentcode is a number it works fine, but if the code is letter it don't work and I don't know what can be causing this, any ideas?

<form action="classgrades.php?id=<?php echo $courseid ?>" method="post">
     <input type="text" name="studentCode" value="" placeholder="Student Code.." />
     <button type="submit" value="Submit">Submit</button> 
</form>

$studentcode = $_POST['studentCode'];
$query = "SELECT s.studentid, s.assignmentid, s.studentpoints,
                 a.assignmentid, a.assignmentname, a.assignmentpoints
          FROM studentgrades AS s, assignments AS a  
          WHERE a.assignmentid = s.assignmentid
          AND s.courseid = $courseid
          AND s.studentid = (SELECT studentid
                             FROM students
                             WHERE studentcode = '$studentcode')";

Database

CREATE TABLE IF NOT EXISTS `students` (
  `studentid` int(11) NOT NULL AUTO_INCREMENT,
  `fname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `lname` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  `studentcode` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
  `courseid` int(11) NOT NULL,
  PRIMARY KEY (`studentid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=21

CREATE TABLE IF NOT EXISTS `assignments` (
  `assignmentid` int(11) NOT NULL AUTO_INCREMENT,
  `assignmentname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `courseid` int(11) NOT NULL,
  PRIMARY KEY (`assignmentid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=27 ;

Example of data record Students table

studentid   - 19;
fname       - lizt;
lname       - tisz;
studentcode - cd;
courseid    - 22;

Assignemt table

assignmentid   - 3;
assignmentname - Hello;
courseid       - 22;
7
  • What is the datatype of studentcode and studentid? Commented May 24, 2014 at 4:27
  • @Jenz Type - varchar, collation - utf8-unicode-ci Commented May 24, 2014 at 4:28
  • Are you sure you have records corresponding to the query where studentcode contains alphabets? Commented May 24, 2014 at 4:30
  • Can you create tables in sqlfiddle.com Commented May 24, 2014 at 4:31
  • @Jenz I don't know how to use sqlfiddle but I include my database sql in the question Commented May 24, 2014 at 4:34

2 Answers 2

1

Please check that collation of both the tables / column are same. Sometimes numbers have same values for different collation, but they differ in alphabets.

Sign up to request clarification or add additional context in comments.

5 Comments

what do you mean by collation on both tables, sorry im new doing this.
Collation means "set of rules for comparing data / records in the table". I can see you have same collation for the tables. I think you have to check with records.
so to compare records they need to have different collation?
what I don't understand is why with numbers works but with letters don't
No, they have to be of same collation to work properly (which is what i know). As your columns collation are same, so this reason is out of scope. May be you should try with executing single queries for the tables to find solution instead of going with join queries.
1

whenever you are executing any query in php using mysql_query($query);

you should also include or print(mysql_error()) so that if the query will not execute it will give you an error and it would be easy for you to debug the code.

so, your query_execute statement would be like:

mysql_query($query) or print(mysql_error())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.