php mysql_query(INSERT ...) 函数在我的代码中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13131401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql_query(INSERT ...) function not working in my code
提问by Fox
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
我创建了数据库,它基本上接受名称和 ID 以及长度为 47 的答案字符串,我的 php 代码将根据我提供的答案键对传入的结果进行评分,并且包含正确答案计数的数字将存储在数据库中。这是我的数据库的信息。数据库名称为标记,表名为“答案”,其中有 5 个字段如下
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';
?>
回答by HellaMad
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.
此外,您应该避免使用mysql_正在被弃用的函数。相反,我建议您熟悉PDO。
Also, note, the or die(mysql_error())portion should not be used in production code, only for debugging purposes.
另请注意,该or die(mysql_error())部分不应用于生产代码,仅用于调试目的。
回答by wiill
Two things.
两件事情。
You are actually executing the query twice. mysql_queryexecutes the query and returns the result resource. http://php.net/manual/en/function.mysql-query.php
您实际上是在执行两次查询。mysql_query执行查询并返回结果资源。http://php.net/manual/en/function.mysql-query.php
And also, you are quoting the intcolumn correctin your query, as far as I know, you can't do that (I could be wrong there).
而且,您在查询中引用了该int列correct,据我所知,您不能这样做(我可能错了)。
$result = mysql_query("INSERT INTO `answer` VALUES (NULL,'$name', '$id','$answers',$correct)");
EDIT:Turns out I'm actually wrong, you may disregard my answer.
编辑:原来我错了,你可以无视我的回答。

