它不会在数据库中插入值 [英] It doesn't insert the values in the database
问题描述
我下面有一个Javascript和表单代码:
I have a Javascript and form code below:
<script>
var qnum = 1;
function insertQuestion(form) {
var $tbody = $('#qandatbl > tbody');
var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
var $qid = $("<td class='qid'>" + qnum + "</td>");
$tr.append($qid);
$tbody.append($tr);
++qnum;
$("#questionNum").text(qnum);
}
</script>
表格:
<form id="QandA" action="<?php echo htmlentities($action); ?>" method="post" >
<h1><?php echo $_SESSION['id'] ?></h1>
<table id="qandatbl" align="center">
<thead>
<tr>
<th class="qid">Question No</th>
</tr>
</thead>
<tbody></tbody>
</table>
<p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" /></p>
</form>
发生的是,在php代码中,它以表格的形式在每个表格行($ qid)内添加了一个问题编号(qnum).
What happens is that in the php code it adds a question number (qnum) within each table row ($qid) in the table's in the form.
问题是,尽管我想在数据库中插入问题编号,但它根本没有这样做.如何使用INSERT VALUES在数据库中添加问题编号?数据库中每行应为1个问题编号.我还希望将SessionId($ _POST ['id'])也插入到数据库的每一行中.
The problem is though that I want to INSERT the question numbers in the database but it is not doing this at all. How can I use INSERT VALUES to add question numbers in the database? It should be 1 question number per row in the database. I also want the SessionId ($_POST['id']) to be inserted in the database as well for each row.
以下是我目前拥有的php INSERT VALUES代码:
Below is the php INSERT VALUES code I currently have:
<?php
session_start();
$username="xxx";
$password="xxx";
$database="xxx";
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$insertquestion = array();
$insertquestion[] = "' ". mysql_real_escape_string( $_POST['id'] ) . "' , ' ". mysql_real_escape_string( $_POST[$qid] ) . "'";
$questionsql = "INSERT INTO Question (SessionId, QuestionId)
VALUES (" . implode('), (', $insertquestion) . ")";
mysql_query($questionsql);
mysql_close();
?>
我已包含在var_dump和mysql_error()之后出现的通知;
I have included the notices which has appeared after var_dump and mysql_error();
注意:未定义的索引:第28行的/Mobile_app/insertQuestion.php中的ID
Notice: Undefined index: id in /Mobile_app/insertQuestion.php on line 28
注意:未定义的变量:第28行的/Mobile_app/insertQuestion.php中的qid
Notice: Undefined variable: qid in /Mobile_app/insertQuestion.php on line 28
注意:未定义的索引:在第28行的/Mobile_app/insertQuestion.php中 string(9)'',''
Notice: Undefined index: in /Mobile_app/insertQuestion.php on line 28 string(9) "' ' , ' '"
推荐答案
在继续学习数据库之前,您需要仔细检查javascript和html:目前,我看不到任何input
元素和<id
的c1>属性,所以在我看来,当您使用$_POST['id']
时,应该收到未定义变量的警告. $_POST[$qid]
也是如此.
Before getting on with the database stuff, you need to check your javascript and html carefully: At the moment I do not see any input
element with a name
attribute of id
so it seems to me you should get a warning of an undefined variable when you use $_POST['id']
. The same applies to $_POST[$qid]
.
除此之外,如果id
应该是整数,我会将其强制转换为int
而不引用它:
Apart from that, if id
is supposed to be an integer, I would cast it to int
and not quote it:
$insertquestion[] = (int) $_POST['id'] . ",". (int) $_POST[$qid];
如果它们都不应该都是整数,那么我至少要删除引号后的前导空格.
If they are not both supposed to be integers, I would at least remove the leading space after the quotes.
这篇关于它不会在数据库中插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!