1

I've create database, which basically accept name and Id and answer string of length 47,and my php code will grade the incoming results against the answer key I provided and number containing the count of correct answers will stored in database. this is information of my database. database name is marking and table called 'answer', which has 5 fields as follow

1) answer_id :int , not null, auto increament.
2) name: text
3)id : text
4)answers : text
5)correct : int

my question and problem is the function is working

// setup query
$q = mysql_query("INSERT INTO `answer` VALUES 
 (NULL,'$name', '$id','$answers','$correct')");
// run query
$result = mysql_query($q);

or in another way , nothing storing in my database ???

Thanks in advance.

this is the whole program.

<?php
error_reporting(E_ALL ^ E_STRICT);
// to turn error reporting off 
error_reporting(0);

$name  =$_POST['name']; 
$id = $_POST['id']; 
$answers = $_POST['answers'];

// check the length of string
if(strlen($answers) !=10) 
{
print'your answer string must be 10';

return;

} 
mysql_connect("localhost","root",""); 
mysql_select_db("marking"); 
$name = addslashes($name); 
$id = addslashes($id); 
$answers =  addslashes($answers); 
$answer_key =  "abcfdbbjca"; 
$correct =  0; 
for($i=0;$i<strlen($answer_key);$i++) 
{

if($answer_key[$i]  == $answers[$i])

$correct++;

} 
 //  Setup query
$q = mysql_query("INSERT INTO `answer` VALUES ('$name', '$id','$answers','$correct')");
$result = mysql_query($q);
print 'Thnak you. You got' + $correct + 'of 10 answers correct'; 
?>
13
  • it looks like you are trying to store a NULL value in your answer_id which can't be null. Commented Oct 30, 2012 at 0:03
  • There are numerous reasons this may fail. First you must debug it. if (!$result) {echo mysql_error();} to verify syntax errors. If your input values are not escaped properly, quotes may break it too. Commented Oct 30, 2012 at 0:03
  • you may need to provide more info for anyone to know. But for starters you are calling mysql_query twice there. Once on a result. We would need to know the values of these vars, if you're connected, any mysql_error results, etc. Also, someone will yell at you for using mysql_functions instead of mysqli or pdo Commented Oct 30, 2012 at 0:03
  • @nathanhayfield That's OK. It's an auto increment field, assigning NULL means that it should get its automatic increment value. Commented Oct 30, 2012 at 0:04
  • @Barmar Hmm, I usually just leave it out altogether. Never tried it that way. Commented Oct 30, 2012 at 0:05

2 Answers 2

2

Try this:

// setup query
$q = "INSERT INTO `answer` (`name`, `id`, `answers`, `correct`) VALUES 
 ('$name', '$id','$answers','$correct')";

//Run Query
$result = mysql_query($q) or die(mysql_error());

Also, you should avoid using mysql_ functions as they are in the process of being deprecated. Instead, I recommend you familiarize yourself with PDO.

Also, note, the or die(mysql_error()) portion should not be used in production code, only for debugging purposes.

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

2 Comments

I like doing it this style. If you're having a problem, you can easily insert echo $q into the script to make sure it's correct.
No DC, unfortunately no error but it's always everything is compiling correctly and when i press submit it's always printing 'your answer string must be 10' even if your string is 10 and the most importantly nothing is storing in table 'answer' in my database
1

Two things.

You are actually executing the query twice. mysql_query executes the query and returns the result resource. http://php.net/manual/en/function.mysql-query.php

And also, you are quoting the int column correct in your query, as far as I know, you can't do that (I could be wrong there).

$result = mysql_query("INSERT INTO `answer` VALUES (NULL,'$name', '$id','$answers',$correct)");

EDIT: Turns out I'm actually wrong, you may disregard my answer.

4 Comments

There's no harm in quoting an int field. It will be converted automatically.
Alright, then I really am wrong there, I wasn't 100% sure about that.
I posted my whole program could anyone please check whats the problem is and pointed me where and what I need to edit to make it work.
Refer to my comment in your OP, I'm willing to help you, but we need more clarifications.

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.