开发者

Insert Row and Update the same row In PHP / MYSQL simultaneously

开发者 https://www.devze.com 2023-03-18 18:26 出处:网络
Please help me with this: I am creating a survey facility for the administrator. The administrator is asked for the number of questions in the questionnaire. And based on this the rows are created in

Please help me with this:

I am creating a survey facility for the administrator. The administrator is asked for the number of questions in the questionnaire. And based on this the rows are created in the survey details table. In the question table the corresponding number of rows are created for the same in the question table.

Now the admin can enter the questions and answers. For this I need to insert the row in question table and update it from the informations collected from FORM using a loop. But the inserted rows are not getting updated simultaneously. Help me with this or is there any other way to do this?

This is my code: Please ignore the programming style as this is the script by a novice:

$sid = intval ($_GET['ids']);
$noq = intval ($_GET['qn']);
for($noq !=0;$noq >=1;$noq--){  
    $q = "insert into sur_ques (sur_id) values ('$sid')";
    $ex = mysql_query($q);
    $rs = mysql_affected_rows();
    if($rs ==1){ echo" Questions Rows Created Corresponding to Survey Subject";}
?>
<form name="form1" method="post" action="<?php echo($PHP_SELF); ?>">
<table width="500" border="0" cellspacing="1" cellpadding="0">
    <tr><br><b>Ques No-<?php echo"$noq";?></b></tr><br>
    <tr><textarea name = "q" rows ="10" cols = "70" wrap = "hard" ></textarea></tr><br>
    <tr><td><b>Ans 1:</b></td><td><input type="text" name="a1" size="37" /></td></tr>   
    <tr><td><b>Ans 2:</b></td><td><input type="text" name="a2" size="37"  /></td></tr>
    <tr><td><b>Ans 3:</b></td><td><input type="text" name="a3" size="37"  /></td></tr>
    <tr><td><b>Ans 4:</b></td><td><input type="text" name="a4" size="37" /></td></tr>
</table>
<input type = "submit" name="qa" Value = "Add Q&A" />
<input type ="reset" Value="Reset" />
</form> 
<?
    if ($_开发者_Go百科POST['qa']){
        $id = mysql_insert_id();
        $result = mysql_query("update ques set q_txt = '$q', ans1 = '$a1' ans2 = '$a2'  ans4 = '$a4' ans4 = '$a4' where q_id = '$id'");
        if($r = mysql_num_rows($result)){
            echo" Question and answers updated";
        }
    } else {
        break;
    }
}
?>


I dont get you .. may be you can put more info like table structure so that i can understand it well.

but one thing for sure you can not update and insert row simultaneously... you will have to insert row into database in order to update row.


I think what you are looking for is mysql_insert_id.

session_start();
$q = "INSERT INTO table (id,key1,key2) VALUES (null,'value1','value2')";
$r = mysql_query($q);
$_SESSION['rowid'] = mysql_insert_id();

then subsequent queries would do:

$sq = "UPDATE table SET key='value',secondkey='secondvalue', WHERE id=$_SESSION['rowid'] ";
$sr = mysql_query($sq);

I don't see the need for a loop when you can insert/update as many columnsas you need in one go as you can see above. Doing it like this you will never need to insert and update at the same time (which is impossible), because you simply start the record with whatever info is available, or even if no info is available, you create an empty row, store the id and then update the row as new infomration becomes available.


"In the question table the corresponding number of rows are created for the same in the question table." - did you mean questions and answers?

Well you have a form, you have two type of inputs (questions[] and answers[]), then in cycle you just insert a question and a corresponding answer text (based on input's key) and connect them (answer should have quesitonID or vise versa, depending on what you insert first and what type of multiplicity connection is between them.. i guess question can have multiple answers)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号