mysql_query(INSERT ...)函数在我的代码中不工作 [英] mysql_query(INSERT ...) function not working in my code
问题描述
我创建数据库,它基本上接受
长度47的名称和回应字符串,我的PHP代码将根据我提供的答案密钥对传入结果进行评分,并且包含正确答案计数的数字存储在数据库中。这是我的数据库的信息。
数据库名称标记为
,表名为answer,其中有以下5个字段:
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 ???
提前感谢。
这是整个程序。
<?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';
?>
推荐答案
尝试:
// 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());
此外,您应避免使用 mysql _
函数因为他们在被废弃的过程中。相反,我建议您熟悉 PDO 。
Also, you should avoid using mysql_
functions as they are in the process of being deprecated. Instead, I recommend you familiarize yourself with PDO.
此外,注意,或die(mysql_error())
部分不应在生产代码中使用,仅用于调试目的。
Also, note, the or die(mysql_error())
portion should not be used in production code, only for debugging purposes.
这篇关于mysql_query(INSERT ...)函数在我的代码中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!