2017-05-29 50 views
0

我已經調用了來自SQL的數據,並通過編號 選擇,然後我想使用「mysqli_fetch_array」行[1]中的數據做一些計算。缺少來自SQL的可變數據

但以某種方式在第14行$ row [1]無法獲得計算的日期。

的index.php

<?php 
$con = mysqli_connect('localhost','root','','test2'); 
$query = mysqli_query($con,"SELECT * FROM count") or 
die(mysqli_error($con)); 


while($row = mysqli_fetch_array($query)) 
    echo "$row[id]. $row[quantity] <a href='edit.php?edit=$row[id]'>Draw 
Patrs<br />"; 
?> 

edit.php

<?php 

if(isset($_GET['edit'])) 
{ 
    $id = $_GET['edit']; 
    $con = mysqli_connect('localhost','root','','test2'); 
    $query = mysqli_query($con,"SELECT * FROM count WHERE id='$id'"); 
    $row = mysqli_fetch_array($query); 
} 

if(isset($_POST['drawquantity'])) 
{ 
    $drawquantity = $_POST['drawquantity']; 
    $newquantity = $row[1]-$drawquantity; 
    $id  = $_POST['id']; 
    $sql  = "UPDATE count SET quantity='$newquantity' WHERE id='$id'"; 
    $con = mysqli_connect('localhost','root','','test2'); 
    $res  = mysqli_query($con,$sql) 
           or die("Could not update".mysql_error()); 
    echo "<meta http-equiv='refresh' content='0;url=index.php'>"; 

} 

?> 
<form action="edit.php" method="POST"> 
How mant you will take away: <input type="text" name="drawquantity" value="" 
placeholder="<?php echo $row[1]; ?>"><br /> 

<input type="hidden" name="id" value="<?php echo $row[0]; ?>"> 
<input type="submit" value=" Update "/> 
</form> 
+0

最有可能當你提交'edit.php',該ID是不在那裏,因此所選的行也不存在,我的猜測是你應該在你的表單中保留相同的url – Ghost

+0

你的代碼容易受到SQL注射。請學習使用[預先準備的語句](https://www.youtube.com/watch?v=nLinqtCfhKY)。 –

回答

0
<?php 

if(isset($_GET['edit'])) 
{ 
    $id = $_GET['edit']; 
    $con = mysqli_connect('localhost','root','','test2'); 
    $query = mysqli_query($con,"SELECT * FROM count WHERE id='$id'"); 
    $row = mysqli_fetch_array($query); 
} 

if(isset($_POST['drawquantity'])) 
{ 
    $drawquantity = $_POST['drawquantity']; 
    $newquantity = $row[1]-$drawquantity; 
    $id  = $_POST['id']; 
    $sql  = "UPDATE count SET quantity='$newquantity' WHERE id='$id'"; 
    $con = mysqli_connect('localhost','root','','test2'); 
    $res  = mysqli_query($con,$sql) 
          or die("Could not update".mysql_error()); 
    echo "<meta http-equiv='refresh' content='0;url=index.php'>"; 

} 

?> 
<form action="edit.php?edit=<?=$_GET['edit']?>" method="POST"> 
How mant you will take away: <input type="text" name="drawquantity" value="" 
placeholder="<?php echo $row[1]; ?>"><br /> 

<input type="hidden" name="id" value="<?php echo $row[0]; ?>"> 
<input type="submit" value=" Update "/> 
</form> 

變化是行動

+0

謝謝Jinesh Suthar,你的代碼正在工作,非常感謝你 – NDSAC

+0

也謝謝。點擊向上箭頭,使其指示其有用。 –