1

我创建了数据库,它基本上接受名称和 ID 以及长度为 47 的答案字符串,我的 php 代码将根据我提供的答案键对传入的结果进行评分,并且包含正确答案计数的数字将存储在数据库中。这是我的数据库的信息。数据库名称是标记和名为“答案”的表,它有以下 5 个字段

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

我的问题和问题是该功能正在运行

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

或者以另一种方式,我的数据库中没有存储任何东西???

提前致谢。

这是整个程序。

<?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'; 
?>
4

2 回答 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.

于 2012-10-30T00:07:45.830 回答
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.

于 2012-10-30T00:07:27.250 回答