0

我正在尝试使用每行的不同提交按钮将不同行表的数据保存在数据库中。

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();
    }   

这是我使用 PHP 为每一行尝试的(每行具有不同的名称、id 等)但是当我按下第一行的提交按钮以将第一行的数据保存到数据库中时它成功保存但是当我按下第二个提交按钮时当时我提交的第一行的数据在数据库中获得“0”(零),第二行的同一时间数据保存在不同行的数据库中。

再次提交后,我尝试保存第一行的数据,当时第一行的数据被保存,但第二行的数据得到“0”。谁能告诉我哪里出错了?

4

2 回答 2

1

你可以这样做

<?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
 }
?>

希望这可以帮助

于 2013-03-25T09:37:35.477 回答
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();
        }   

        }



        ?>
于 2013-03-25T07:51:32.210 回答