mysql query insert,mysql_query(INSERT ...)函数在我的代码中不工作

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.

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

{

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

?>

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值