簡體   English   中英

一鍵保存如何在php / mysql中插入數據?

[英]How to insert data in php/mysql in one save click?

我有帶player_name,runs,sixes,match_no,五十,數百,point_scored的表player_points。

我在上面的字段中創建表格,在其中插入奔跑次數和6,並使用奔跑次數和6來計算五十,數百和point_scored,在3六之后每增加6得分。 它的工作正常,但當我插入運行和六點並保存時,它僅以五十,一百和點計分的文本框顯示計算,而不保存到數據庫表中。 在我第二次保存它之后,將其保存到數據庫中。 我的代碼如下:

<tr><td colspan="9"><hr style="border:1px #999 dashed;"></td></tr>
 <tr>
  <th>Player</th>
  <th>Status</th>
  <th>Runs</th>
  <th>6's</th>
  <th>50's</th>
  <th>100's</th>
  <th>TP</th>
  <th>Action</th>
 </tr>
 <tr><td colspan="9"><hr style="border:1px #999 dashed;"></td></tr>

<?php   
        $classObj->getPlayerPointTable();
        //$matchid = $_GET['mid'];
        $condpl = "player_name='".$player1."' AND match_no = '".$matchid."'";                           
        $resultpl = $classObj->selectSql($condpl);
        //$recordcount = $classObj->getAffectedRows($result);
        $rowpl = $classObj->fetchData($resultpl);

        foreach($rowpl as $rowspl)
        {
          $Status1 = $rowspl['status'];
          $runs1 = $rowspl['run_scored'];
          $sixes1 = $rowspl['sixs'];
          $fifty1 = $rowspl['fifty'];
          $hundred1 = $rowspl['hundred'];
          $total_score1 = $rowspl['Total_Score'];
          $total_points1 = $rowspl['point_scored'];
          $firstPoint1 = $rowspl['FirstPoint'];
          $allocation1 = $rowspl['allocation'];
        }
?>

<?php 
$bonus1 = 10;
$bonus2 = 20;
$bonus3 = 25;
$bonus4 = 50;
/*condition for fifty 0 & 1 start */
if ($runs1 >= 50 && $runs1 <= 99 || $runs1 >= 150 && $runs1 <= 199 || $runs1 >= 250 && $runs1 <= 299) {
    $fifty1 = 1;
} else {
    $fifty1 = 0;
}

if ($runs1 >= 50 && $runs1 <= 499) {
    $tfif1 = $runs1 + $bonus3;
} else {
    $tpor1 = $runs1;
}
/*condition for fifty 0 & 1 end */
/*condition for hundred 0 & 1 start */
if ($runs1 >= 100 && $runs1 <= 199) {
    $hundred1 = 1;
} 
else if ($runs1 >= 200 && $runs1 <= 299) {
    $hundred1 = 2;
} 
else {
    $hundred1 = 0;
}
/*condition for hundred 0 & 1 end */


/*Condition start for Six excess to 3 sixes*/
$i1 = $sixes1 - 3;
if($i1 >= 1 && $i1 <= 500)
{
    $tsix1 = $i1 * $bonus1;
}
else
{
    $tsix1 == 0;
}
/*Condition start for Six excess to 3 sixes*/

if($fifty1 == 1 && $hundred1 >= 1)
{
    $tsixhnrd1 += $bonus4;
}
/*condition for hundred 0 & 1 end */

if ($runs1 >= 100 && $runs1 <= 500) {
    $trun1 += $bonus3;
}
    $total_points1 = $tfif1+$tsix1+$tsixhnrd1+$trun1+$tpor1;
    $total_score1 = $runs1;    

/*veriable for Balling team database insertions end*/
?>

 <form enctype="multipart/form-data" method="post" name="formA1">
 <tr>
  <td><div style="width:auto;padding-left:7px;font-size:17px;"><?php echo $player1;?></div>
   <input type="hidden" name="player1" id="player1" value="<?php echo $player1;?>" />
   <input type="hidden" name="matchid" id="matchid" value="<?php echo $matchid;?>" />
  </td>
  <?php 
    if($Status1=='P'){
  ?>
  <td align="center">   
   <select id='status1' name='status1' onChange="dis_ableA1(this)">
     <option value='NP'>NP</option>
     <option value='P' selected>P</option>
     <option value='O'>Out</option>
   </select>
  </td>
  <td align="center">
   <input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:50px;height:25px;text-align:right;background:#f5c0c0;" name="runs1" id="runs1" value="<?php echo $runs1;?>" size="3" maxlength="3" />
  </td>
  <td align="center">
   <input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:30px;height:25px;text-align:right;background:#f5c0c0;" name="sixes1" id="sixes1" value="<?php echo $sixes1;?>" size="2" maxlength="2" />
  </td>
  <?php 
    } else if($Status1=='O'){
  ?>
  <td align="center">   
   <select id='status1' style="width:50px;height:25px;" name='status1' onChange="dis_ableA1(this)">
     <option value='NP'>NP</option>
     <option value='P'>P</option>
     <option value='O' selected>Out</option>
   </select>
  </td>
  <td align="center">
   <input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:50px;height:25px;text-align:right;background:#ccc;" disabled name="runs1" id="runs1" value="<?php echo $runs1;?>" size="3" maxlength="3" />
  </td>
  <td align="center">
   <input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:30px;height:25px;text-align:right;background:#CCC;" disabled name="sixes1" id="sixes1" value="<?php echo $sixes1;?>" size="2" maxlength="2" />
  </td>
  <?php } else { ?>
  <td align="center"> 
   <select id='status1' style="width:50px;height:25px;" name='status1' onChange="dis_ableA1(this)" tabindex="1">
     <option value='NP'>NP</option>
     <option value='P'>P</option>
     <option value='O'>Out</option>
   </select>
  </td>
  <td align="center">
   <input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:50px;height:25px;text-align:right;" name="runs1" disabled id="runs1" value="<?php echo $runs1;?>" size="3" maxlength="3" />
  </td>
  <td align="center">
   <input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:30px;height:25px;text-align:right;" name="sixes1" disabled id="sixes1" value="<?php echo $sixes1;?>" size="2" maxlength="2" />
  </td>
  <?php }?>
  <td align="center">
   <label style="width:30px;height:25px;text-align:right;"><?php echo $fifty1;?></label>
   <input style="width:30px;height:25px;text-align:right;" type="hidden" id="fifty1" name="fifty1" value="<?php echo $fifty1;?>" size='2' maxlength='2' />
  </td>
  <td align="center">
   <label style="width:30px;height:25px;text-align:right;"><?php echo $hundred1;?></label>
   <input style="width:30px;height:25px;text-align:right;" type="hidden" id="hundred1" name="hundred1" value="<?php echo $hundred1;?>" size='2' maxlength='2' />
  </td>

  <td align="center">
   <label style="width:30px;height:25px;text-align:right;"><?php echo $total_points1;?></label>
   <input style="width:30px;height:25px;text-align:right;" type="hidden" id="total_points1" name="total_points1" value="<?php echo $total_points1;?>" size='5' maxlength='5' />
   <input type="hidden" name="allocation1" id="allocation1"  value="pending" />
  </td>
  <?php 
    if($Status1=='P'){
  ?>
  <td align="center">
   <input type="submit" name="submitA1" id="submitA1" value="Save" />
  </td>
  <?php } else { ?>
  <td align="center">
   <input type="submit" disabled="disabled" name="submitA1" id="submitA1" value="Save" />
  </td>
  <?php } ?>
 </tr>
 </form>
 <tr><td style="height:5px"></td></tr>
<!-- Insert Code Starts Submit1-->
<?php 
 if(isset($_POST['submitA1']))
 {
    $classObj->getPlayerPointTable();
    $matchid = $_GET['mid'];
    $cond = "player_name='".$player1."' AND match_no = '".$matchid."'";                         
    $result = $classObj->selectSql($cond);
    $count = $classObj->getAffectedRows($result);
    //echo $count;
    if($count > 0)
    {
        $player1up['status'] = $postedData['status1'];
        //$player1up['player_name'] = $postedData['player1'];
        $player1up['run_scored'] = $postedData['runs1'];
        $player1up['sixs'] = $postedData['sixes1'];
        $player1up['fifty'] = $postedData['fifty1'];
        $player1up['hundred'] = $postedData['hundred1'];
        $player1up['Total_Score'] = $postedData['total_score1'];
        $player1up['point_scored'] = $postedData['total_points1'];
        $player1up['allocation'] = $postedData['allocation1'];
        $player1up['FirstPoint'] = $postedData['total_points1'];
        $classObj->getPlayerPointTable();
        $classObj->userPostedData = $player1up;
        $cond = "match_no= '".$matchid."' AND player_name='".$player1."'";
        $insert_query = $classObj->updateData($cond);

          echo "<script> alert ('Player 1 update successfully.')</script>";
          echo "<script>window.location='FirstIn.php?mid=$matchid'</script>";
    }
    else if($count <= 0)
    { 
        $player['player_name'] = $postedData['player1'];
        $player['match_no'] = $postedData['matchid'];
        $player['status'] = $postedData['status1'];
        $player['run_scored'] = $postedData['runs1'];
        $player['sixs'] = $postedData['sixes1'];
        $player['fifty'] = $postedData['fifty1'];
        $player['hundred'] = $postedData['hundred1'];
        $player['Total_Score'] = $postedData['total_score1'];
        $player['point_scored'] = $postedData['total_points1'];
        $player['FirstPoint'] = $postedData['total_points1'];

        $classObj->userPostedData = $player;
        $insert_query = $classObj->insertData();

            if($insert_query == 'success')
            {
            echo "<script> alert('Player 1 score added.')</script>";
            echo "<script>window.location='score.php?mid=$matchid'</script>";
            }
    }
    else
    {
    echo "<script> alert ('Error Occured')</script>";
    }

 }  
?>

假設您的類的getAffectedRows()方法確實從最后一條語句中獲取了受影響的行,則必須注意,SELECT並未對此進行設置,它將為零。

如果您想知道選擇了多少行,請使用mysqli_num_rows之類的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM