1

I am trying to save data of different rows of table in database using different submit button for each row.

echo '<form style="text-align:center" id="form1" name="form1" method="post" action="">';
        echo "<tr>";
        echo "<td nowrap='nowrap'><label>".$rowsum['student_name1']."</label><input type='hidden' name='studentId1' id='studentId1' value=".$rowsum['student_name1']." /></td>";
        echo "<td align='center'>
                <select id='Status1' name='Status1'><option value='NP'>Not Present</option><option value='P'>Present</option></select></td>";
        echo "<td align='center'>
                <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='mar1' type='text' id='mar1' size='5' maxlength='5' value='".$mar1."' /></td>";
        echo "<td align='center'>
                <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='hin1' type='text' id='hin1' size='2' maxlength='2' value='".$hin1."' /></td>";
        echo "<td align='center'>
                <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000; border:hidden;' type='text' id='eng1' disabled='disabled' name='eng' value='".$eng1."' size='2' maxlength='2' /></td>";
        echo "<td align='center'>
                <input style='font-weight:bold; color:#000000; background:#FFFFFF; text-align:right; border:hidden;' name='hist1' id='hist1' value='".$hist1."' disabled='disabled' size='2' maxlength='2' /></td>";
        echo "<td align='center'>
                <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000; border:hidden;' name='geo1' id='geo1' value='".$geo1."' disabled='disabled' size='5' maxlength='5' type='text' /></td>";
        echo "<td align='center'>
                <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000; border:hidden;' type='text' name='marks1' disabled='disabled' id='marks1' value='".$marks1."' size='5' maxlength='5' /></td>";
        echo "<td align='center'>
                <input type='submit' name='SaveBat1' id='SaveBat1' value='Save' />
            </td></tr>";
        echo '</form>';

    //}
    $sql_result1 = mysql_query("SELECT `student_name` FROM `bat_score_board` WHERE `student_name` =  '$student1'") or die(mysql_error());
    $sql_row1 = mysql_fetch_assoc($sql_result1);
    if(!empty($sql_row1['student_name'])) {
        //echo "Id exists - " .  $sql_row1['student_name'] . "\n";
        $sqlup1 = "update bat_score_board set student_name='$student1', status='$status1', mar='$mar1', hin='$hin1', eng='$eng1', hist='$hist1', geo='$geo1', marks='$marks1' where student_name='$student1'";
        $resultup1 = mysql_query($sqlup1);
        //print_r($resultup1); exit();
    } else {
        //echo "Id no longer exists - " . $sql_row1['student_name'] . "\n";
        $sqlBat1 = "INSERT INTO bat_score_board(student_name, status, mar, hin, eng, hist, geo, marks)VALUES('$student1', '$status1', '$mar1', '$hin1', '$eng1', '$hist1', '$geo1', '$marks1')";
        $resultBat1 = mysql_query($sqlBat1);
        //print_r($resultBat1); exit();
    }   

This is what I tried for each row using PHP (with different name, id etc for each row) but when I press the submit button of first row to save data of first row in database it saved successfully but when I press submit button of second row at that time data of first row which I submitted before get '0' (zero) in database and same time data of second row's get saved in database in different row.

After this submission again I tried to save data of first at that time data of first row get saved but data of second row get '0'. Can anybody tell me where I am going wrong?

6
  • Are you using only one form? Commented Mar 25, 2013 at 7:37
  • where do you define and set $student1 and other variables? Commented Mar 25, 2013 at 8:01
  • @Ander2 i am using separate form for every row. Commented Mar 25, 2013 at 8:16
  • @Amir i am defining all variable at the time of retrieving them from Database Commented Mar 25, 2013 at 8:22
  • @Shri Can you post the missing loop parts of your code? Is there any loop in the query part ? Commented Mar 25, 2013 at 8:53

2 Answers 2

2

You can do it this way

<?php
 if(isset($_POST['SUBMIT BUTTON 1 NAME'])) {
  // YOUR 1ST INSERT QUERY
 }
 if(isset($_POST['SUBMIT BUTTON 2 NAME'])) {
  // YOUR 2ND INSERT QUERY
 }
 if(isset($_POST['SUBMIT BUTTON 3 NAME'])) {
  // YOUR 3RD INSERT QUERY
 }
?>

Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

thanks @Roger i just tried this but now the problem is data is saving in database well but not retrieving back to page.
@Shri: Happy that I could help
1
check this out dude, 
   <?php
    class Connection {
        var $hostname, $username, $password, $database;
        function __construct() {

            //  local
            $this->hostname = "localhost"; 
            $this->username = "root"; 
            $this->password = ""; 
            $this->database = "test"; 
            $this->dbConnection(); 
        }

        public function dbConnection () {
            $connection = mysql_connect($this->hostname,$this->username,$this->password) or die ('Cannot make a connection');
            if ($connection) {
                $selectDB = mysql_select_db($this->database) or die ('Cannot select database');
            }
        }
    }
    $connect = new Connection();

    $sql = "SELECT * FROM bat_score_board";
    $row = mysql_fetch_row(mysql_query($sql));

    $cricketer_name = $row[1];
    $status = $row[2];
    $mar = $row[3];
    $hin = $row[4];
    $eng = $row[5];
    $hist = $row[6];
    $geo = $row[7];
    $marks = $row[8];



    echo '<form style="text-align:center;" id="form1" name="form1" method="post" action="">';
            echo "<tr>";
            echo "<td nowrap='nowrap'><label>".$cricketer_name."</label><input type='hidden' name='studentId1' id='studentId1' value=".$cricketer_name." /></td>";
            echo "<td align='center'>
                    <select id='Status1' name='Status1'><option value='NP'>Not Present</option><option value='P'>Present</option></select></td>";
            echo "<td align='center'>
                    <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' readonly name='mar1' type='text' id='mar1' size='5' maxlength='5' value='".$mar."' /></td>";
            echo "<td align='center'>
                    <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' readonly name='hin1' type='text' id='hin1' size='2' maxlength='2' value='".$hin."' /></td>";
            echo "<td align='center'>
                    <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000; border:hidden;' type='text' id='eng1' readonly name='eng1' value='".$eng."' size='2' maxlength='2' /></td>";
            echo "<td align='center'>
                    <input style='font-weight:bold; color:#000000; background:#FFFFFF; text-align:right; border:hidden;' name='hist1' id='hist1' value='".$hist."' readonly size='2' maxlength='2' /></td>";
            echo "<td align='center'>
                    <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000; border:hidden;' name='geo1' id='geo1' value='".$geo."' readonly size='5' maxlength='5' type='text' /></td>";
            echo "<td align='center'>
                    <input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000; border:hidden;' type='text' name='marks1' readonly  id='marks1' value='".$marks."' size='5' maxlength='5' /></td>";
            echo "<td align='center'>
                    <input type='submit' name='SaveBat1' id='SaveBat1' value='Save' />
                </td></tr>";
            echo '</form>';

        //}

        if ( isset($_POST["Status1"]) ) {

        echo "hello";
        $cricketer_name = $row[1];
        $status = $row[2];
        $mar = $row[3];
        $hin = $row[4];
        $eng = $row[5];
        $hist = $row[6];
        $geo = $row[7];
        $marks = $row[8];

        $sql_result1 = mysql_query("SELECT `cricketer_name` FROM `bat_score_board` WHERE `cricketer_name` =  '".$_POST['studentId1']."' ") or die(mysql_error());
        $sql_row1 = mysql_fetch_assoc($sql_result1);
        if(!empty($sql_row1['cricketer_name'])) {
            //echo "Id exists - " .  $sql_row1['cricketer_name'] . "\n";
            $sqlup1 = " update bat_score_board set 
                        cricketer_name= '".$_POST['studentId1']."', 
                        status='".$_POST['Status1']."', 
                        mar='".$_POST['mar1']."', 
                        hin= '".$_POST['hin1']."', 
                        eng='".$_POST['eng1']."', 
                        hist='".$_POST['hist1']."', 
                        geo='".$_POST['geo1']."', 
                        marks='".$_POST['marks1']."'            
                        where cricketer_name='".$_POST['studentId1']."' ";
            $resultup1 = mysql_query($sqlup1);
            //print_r($resultup1); exit();
        } else {
            //echo "Id no longer exists - " . $sql_row1['cricketer_name'] . "\n";
            $sqlBat1 = "INSERT INTO bat_score_board(cricketer_name, status, mar, hin, eng, hist, geo, marks)VALUES
            '(".$_POST['studentId1']."', 
            '".$_POST['Status1']."', 
            '".$_POST['mar1']."', 
            '".$_POST['hin1']."', 
            '".$_POST['eng1']."', 
            '".$_POST['hist1']."',
            '".$_POST['geo1']."', 
            '".$_POST['marks1']."')";

            $resultBat1 = mysql_query($sqlBat1);
            //print_r($resultBat1); exit();
        }   

        }



        ?>

7 Comments

i am just passing the value of label in database.
@Shri what's the condition for selecting $student1 on your studentId1 (hidden field). Provide more code..
@Shri how did you fetch the value?
require("/includes/commonClass.php"); $classObj = new commonclass; $postedData = $classObj->getRequestedData(); $setsession = $classObj->validsessionmanager(); $classObj->getTempteam(); $resultum = $classObj->selectSql($condum); $rowum = $classObj->fetchData($resultum); foreach($rowum as $rowsum) { $student1 = $rowsum['student_name1']; $student2 = $rowsum['student_name2']; $student3 = $rowsum['student_name3']; $student4 = $rowsum['student_name4']; }
i am using it by creating classObj from commonClass.php file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.