2014-01-27 63 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”自动递增的关键:)