I have an application where it adds questions into question rows. I have a problem though when it comes to INSERTING VALUES in the database.
I my application if I add 2 questions then it looks like this below:
SessionId QuestionId QuestionContent
ABV 1 What is 2+2?
ABV 2 What is 3+3?
But if I add these values in the database, then it inserts it like this:
SessionId QuestionId QuestionContent
ABV 3 What is 2+2?
ABV 3 What is 3+3?
The problem is that it is INSERTING the next question number after those that have been added, in this case the number '3'. How can I add the correct question numbers like the first example in the database?
Below is the INSERT VALUES code I currently have:
$insertquestion = array();
foreach($_POST['questionText'] as $question)
{
$insertquestion[] = "' ". mysql_real_escape_string( $_SESSION['id'] ) . "' , ' ". mysql_real_escape_string( $_POST['num_questions'] ) . "', ' ". mysql_real_escape_string( $question ) . "'";
}
$questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent)
VALUES (" . implode('), (', $insertquestion) . ")";
echo($questionsql);
Below is the javascript code which adds the question numbers in the table rows in the application ( I have not included appending the sessionId and Questions in the code below)
<script>
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);
$("#num_questions").val(qnum);
</script>
<form id="QandA" action="insertQuestion.php" method="post" >
<input type="hidden" id ="num_questions" value="" name="num_questions">
<div id="detailsBlock">
<table id="question">
<tr>
<th colspan="2">
Question Number <span id="questionNum">1</span>
</th>
</tr>
</table>
</form>
If you just use DELETE * FROM tableName
or EMPTY function in phpMyAdmin
(which do DELETE * FROM tableName
) it's normal, u just have index with auto_increment
and when you delete all record in your table last auto_increment_id
is keeped.
If you TRUNCATE
this table auto_increment_id
will be returned to 1.