2014-01-27 65 views
-3

我的代碼目標是顯示錶格批處理代碼中我的文本框中批次代碼數量的計數查詢,如果我點擊按鈕它將保存到批處理代碼表...我的批次代碼字段是在文本框中的顯示數量

「身份證」,「batchcode」

我當前的代碼:

<?php 
ob_start(); 
?> 
<html> 
<head> 
<title>test</title> 
</head> 
<body> 
<?php 
include('include/connect.php'); 

$query = "SELECT DISTINCT count(batchcode) FROM batchcodes"; 
while($rows = mysql_fetch_array($query)) { 
} 
?> 
<?php 
if(isset($_POST['save'])){ 
$var = $query+1; 
    $sql = "INSERT INTO batchcodes(batchcode) VALUES ('$var')"; 
} 
?> 
<form method="post" action="index.php" > 
<input type="text" value="batch<?php echo $query; ?>" /> 

<input type="submit" name"save" /> 
</form> 
</body> 
</html> 

在我的代碼我可是從像未定義的變量查詢,並警告mysql_fetch_array錯誤患預計參數1 ...我需要你的幫助傢伙。

+0

你有*在你從中獲取結果之前執行*查詢。 –

+0

1.執行您的查詢。 2.你將在'$ rows'中得到結果。 3. ** mysql _ ***函數是[棄用](https://wiki.php.net/rfc/mysql_deprecation)。 – Rikesh

+0

請提及您需要插入文本框的值或查詢結果。查詢結果是單值還是長數組? –

回答

1

使用的mysql_query。

$query = mysql_query("SELECT DISTINCT count(batchcode) AS nb_batchcode FROM batchcode"); 
while($row= mysql_fetch_array($query)) { 
    $batchcode=$row['nb_batchcode']; 
} 

<input type="text" name="save" value="batch<?php echo $batchcode; ?>" /> 
-1

使用$row而不是$query從查詢中獲得結果。嘗試以下代碼可以幫助您。

<?php 
ob_start(); 
?> 
<html> 
<head> 
<title>test</title> 
</head> 
<body> 
<?php 
include('include/connect.php'); 

$query = "SELECT DISTINCT count(batchcode) as batchcode FROM batchcodes"; 
while($rows = mysql_fetch_array($query)) { 
} 
?> 
<?php 
if(isset($_POST['save'])){ 
    $var = $row[0] + 1; 
    $sql = "INSERT INTO batchcodes(batchcode) VALUES (". $var .")"; 
} 
?> 
<form method="post" action="index.php" > 
<input type="text" name="save" value="batch<?php echo $query; ?>" /> 

<input type="submit" 
</form> 
</body> 
</html> 
0

您應該使用的mysql_query函數來執行查詢

$query = mysql_query("SELECT DISTINCT count(batchcode) as batchcode FROM batchcodes"); 

$sql = mysql_query("INSERT INTO batchcodes(batchcode) VALUES (". $var .")"); 
0

這裏是基於我的SQL數據我怎麼做這件事是PHP

<li> 
       <label> OR #: </label> 
       <?php 
       include('php/connect-db.php'); 
       $sql = mysql_query("SELECT MAX(or_num)+1 AS inc_or FROM tbl_admission"); 
       while($row = mysql_fetch_array($sql)){ 
       $nextOR=$row['inc_or']; 
       } 
       ?> 

       <input type="text" name="asID" value="<?php echo $nextOR; ?>" disabled> 

OR_num是我的整數,在我的表「tbl_admission」自動遞增的關鍵:)