Jump to content


Photo

Problems Inserting into DB table


  • Please log in to reply
7 replies to this topic

#1 MIR1999

MIR1999
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 14 March 2006 - 09:55 PM

Hello again,

Having trouble inserting array values into my database table. The array in question - $qid - is created from values of one table. The form is displayed and when the submit button is pressed i would like the array values to be entered into another table. Here is some of what i have, i have cut irrelevant bits out. I think the problem lies with the fact that when the form is submitted my array values are no longer available so they cannot be inserted. Any ideas?

<form name ="systemstest" action="<?php echo $_SERVER['PHP_SELF'];?>"method="post">
<div align="center"><input type="submit" value="check score" name="submitted"></div>
<?php

//Connect to db.
require_once ('../mysql_connect.php');

//Select 10 random questions
$query = "SELECT question_id, question, answer, choice1, choice2, choice3 FROM questions ORDER BY RAND() LIMIT 10";
$result = @mysql_query ($query); //Run query

if(!isset($_POST['submitted'])){ //display the form

$randomiseAnswer=rand(1,5);//used for randomising answer order

if($result) {
echo '<table align="left" cellspacing="10" cellpadding="10">';

$qid=array();
$counter = 0;

while ($row=mysql_fetch_array($result,MYSQL_NUM)){

$id = $row[0];
$qid[$counter] = $id;
$questions = $row[1];
$answer = $row[2];

if($randomiseAnswer==1){
$opt1 = $row[3];
$opt2 = $row[4];
$opt3 = $row[5];
}
elseif{......}(more choices)

//Show questions and random choices
(some html/php code)

$counter++;
}//end while
echo '</form>';//close form


}else{ //else no result
echo'<p>Test failed</p>';
}

}else{//Add question ids to database if submitted is pressed
require_once ('../mysql_connect.php');
$qu = "INSERT INTO test_score(subject_id, q1,q2,q3,q4,q5,q6,q7,q8,q9,q10) VALUES ('1','qid[0]','qid[1]','qid[2]','qid[3]','qid[4]','qid[5]','qid[6]','qid[7]','qid[8]','qid[9]')";
$res = @mysql_query ($qu);//Run the query
}
mysql_close();
?>

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 14 March 2006 - 10:12 PM

Your missing the variable delimiter ($) before your array variable:

'[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[0]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[1]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[2]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[3]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[4]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[5]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[6]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[7]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[8]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[9]'

#3 MIR1999

MIR1999
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 14 March 2006 - 10:21 PM

[!--quoteo(post=355090:date=Mar 14 2006, 10:12 PM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Mar 14 2006, 10:12 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Your missing the variable delimiter ($) before your array variable:

'[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[0]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[1]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[2]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[3]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[4]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[5]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[6]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[7]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[8]','[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]qid[9]'
[/quote]

You are correct. However, i've now added them and it makes no difference. There are still no values being added to the table.

#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 14 March 2006 - 10:43 PM

Why are you supressing errors on your query when it's giving you problems?

change it to:

$res = mysql_query($qu) or die(mysql_error());


#5 MIR1999

MIR1999
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 14 March 2006 - 10:50 PM

[!--quoteo(post=355103:date=Mar 14 2006, 10:43 PM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Mar 14 2006, 10:43 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Why are you supressing errors on your query when it's giving you problems?

change it to:

$res = mysql_query($qu) or die(mysql_error());
[/quote]

Believe me it wasn't intentional. I'm not very good at this lol. Thanks for your quick replies. The error message i get now is:

Incorrect integer value:"for column 'q1' at row 1

Any ideas?

#6 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 14 March 2006 - 11:04 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Incorrect integer value:"for column 'q1' at row 1[/quote]

My guess would be that you are trying to insert an incorrect integer value into the q1 field for your table.

Check the data type of that field...if it's supposed to be an INT, then make sure you are trying to insert an INT, if it's not supposed to be an INT, then change it.

echo out your query if needed to check that it is correct as well.

#7 MIR1999

MIR1999
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 15 March 2006 - 12:02 AM

[!--quoteo(post=355114:date=Mar 14 2006, 11:04 PM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Mar 14 2006, 11:04 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
My guess would be that you are trying to insert an incorrect integer value into the q1 field for your table.

Check the data type of that field...if it's supposed to be an INT, then make sure you are trying to insert an INT, if it's not supposed to be an INT, then change it.

echo out your query if needed to check that it is correct as well.
[/quote]

What i've done to test it is to make the initial form display happen only when the button is pressed. I changed

if(!isset($_POST['submitted'])){ //display the form

to

if(isset($_POST['submitted'])){ //display the form

so that everything happens only when the button is pressed. I also had to remove the last 'else' so that the database query ran when the button was pressed. When i run it like this it works fine so the correct information is being passed to the array and then onto my table. The problems only start to happen when the array info is passed with if(!isset($_POST['submitted'])) and the last 'else' in place. My array information is not being sent when the submit button is pressed. To get round this i even tried making the insert values like the following:

'".$_POST['qid'][0]."' with the 1 changing to a 1 and so on but again i get the

Incorrect integer value:"for column 'q1' at row 1 error message.

#8 MIR1999

MIR1999
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 15 March 2006 - 11:03 AM

Ok here's an update. After reading another topic on here about passing variables after a submit button had been pressed i decided i would try and use a session to help with my problem. At the very top of my page i put in

<?php
session_start();
?>

Then when my while loop had finished and my array had been finalised i put in

$_SESSION['questids']=$_POST['qid']

Now by my reckoning i have created a new array questids and filled it with the values of qid. Is this correct?

Then in my INSERT command held in the 'else' invoked when the button is pressed i have tried various pieces of code e.g.

}else{//Add question ids to database

require_once ('../mysql_connect.php');
$qu = "INSERT INTO test_score(subject_id, q1,q2,q3,q4,q5,q6,q7,q8,q9,q10) VALUES('1','$questids[0]','$questids[1]','$questids[2]','$questids[3]','$questids[4]','$questids[5]','$questids[6]','$questids[7]','$questids[8]','$questids[9]')";
$res = @mysql_query ($qu) or die(mysql_error());//Run the query
}
mysql_close();

I also tried this:

}else{//Add question ids to database

require_once ('../mysql_connect.php');
$qu = "INSERT INTO test_score(subject_id, q1,q2,q3,q4,q5,q6,q7,q8,q9,q10) VALUES('1','".$_SESSION['questids'][0]."','".$_SESSION['questids'][1]."','".$_SESSION['questids'][2]."','".$_SESSION['questids'][3]."','".$_SESSION['questids'][4]."','".$_SESSION['questids'][5]."','".$_SESSION['questids'][6]."','".$_SESSION['questids'][7]."','".$_SESSION['questids'][8]."','".$_SESSION['questids'][9]."')";
$res = @mysql_query ($qu) or die(mysql_error());//Run the query
}
mysql_close();

Both continue to give the error

Incorrect integer value:"for column 'q1' at row 1 error message.

I can't see where i'm going wrong. Can anyone help?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users