2016-12-07 202 views
2

任何人都可以幫助我理解爲什麼此更新查詢不更新數據庫中的字段。更新查詢不起作用PHP MySQL

<?php 
    $id= $_GET['id']; 

       $query1 = mysql_query("SELECT * FROM invoice WHERE id=$id limit 1"); 

     ?> 
     <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
     <div class="card"> 
      <div class="card-header"> 
       <strong>Create Tax</strong> 
      </div> <?php 

      while($query2=mysql_fetch_array($query1)){ 
      // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 

       ?> 
      <div class="card-block"> 
       <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Coustomer Name</label> 
        <input type="text" class="form-control" id="company" name="cname" value="<?php echo $query2['CustomerName']?>" placeholder="Coustomer Name"> 
        </div> 
       </div> 
       <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Coustomer Mobile </label> 
        <input type="text" class="form-control" id="company" name="mobile" value="<?php echo $query2['CustomerMobile']?>" placeholder="Coustomer Mobile"> 
        </div> 
       </div> 
       <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Email</label> 
        <input type="text" class="form-control" id="company" name="email" value="<?php echo $query2['Email']?>" placeholder="Email"> 
        </div> 
       </div> <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Coustomer Streat </label> 
        <input type="text" class="form-control" id="company" name="streat" value="<?php echo $query2['CustomerStreat']?>" placeholder="Coustomer Streat"> 
        </div> 
       </div> <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Coustomer City</label> 
        <input type="text" class="form-control" id="company" name="city" value="<?php echo $query2['CustomerCity']?>" placeholder="Coustomer City"> 
        </div> 
       </div> <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Coustomer State</label> 
        <input type="text" class="form-control" id="company" name="state" value="<?php echo $query2['CustomerState']?>" placeholder="Coustomer State"> 
        </div> 
       </div> <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Coustomer Country</label> 
        <input type="text" class="form-control" id="company" name="country" value="<?php echo $query2['CustomerCountry']?>" placeholder="Coustomer Country"> 
        </div> 
       </div> <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Zip/Pin Code</label> 
        <input type="text" class="form-control" id="company" name="pin" value="<?php echo $query2['ZipCode']?>" placeholder="ZipCode"> 
        </div> 
       </div> 
       <?php } ?> 
       <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Tax Name </label><?php 

       $query1 = mysql_query("SELECT * FROM Tax"); 

     ?> 
        <select id="select" name="tax" class="form-control" size="1"><?php 

      while($query2=mysql_fetch_array($query1)){ 
      // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 

       ?> 
               <option value="<?php echo $query2['NameOfTax']?> , <?php echo $query2['TaxPercentage']?>"><?php echo $query2['NameOfTax']?> , <?php echo $query2['TaxPercentage']?></option> 
           <?php $taxvalue = $query2['TaxPercentage']; ?>       <?php } ?> 

              </select> 
        </div> 
       </div> <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Iteam Name </label> 
        <input type="text" class="form-control" id="company" name="item" placeholder="Iteam Name" value="<?php echo $query2['ItemName']?>"> 
        </div> 
       </div> 
       <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Iteam Price </label> 
        <input type="text" class="form-control" id="company" name="itemprice" placeholder="Iteam price" value="<?php echo $query2['ItemPrice']?>"> 
        </div> 
       </div> 
       <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Paid Ammount </label> 
        <input type="text" class="form-control" id="company" name="pammount" placeholder="Paid Ammount" value="<?php echo $query2['PaidAmmount']?>"> 
        </div> 
       </div> <div class="col-sm-6"> 
        <div class="form-group"> 
        <label for="company">Date</label> 
        <input type="text" class="form-control" id="company" name="date" placeholder="Date" value="<?php echo $query2['Date']?>"> 
        </div> 
       </div> 
       <div class="col-sm-12"> 
        <input type="submit" class="btn btn-outline-success" name="submit" value="Processe Invoice"/> 
       </div> 
      </div> 
     </div> 
    </div> 
    </div> 
    <!--/col--> 
    </form> 
    <?php 


    $cname = $_POST['cname']; 
    $mobile = $_POST['mobile']; 
    $email = $_POST['email']; 
    $streat = $_POST['streat']; 
    $city = $_POST['city']; 
    $state = $_POST['state']; 
    $country = $_POST['country']; 
    $pin = $_POST['pin']; 
    $tax = $_POST['tax']; 
    $item = $_POST['item']; 
    $itemprice = $_POST['itemprice']; 
    $percent = ($taxvalue/100) * $itemprice ; 
    $tammount = $percent + $itemprice; 
    $pammount = $_POST['pammount']; 
    $uammount = $tammount - $pammount; 
    $date = $_POST['date']; 
    $ip = $_SERVER['REMOTE_ADDR']; 
    $date1 = date('h,i,s'); 

    if(isset($_POST['submit'])) 
     { 

     $sql = "UPDATE `ero`.`invoice` SET `CustomerName` = '$cname', `CustomerMobile` = '$mobile', `Email` = '$email', `CustomerStreat` = '$streat', `CustomerCity` = '$city', `CustomerState` = '$state', `CustomerCountry` = '$country', `ZipCode` = '$pin', `TaxName` = '$tax', `ItemName` = '$item', `ItemPrice` = '$itemprice', `TotalAmmount` = '$tammount', `PaidAmmount` = '$pammount', `UnpaidAmmount` = '$uammount', `Date` = '$date', `IP` = '$ip', `DateTime` = '$date1' WHERE `invoice`.`id` = '$id'"; 

     $result = mysql_query($sql); 

     if($result) 


$url='customer.php'; 

    echo '<script>window.location = "'.$url.'";</script>'; 
    die; 
     } 

     ?> 

invoice.php

<?php 

       $query1 = mysql_query("SELECT * FROM invoice"); 

     ?> 
           <table class="table table-bordered table-striped table-condensed"> 
            <thead> 
             <tr> 
              <th>Customer Name</th> 
              <th>Mobile</th> 
              <th>Email</th> 
              <th>City</th> 
              <th>Item Name</th> 
              <th>Item Price</th> 
              <th>Tax</th> 
              <th>Total Ammount</th> 
              <th>Paid Ammount</th> 
              <th>Unpaid Ammount</th>          
              <th>Action</th> 
             </tr> 
            </thead> 
            <tbody><?php 

      while($query2=mysql_fetch_array($query1)){ 
      // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 

       ?> 

             <tr> 
              <td><?php echo $query2['CustomerName']?></td> 
              <td><?php echo $query2['CustomerMobile']?></td> 
              <td><?php echo $query2['Email']?></td> 
              <td><?php echo $query2['CustomerCity']?></td> 
              <td><?php echo $query2['ItemName']?></td> 
              <td><?php echo $query2['ItemPrice']?></td> 
              <td><?php echo $query2['TaxName']?></td> 
              <td><?php echo $query2['TotalAmmount']?></td> 
              <td><?php echo $query2['PaidAmmount']?></td> 

              <td <?php if ($query2['UnpaidAmmount'] > 1) echo 'style="background-color:#FF0000"' ?>><?php if ($query2['UnpaidAmmount'] < 1) echo "Ammount Paid"; else echo $query2['UnpaidAmmount'];?></td> 
              <td> 
<button type="button" class="btn btn-outline-danger btn-sm"><a href="DeleteInvoice.php?id='<?php echo $query2['id'] ?>'" >Delete</a> 
<button type="button" class="btn btn-outline-success btn-sm"><a href="ViewInvoice.php?id='<?php echo $query2['id'] ?>'" >View</a></button> 
<button type="button" class="btn btn-outline-success btn-sm"><a href="updatecheck.php?id='<?php echo $query2['id'] ?>'" >Update</a></button> 

</div>      </td>  </tr> <?php } ?> 
<tr> 

此腳本重定向customer.php.if我把手動ID等ID = 38,其wroking。但MySql Table.i中沒有更新。我是編程新手,請使用一些代碼示例進行解釋。

感謝

+0

評論不適用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/130001/discussion-on-question-by-utam-sharma-update-query-not-working-php-mysql)。 –

+0

標籤是與Utam相關的內容,而不是您用來構建應用程序的所有內容的大雜燴。例如。請參閱[tag:phpmyadmin]的說明。在關於CakePHP,phpmyadmin,發送電子郵件或xampp的問題中沒有任何問題。或者你的問題是缺少標籤'顯示器','鍵盤'和'椅子':)。 – AD7six

回答

-2
UPDATE table 
SET col_name = 'new value' 
WHERE condition 

這是在SQL更新語法,請嘗試刪除與雙引號中查詢``

+6

反引號表示列或數據庫,它們在OPs代碼中是有效的。 – Qirel

+0

很好,對不起,不知道 – salifkeita

+0

你永遠不會停止學習;-)訪問這個主題了解更多關於此事的信息:http://stackoverflow.com/q/11321491/4535200 – Qirel

1

替換單引號既然你已經使用了雙引號查詢,轉義變量的雙引號或使用字符串連接。

<?php 
$id = $_GET['id']; 
$cname = $_POST['cname']; 
$mobile = $_POST['mobile']; 
$email = $_POST['email']; 
$streat = $_POST['streat']; 
$city = $_POST['city']; 
$state = $_POST['state']; 
$country = $_POST['country']; 
$pin = $_POST['pin']; 
$tax = $_POST['tax']; 
$item = $_POST['item']; 
$itemprice = $_POST['itemprice']; 
$percent = ($taxvalue/100) * $itemprice; 
$tammount = $percent + $itemprice; 
$pammount = $_POST['pammount']; 
$uammount = $tammount - $pammount; 
$date = $_POST['date']; 
$ip = $_SERVER['REMOTE_ADDR']; 
$date1 = date('h,i,s'); 

if (isset($_POST['submit'])) 
    { 
    $sql = "UPDATE `ero`.`invoice` SET `CustomerName` = \"$cname\", `CustomerMobile` = \"$mobile\", `Email` = \"$email\", `CustomerStreat` = \"$streat\", `CustomerCity` = \"$city\", `CustomerState` = \"$state\", `CustomerCountry` = \"$country\", `ZipCode` = \"$pin\", `TaxName` = \"$tax\", `ItemName` = \"$item\", `ItemPrice` = \"$itemprice\", `TotalAmmount` = \"$tammount\", `PaidAmmount` = \"$pammount\", `UnpaidAmmount` = \"$uammount\", `Date` = \"$date\", `IP` = \"$ip\", `DateTime` = \"$date1\" WHERE `invoice`.`id` = \"$id\""; 
    $result = mysql_query($sql); 
    if ($result) $url = 'customer.php'; 
    echo '<script>window.location = "' . $url . '";</script>'; 
    die; 
?> 
+0

感謝你們的工作。對此,我真的非常感激 。但我有另一個問題,我的互聯網無法正常工作。如果您可以從dropbox.com/s/k928pjh9cp0oad4/ero.zip?dl=0下載這些文件,這將對我非常有幫助。並在此發佈解決方案。我會在幾個小時後來到這裏。非常感謝你@Epistemophile –

+0

添加索引/登錄頁面,以及您的保管箱 –

+0

這裏是更新的鏈接與登錄文件https://www.dropbox.com/s/k928pjh9cp0oad4/ero.zip?dl=0 @MasivuyeCokile –