在文本框中显示计数

时间:2014-01-27 08:50:40

标签: php html mysql

我的代码的目标是显示表格批处理代码中批处理代码数量的计数查询到我的文本框中,如果我点击按钮它将保存到批处理代码表...我的批处理代码字段是

的 'id', '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 ......我需要你的帮助。

4 个答案:

答案 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 :(得分:0)

您应该使用mysql_query函数来执行查询

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

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

答案 2 :(得分: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”中的整数和自动递增的键:)

答案 3 :(得分:-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>