mmarif4u Posted May 4, 2007 Share Posted May 4, 2007 Hi guys, The problem which now i want to write is not big but my head around it not working. I have a page where i pullout some data from table in the form of dropdown then giving the user option to change it and save it. But the problem is that it did not saving the data to db, i am confused bcoz evry thing seems ok to me. Here is the code: <td width="30%"> Change Subject</td> <td><b><select name="subject" class="selectBlue"> <?php $sql="select * from p_subject"; $result_sub = mysql_query($sql) or mysql_error(); while($row_sub= mysql_fetch_object($result_sub)){ $selected=""; if($row_sub->cdsubject==$cdsubject){$selected="selected";} echo "<option value='$row_sub->cdsubject' $selected>$row_sub->cdsubject</option>"; } ?></select></b></td> Here i get the value: $cdsubject1=trim($_POST['subject']); Here i update the db: $sql="Update m_question set cdsubject='$cdsubject1',tyquestion='$tyquestion', where nrquestion=$nrquestion"; All these code is on one page. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/49932-solved-dropdown-value-small-issue/ Share on other sites More sharing options...
jitesh Posted May 4, 2007 Share Posted May 4, 2007 if($row_sub->cdsubject==$cdsubject){$selected="selected";} else {$selected=""; } Quote Link to comment https://forums.phpfreaks.com/topic/49932-solved-dropdown-value-small-issue/#findComment-245047 Share on other sites More sharing options...
trq Posted May 4, 2007 Share Posted May 4, 2007 Post the actual code where you do the update. The dropdown code is irrelevant. Also this line is invalid html. echo "<option value='$row_sub->cdsubject' $selected>$row_sub->cdsubject</option>"; should be... echo "<option value='{$row_sub->cdsubject}' selected='$selected'>{$row_sub->cdsubject}</option>"; I also added the curly {} braces as they should be used with complex variables within double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/49932-solved-dropdown-value-small-issue/#findComment-245052 Share on other sites More sharing options...
mmarif4u Posted May 4, 2007 Author Share Posted May 4, 2007 The whole code is too much long but i will post some specific code regarding to my problem: <?php include('../common/adm_inc_commons.php'); // include('adm_inc_authorization.php'); // include('adm_common_functions.php'); // include('adm_inc_question.php'); // include('adm_mysql_connect.php'); //This page may access from adm_prd_qmk_action or by itselft if(isset($_POST['btnsave_x'])){ $nrquestion=trim($_POST['nrquestion']); $cdlangu=trim($_POST['cdlangu']); $cdsubject1=trim($_POST['subject']); $nrtopic=trim($_POST['nrtopic']); $cddifficulty=trim($_POST['cddifficulty']); $nrmark=trim($_POST['nrmark']); $cdalignhdr = trim($_POST['cdalignhdr']); $txheader=trim($_POST['txheader']); $tyheader=trim($_POST['tyheader']); //question and answer $tyquestion=trim($_POST['tyquestion']); $txquestion=trim($_POST['txquestion']); $tyanswer=trim($_POST['tyanswer']); $nmanswer=trim($_POST['nmanswer']); $nma=trim($_POST['nma']); $nmb=trim($_POST['nmb']); $nmc=trim($_POST['nmc']); $nmd=trim($_POST['nmd']); //header question if($tyheader=="N"){ $sql="delete from m_question_hdr where nrquestion=$nrquestion"; $result = mysql_query($sql) or mysql_error(); }else{ $sql="select cdlangu from m_question_hdr where nrquestion=$nrquestion and cdlangu=$cdlangu"; $result = mysql_query($sql) or mysql_error(); if(mysql_num_rows($result)>0){ $sql="Update m_question_hdr set txheader='$txheader' where nrquestion=$nrquestion and cdlangu=$cdlangu"; }else{ $sql="Insert into m_question_hdr(nrquestion,cdlangu,txheader) values($nrquestion,$cdlangu,'$txheader')"; } $result = mysql_query($sql) or mysql_error(); } // question $nruser_modify=$nruser_login; $dtmodify=date("Y-m-d"); $sql="Update m_question set nrmark=$nrmark,cddifficulty=$cddifficulty,nrtopic=$nrtopic,tyheader='$tyheader', cdalignhdr='$cdalignhdr',cdsubject='$cdsubject1',tyquestion='$tyquestion',tyanswer='$tyanswer',nmanswer='$nmanswer', nruser_modify=$nruser_modify,dtmodify='$dtmodify' where nrquestion=$nrquestion"; $result = mysql_query($sql) or mysql_error(); // question detail $sql="select nrquestion from m_question_dtl where nrquestion=$nrquestion and cdlangu=$cdlangu"; $result = mysql_query($sql) or mysql_error(); if(mysql_num_rows($result)>0){ $sql="Update m_question_dtl set txquestion='$txquestion',nma='$nma',nmb='$nmb',nmc='$nmc',nmd='$nmd' where nrquestion=$nrquestion and cdlangu=$cdlangu"; }else{ $sql="Insert into m_question_dtl(nrquestion,cdlangu,txquestion,nma,nmb,nmc,nmd) values($nrquestion,$cdlangu,'$txquestion','$nma','$nmb','$nmc','$nmd')"; } $result = mysql_query($sql) or mysql_error(); send_msg($cons_msg_data_is_saved,"-1"); //header("location:adm_message.php?message=Question No:$nrquestion is saved&back=-2"); } this is dropdown area: <tr> <td width="30%"> Change Subject</td> <td><b><select name="subject" class="selectBlue"> <?php $sql="select * from p_subject"; $result_sub = mysql_query($sql) or mysql_error(); while($row_sub= mysql_fetch_object($result_sub)){ if($row_sub->cdsubject==$cdsubject){$selected="selected";} else {$selected=""; } echo "<option value='$row_sub->cdsubject' $selected>$row_sub->cdsubject</option>"; } ?></select></b></td> </tr> This is the button code: <tr> <td width="65%"></td> <td width="35%"> <input type="image" onclick="set_action('btnpreview');" name="btnpreview" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('btnpreview','','images/btn_preview_mo.jpg',1);" src="images/btn_preview.jpg"> <input type="image" onclick="set_action('btnsave');" name="btnsave" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('btnsave','','images/btn_save_mo.jpg',1);" src="images/btn_save.jpg"> </td> </tr> @jitesh: i get the selected value its ok, no problem with the problem to update the table with new value. Quote Link to comment https://forums.phpfreaks.com/topic/49932-solved-dropdown-value-small-issue/#findComment-245058 Share on other sites More sharing options...
trq Posted May 4, 2007 Share Posted May 4, 2007 Change these lines.... $result = mysql_query($sql) or mysql_error(); to... $result = mysql_query($sql) or die(mysql_error()); Are you now getting any errors? What are they? Quote Link to comment https://forums.phpfreaks.com/topic/49932-solved-dropdown-value-small-issue/#findComment-245067 Share on other sites More sharing options...
mmarif4u Posted May 4, 2007 Author Share Posted May 4, 2007 This si error now: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tyheader='T', cdalignhdr='L',cdsubject='2',tyquestion='T',tyanswer='T',n' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/49932-solved-dropdown-value-small-issue/#findComment-245071 Share on other sites More sharing options...
trq Posted May 4, 2007 Share Posted May 4, 2007 You need to make sure all values are surrounded by quotes. $sql="UPDATE m_question_hdr SET txheader='$txheader' WHERE nrquestion = '$nrquestion' AND cdlangu = '$cdlangu'"; Ive also changed your SQL attributes to caps because that is the standard. Its a good habit to get in and makes your sql much easier to read. Quote Link to comment https://forums.phpfreaks.com/topic/49932-solved-dropdown-value-small-issue/#findComment-245075 Share on other sites More sharing options...
mmarif4u Posted May 4, 2007 Author Share Posted May 4, 2007 Thanks thorpe, Yes the problem was with quotes. Thanks for help. Quote Link to comment https://forums.phpfreaks.com/topic/49932-solved-dropdown-value-small-issue/#findComment-245134 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.