DamienRoche Posted January 25, 2009 Share Posted January 25, 2009 Hi. I am completely stumped on how I should do this. I have a function like so: <?php function update_a($table, $qidH, $qidT, $ansidH, $ansidT, $atextH, $atextT, $apointH, $apointT){ include('condb.php'); $data = mysql_query("SELECT * FROM $table WHERE($qidH = '1')") or die(mysql_error()); $fetch = mysql_fetch_array($data); if($fetch > 0){mysql_query("UPDATE $table SET $atextH = '$atextT', $apointH = '$apointT'") or die("unable to update table");} } $dbfun->update_a($atable, "qid", $qid2, "ansid", $ansid3, "answer", $atext, "points", $apoints); ?> the data comes from dynamically generated forms on the same page(is this an issue?): <?php <form action="?a=edit&qid=1" method="post" name="form1"> <div class="survey-q"><a class="skiplink" name="1" id="1"></a> <span class="num">1. </span>question 1 </div> <div class="survey-a-wrap"> <div class="delbut"><!--<input type="image" src="template/images/del.jpg" name="del_ans"/>--> <a href="?a=dela&id=q1-1#1"></a></div> <input type="text" name="anstext_q1-1" value="trter" style="width:360px;"/> <select name="points_q1-1"> <option selected value='2'>2</option> <option value='0'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option></select> </div> <div class="survey-a-wrap"> <div class="delbut"><!--<input type="image" src="template/images/del.jpg" name="del_ans"/>--> <a href="?a=dela&id=q1-2#1"></a></div> <input type="text" name="anstext_q1-2" value="trter" style="width:360px;"/> <select name="points_q1-2"> <option selected value='2'>2</option> <option value='0'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option></select> </div> <div class="survey-a-wrap"> <div class="delbut"><!--<input type="image" src="template/images/del.jpg" name="del_ans"/>--> <a href="?a=dela&id=q1-3#1"></a></div> <input type="text" name="anstext_q1-3" value="trter" style="width:360px;"/> <select name="points_q1-3"> <option selected value='2'>2</option> <option value='0'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option></select> </div> <input type="hidden" name="qid" value="1"/> <div class="plus"><a href="?a=adda&id=q1-3#1">+</a></div> <div class="savequestion"><input type="submit" name="SUBQUESTION1" value="Save Question 1"/></div> </form> <form action="?a=edit&qid=2" method="post" name="form2"> <div class="survey-q"><a class="skiplink" name="2" id="2"></a> <span class="num">2. </span>question 2 </div> <div class="survey-a-wrap"> <div class="delbut"><!--<input type="image" src="template/images/del.jpg" name="del_ans"/>--> <a href="?a=dela&id=q2-1#2"></a></div> <input type="text" name="anstext_q2-1" value="trter" style="width:360px;"/> <select name="points_q2-1"> <option selected value='2'>2</option> <option value='0'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option></select> </div> <div class="survey-a-wrap"> <div class="delbut"><!--<input type="image" src="template/images/del.jpg" name="del_ans"/>--> <a href="?a=dela&id=q2-2#2"></a></div> <input type="text" name="anstext_q2-2" value="trter" style="width:360px;"/> <select name="points_q2-2"> <option selected value='2'>2</option> <option value='0'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option></select> </div> <div class="survey-a-wrap"> <div class="delbut"><!--<input type="image" src="template/images/del.jpg" name="del_ans"/>--> <a href="?a=dela&id=q2-3#2"></a></div> <input type="text" name="anstext_q2-3" value="trter" style="width:360px;"/> <select name="points_q2-3"> <option selected value='2'>2</option> <option value='0'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option></select> </div> <input type="hidden" name="qid" value="2"/> <div class="plus"><a href="?a=adda&id=q2-3#2">+</a></div> <div class="savequestion"><input type="submit" name="SUBQUESTION2" value="Save Question 2"/></div> </form> ?> Here is the while loop that submits the data(messy, I know): <?php $qid2 = $_POST['qid']; $ansid3=1; $entries=0; $loop3=0; while($loop3 <=10){ if(!empty($_POST["anstext_q".$qid2."-".$loop3])){$entries++;}$loop3++;} echo $entries; while($ansid3 <= $entries){ $atextpost = "anstext_q".$qid2."-".$ansid3; $apointspost = "points_q".$qid2."-".$ansid3; $atext = $_POST["$atextpost"]; $apoints = $_POST["$apointspost"]; echo $atext; $dbfun->condb(); $dbfun->update_a($atable, "qid", $qid2, "ansid", $ansid3, "answer", $atext, "points", $apoints); echo $qid2; #echo $atext.",".$apoints; $ansid3++;}?> Problems: -I can only update with data in field 3(q1/q2-3) - it ignores the rest. -when I update field3 it updates every field -when I update 1 question, it updates every other Is this because of having two forms on the same page? I don't remember ever having this issue before. If so, what would be a better way to do this... namely - a save button for each question, when saved, checks if answers exist, if they do, updates them, if answer doesn't exist, create it. I understand the above code is messy, it's not best practice to employ the methods I have but at the moment I am just troubleshooting this issue. Any help is greatly appreciated and I will actually pay somebody to sort this out for me - though I'm certainly not here for that - I created this from scratch and will take it to the end. Thanks. Link to comment https://forums.phpfreaks.com/topic/142317-solved-desparately-need-some-help-please/ Share on other sites More sharing options...
redarrow Posted January 25, 2009 Share Posted January 25, 2009 when you update a database you need to use a where clause mate. Link to comment https://forums.phpfreaks.com/topic/142317-solved-desparately-need-some-help-please/#findComment-745721 Share on other sites More sharing options...
DamienRoche Posted January 25, 2009 Author Share Posted January 25, 2009 when you update a database you need to use a where clause mate. that's in the first snippet: <?php mysql_query("SELECT * FROM $table WHERE($qidH = '1')") or die(mysql_error()); ---> it was $data = mysql_query("SELECT * FROM $table WHERE $qidH = '$qidT' AND $ansidH = '$ansidT'") or die(mysql_error()); # DOESN'T WORK EITHER ?> This is what I don't understand. I am clearly saying that question id(qid) should be 1, so why is it even updating question 2. I have a feeling it is the forms, in that it is submitting both forms, one after the other. Thanks for looking any way. Link to comment https://forums.phpfreaks.com/topic/142317-solved-desparately-need-some-help-please/#findComment-745724 Share on other sites More sharing options...
revraz Posted January 25, 2009 Share Posted January 25, 2009 A SELECT statement is not a UPDATE, he was referring to this: mysql_query("UPDATE $table SET $atextH = '$atextT', $apointH = '$apointT'") or die("unable to update table"); That will update all the rows since you don't specify one. Link to comment https://forums.phpfreaks.com/topic/142317-solved-desparately-need-some-help-please/#findComment-745737 Share on other sites More sharing options...
DamienRoche Posted January 25, 2009 Author Share Posted January 25, 2009 I'm an idiot. I didn't even think of that, I figured because I had selected it, it would amend that particular row. Forgot how specific I have to be. Just tested it and, obviously, is now working fine. I hope you understand, just sometimes I stare at it so long it loses all meaning. Was hoping I'd got to grips with this basic stuff. Thank you very much! Link to comment https://forums.phpfreaks.com/topic/142317-solved-desparately-need-some-help-please/#findComment-745759 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.