2015-11-17 46 views
-1

我有問題將值保存到mysql數據庫作爲輸入值無法進入varables.Please幫助我。
我喜歡這個PHP用字符串輸入字段值保存到mySql數據庫

<td> <?php 
    $targets = new Target(); 
    $alltargets = $targets-> getBranches(); 

    foreach($alltargets as $target){ 

    echo "<tr>"; echo "<td>"; echo $target['code']; echo "</td>"; echo " <td>"; echo '<input type="text" value="" onKeyUp = "myFunction(this)" name="'. $target['code'] . '" id="'. $target['code'] . '_percentage" class="input-block- level" autocomplete="off" required/>'; echo "</td>"; echo "<td>"; echo '<input type="text" value="" name="' . $target['code'] . '" id="'.$target['code'] . '_amount" class="input-block-level" autocomplete="off" required/>'; echo " </td>"; echo "</tr>"; 
       $targets = new Target(); 
       $branch = $target['code']; 
       $percentage = $target['code'] . '_percentage; 
       $amount = $target['code'] . '_amount; 
       $targets-> saveBranchTargets($branch, $percentage, $amount); 
       $messagetoshow = "Update Successfully"; 
       } 

      ?> 
     </td> 
    </tr> 
    </tbody> 
    </table> 
<input type="submit" class="btn" value="Save"/> 
</div> 
</form> 
    </div> 
<script> 
     var percentage = 0; 
     function myFunction(element){ 
     var v1 = document.getElementById('totalamount').value; 
     var v2 = element.value; 
     var percentage = parseInt(v1) * parseInt(v2)/100; 

     var idString = element.getAttribute("id"); 
     var amountId = idString.substr(0, idString.indexOf('_')) + '_amount'; 
     console.log(amountId); 

     document.getElementById(amountId).value = percentage; 
    } 

    </script> 

<?php 

require_once代碼( '包括/ MysqliDb.php');

類目標{

public function __construct() { 

} 


    public function saveBranchTargets($branch, $percentage, $amount){ 

     $this->branch = $branch; 
     $this->percentage = $percentage; 
     $this->amount = $amount; 


     $db = new Mysqlidb(); 

     $data_branchtargets = array(

      'branch' => $this->branch, 
      'percentage' => $this->percentage, 
      'amount' => $this->amount, 


    ); 
     $this->id = $db->insert('store_targets', $data_branchtargets); 

    } 
} 

數據未插入到我的數據庫中正確,因爲他們沒有將其設置爲變量正確。

+1

什麼是Target類?不知道'saveBranchTargets()'函數是如何工作的,這個問題就不能回答。 –

回答

0

是你有沒有完成單QUOT在

$percentage = $target['code'] . '_percentage; 
$amount = $target['code'] . '_amount; 

試試這個chnaging通過

$percentage = $target['code'] . '_percentage'; 
$amount = $target['code'] . '_amount'; 

讓我知道了更多的錯誤或問題,如果你有。

+0

是的,你是對的,那是我的錯。謝謝。但我的問題是$百分比和$金額變量不會得到輸入值。 –

+0

你在腳本中遇到了問題。你沒有得到價值。? –

+0

一旦我print_r變量它不會給賦值。 –

相關問題