昨天多谢大家来帮助我解决问题。
有人说我是挤牙膏式的问问题,或者怀疑我是在做作业,其实都不是。公司老板要求一天变三变,今年经济形势不好,谁都怕开除,我本身不是做php程序方向的,只是为了保住饭碗,在这里请求帮助,以前很少来CSDN发求助帖,所以不懂规矩,也没有分,还请大家原谅。今天奉上我昨天因为回帖得到一点分,希望大家能帮我解决问题,不胜感激,我也希望能和大家在这里成为朋友。
言归正传,现在这个程序运行得很好,能够正常查询,我想给查询的每行数据前面加一个checkbox,然后将选中的数据行重新写入到一个新的数据表中,但是因为不是很了解这部分,虽然加上了checkbox,但是没有办法传递值,因为我不是特别懂,所以还请大家帮我写出比较清晰的解决步骤。可能耽误一些高手的时间,但是这小问题对我来说确实很重要,所以请大家多体谅。在这里先感谢所有给我帮助的朋友,或者回帖解答的朋友。
<?php
$search = $_GET['Town'];
$conn = mysql_connect("localhost","dirgro5_check","dirgroup178");
$maxnum = 50; //每页显示记录条数
mysql_select_db("dirgro5_dirdb", $conn);
mysql_query("SET NAMES UTF8");
$query1 = "SELECT COUNT(*) AS totalrows FROM Sheet1 Where Town like '$search%'";
$result1 = mysql_query($query1, $conn) or die(mysql_error());
$row1 = mysql_fetch_assoc($result1);
$totalRows1 = $row1['totalrows']; //数据集数据总条数
$totalpages = ceil($totalRows1/$maxnum);//计算可分页总数,ceil()为上舍函数
if(!isset($_GET['page']) || !intval($_GET['page']) || $_GET['page'] > $totalpages) $page = 1; //对3种出错进行默认处理
//在url参数page不存在时,page不为10进制数时,page大于可分页数时,默认为1
else $page = $_GET['page'];
$startnum = ($page - 1)*$maxnum; //从数据集第$startnum条开始取,注意数据集是从0开始的
echo $query = "SELECT * FROM Sheet1 Where Town like '$search%' LIMIT $startnum,$maxnum";//选择出符合要求的数据 从$startnum条数据开始,选出$maxnum行
$result = mysql_query($query, $conn) or die(mysql_error());
$row = mysql_fetch_assoc($result);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=latin1_swedish_ci">
<title>Dir Group Customer Check</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<style type="text/css">
a{text-decoration:none;}
a:hover{text-decoration:underline}
table{font-size:12px;}
.tb{background-color:#73BB95}
.tr{background-color:#FFFFFF}
</style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" class="tb">
<form method="GET" action="">Town <input name="Town" /> </form>
<tr>
<th height="24"> <div align="center"> <form name="form2" method="post" action="input.php"/>
  <input type="submit" name="submit" value=submit>
  </div></th>
<th height="24"> <div align="center">Company </div> </th>
<th height="24"> <div align="center">Salutation </div> </th>
<th height="24"> <div align="center">Forename </div> </th>
<th height="24"> <div align="center">Surname </div> </th>
<th height="24"> <div align="center">Job Title </div> </th>
<th height="24"> <div align="center">Address 1 </div> </th>
<th height="24"> <div align="center">Address 2 </div> </th>
<th height="24"> <div align="center">Address 3 </div> </th>
<th height="24"> <div align="center">Address 4 </div> </th>
<th height="24"> <div align="center">Town </div> </th>
<th height="24"> <div align="center">County </div> </th>
<th height="24"> <div align="center">Postcode </div> </th>
<th height="24"> <div align="center">Tel </div> </th>
<th height="24"> <div align="center">Email </div> </th>
<th height="24"> <div align="center">Thomson Classification </div> </th>
<th height="24"> <div align="center">SIC Code (UK 2007) </div> </th>
</tr>
<?php if($totalRows1) {//记录集不为空显示
do {
?>
<tr class="tr">
<th height="24"> <div align="center"> <input name=checkbox[] type=checkbox value="$result"> </form> </div></th>
<th height="24"> <div align="center"> <?php echo $row['Company'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Salutation'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Forename'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Surname'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Job Title'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Address 1'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Address 2'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Address 3'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Address 4'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Town'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['County'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Postcode'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Telephone No'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Email'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['Thomson Classification'];?> </div> </th>
<th height="24"> <div align="center"> <?php echo $row['SIC Code (UK 2007)'];?> </div> </th>
</tr>
<?php }while($row = mysql_fetch_assoc($result));?>
</table>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr> <form>
<td height="27"> <div align="center">
<?php
echo "Total <font color=\"#ff0000\">$totalRows1 </font>";
echo " <font color=\"#ff0000\">".$page." </font>"."/".$totalpages."Pages";
//实现 < < < 1 2 3 4 5> >> 分页链接
$pre = $page - 1;//上一页
$next = $page + 1;//下一页
$maxpages = 4;//处理分页时 < < < 1 2 3 4 > >>显示4页
$pagepre = 1;//如果当前页面是4,还要显示前$pagepre页,如 < < < 3 /4/ 5 6 > >> 把第3页显示出来
if($page != 1) { echo " <a href='".$_SERVER['PHP_SELF']."?Town=$search'> < < </a> ";
echo " <a href='".$_SERVER['PHP_SELF'].'?page='.$pre."&Town=$search'> < </a> ";}
if($maxpages>=$totalpages) //如果总记录不足以显示4页
{$pgstart = 1;$pgend = $totalpages;}//就不所以的页面打印处理
elseif(($page-$pagepre-1+$maxpages)>$totalpages)//就好像总页数是6,当前是5,则要把之前的3 4 显示出来,而不仅仅是4
{$pgstart = $totalpages - $maxpages + 1;$pgend = $totalpages;}
else{
$pgstart=(($page <=$pagepre)?1:($page-$pagepre));//当前页面是1时,只会是1 2 3 4 > >>而不会是 0 1 2 3 > >>
$pgend=(($pgstart==1)?$maxpages:($pgstart+$maxpages-1));
}
$search = urlencode($search);
for($pg=$pgstart;$pg <=$pgend;$pg++){ //跳转菜单
if($pg == $page) echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$pg&Town=$search\"> <font color=\"#ff0000\">$pg </font> </a> ";
else echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$pg&Town=$search\">$pg </a> ";
}
if($page != $totalpages)
{echo " <a href='".$_SERVER['PHP_SELF'].'?page='.$next."&Town=$search'>> </a> ";}
echo " <a href='".$_SERVER['PHP_SELF'].'?page='.$totalpages."&Town=$search'>>> </a> ";
?>
<select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
<option value="">Select </option>
<?php for($pg1=1;$pg1 <=$totalpages;$pg1++) {
echo " <option value=\"".$_SERVER['PHP_SELF']."?page=$pg1&Town=$search\">".$pg1." </option>";
}?>
</select>
</td> </form>
</tr>
</table>
<?php } else {//记录集为空时显示?>
<tr class="tr">
<td height="24"> <div align="center">No Result </div> </td>
</tr>
</table>
<?php }?>
</body>
</html>
<?php
mysql_free_result($result1);
mysql_free_result($result);
?> 

解决方案 »

  1.   

    楼主的程序好像没有处理checkbox的代码???是吗?
      

  2.   

    楼主的checkbox所在表单是POST的,楼主还要注意:
    <input name=checkbox[] type=checkbox value="$result">
    这里的代码name值最好用引号括住,即<input name="checkbox[]" type=checkbox value="$result">
      

  3.   

    继续二楼的讲话:
    楼主提交后,可以使用循环读取checkbox值:
    <?php
    foreach($_POST['checkbox'] as $key=>$value)
    {
    //例如,输出数组:
    echo "<p>选择项:索引:".$key.";值:".$value."</p>";
    }
    ?>
    因为checkbox是一个数组,所以用数组的访问方式进行访问。
      

  4.   

    楼主以后发帖,有PHP代码,尽量用UBB的PHP代码高亮功能,否则看得很乱。
      

  5.   

    因为不懂,所以我一直都在堆砌代码,所以还请帮助我的朋友们原谅。我写了个input.php,大概的意思就是想要让之前选中的checkbox把选中的值传递到这里然后写如到新表中
    <?php 
    $dbhost='localhost'; // 数据库主机 
    $dbuser='root'; //数据库用户 
    $dbpass=''; //数据库密码 
    $dbname='sendchecking'; // 数据库名 
    $conn=mysql_connect($dbhost, $dbuser, $dbpass);// or die ("无法连接数据库主机"); 
    mysql_query("SET NAMES UTF8"); 
    mysql_select_db($dbname,$conn); 
    if ($_POST[Submit]!="") 

    mysql_query("insert into Sheet1 () value ()");
    }
    ?>
      

  6.   


    我想的方法,就是这样:
    <?php 
    $dbhost='localhost'; // 数据库主机 
    $dbuser='root'; //数据库用户 
    $dbpass=''; //数据库密码 
    $dbname='sendchecking'; // 数据库名 
    $conn=mysql_connect($dbhost, $dbuser, $dbpass);// or die ("无法连接数据库主机"); 
    $value=NULL;
    mysql_query("SET NAMES UTF8"); 
    mysql_select_db($dbname,$conn); 
    if ($_POST[Submit]!="") 

    foreach($_POST['checkbox'] as $key=>$value)
    {
    //例如,输出数组:
    //echo "<p>选择项:索引:".$key.";值:".$value."</p>";
    $value.=$value.";";//把checkbox的几个值组成一个字符串,形式:1;2;3;4这么样子,用分号隔开
    }
    mysql_query("insert into Sheet1 (checkbox_value) value (".rtrim($value,';').")");//插入数据库,把最后一个分号去掉
    }
    ?>
      

  7.   

    我觉得不仅仅是我input.php里面的问题,Checkbox写的也有问题,如果才能获得每一行的数据呢?
    <input name="checkbox[]" type="checkbox" value="<?php echo $row['Company'] ?>">
    这样写可以获得company中的数据么?
      

  8.   

    问题还是没有被解决,我现在修改过程序了,希望大家帮我修改一下,看看怎么把选中的数据行保存到新的数据库中。
    数据显示页面TEST1.PHP
    <?php
    $search = $_GET['Town'];
    $conn = mysql_connect("localhost","root","");
    $maxnum = 50; //每页显示记录条数
    mysql_select_db("db", $conn);
    mysql_query("SET NAMES UTF8");
    $query1 = "SELECT COUNT(*) AS totalrows FROM Sheet1 Where Town like '$search%'";
    $result1 = mysql_query($query1, $conn) or die(mysql_error());
    $row1 = mysql_fetch_assoc($result1);
    $totalRows1 = $row1['totalrows']; //数据集数据总条数
    $totalpages = ceil($totalRows1/$maxnum);//计算可分页总数,ceil()为上舍函数
    if(!isset($_GET['page']) || !intval($_GET['page']) || $_GET['page'] > $totalpages) $page = 1; //对3种出错进行默认处理
    //在url参数page不存在时,page不为10进制数时,page大于可分页数时,默认为1
    else $page = $_GET['page'];
    $startnum = ($page - 1)*$maxnum; //从数据集第$startnum条开始取,注意数据集是从0开始的
    echo $query = "SELECT * FROM Sheet1 Where Town like '$search%' LIMIT $startnum,$maxnum";//选择出符合要求的数据 从$startnum条数据开始,选出$maxnum行
    $result = mysql_query($query, $conn) or die(mysql_error());
    $row = mysql_fetch_assoc($result);
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=latin1_swedish_ci">
    <title>Dir Group Customer Check</title>
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_jumpMenu(targ,selObj,restore){ //v3.0
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
    }
    //-->
    </script>
    <style type="text/css">
    a{text-decoration:none;}
    a:hover{text-decoration:underline}
    table{font-size:12px;}
    .tb{background-color:#73BB95}
    .tr{background-color:#FFFFFF}
    </style>
    </head>
    <body>
    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" class="tb">
    <form method="GET" action="">Town <input name="Town" /> </form>
    <tr>
    <th height="24"> <div align="center"><form name="Submit" method="post" action="input.php"/>
      </div></th>
    <th height="24"> <div align="center">Company </div> </th>
    <th height="24"> <div align="center">Salutation </div> </th>
    <th height="24"> <div align="center">Forename </div> </th>
    <th height="24"> <div align="center">Surname </div> </th>
    <th height="24"> <div align="center">Job Title </div> </th>
    <th height="24"> <div align="center">Address 1 </div> </th>
    <th height="24"> <div align="center">Address 2 </div> </th>
    <th height="24"> <div align="center">Address 3 </div> </th>
    <th height="24"> <div align="center">Address 4 </div> </th>
    <th height="24"> <div align="center">Town </div> </th>
    <th height="24"> <div align="center">County </div> </th>
    <th height="24"> <div align="center">Postcode </div> </th>
    <th height="24"> <div align="center">Tel </div> </th>
    <th height="24"> <div align="center">Email </div> </th>
    <th height="24"> <div align="center">Thomson Classification </div> </th>
    <th height="24"> <div align="center">SIC Code (UK 2007) </div> </th>
    </tr>
    <?php if($totalRows1) {//记录集不为空显示
    do {
    ?>
    <tr class="tr">
    <th height="24"> <div align="center"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['Company'];?>"/></div></th>
    <th height="24"> <div align="center"> <?php echo $row['Company'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Salutation'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Forename'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Surname'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Job Title'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Address 1'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Address 2'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Address 3'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Address 4'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Town'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['County'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Postcode'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Telephone No'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Email'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['Thomson Classification'];?> </div> </th>
    <th height="24"> <div align="center"> <?php echo $row['SIC Code (UK 2007)'];?> </div> </th>
    </tr>
    <?php }while($row = mysql_fetch_assoc($result));?>
    <th height="24"><div align="center"> <input type="submit" name="submit" value=submit></form></div> </th>
    </table>
    <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr> <form>
    <td height="27"> <div align="center">
    <?php
    echo "Total <font color=\"#ff0000\">$totalRows1 </font>";
    echo " <font color=\"#ff0000\">".$page." </font>"."/".$totalpages."Pages";
    //实现 < < < 1 2 3 4 5> >> 分页链接
    $pre = $page - 1;//上一页
    $next = $page + 1;//下一页
    $maxpages = 4;//处理分页时 < < < 1 2 3 4 > >>显示4页
    $pagepre = 1;//如果当前页面是4,还要显示前$pagepre页,如 < < < 3 /4/ 5 6 > >> 把第3页显示出来
    if($page != 1) { echo " <a href='".$_SERVER['PHP_SELF']."?Town=$search'> < < </a> ";
    echo " <a href='".$_SERVER['PHP_SELF'].'?page='.$pre."&Town=$search'> < </a> ";}
    if($maxpages>=$totalpages) //如果总记录不足以显示4页
    {$pgstart = 1;$pgend = $totalpages;}//就不所以的页面打印处理
    elseif(($page-$pagepre-1+$maxpages)>$totalpages)//就好像总页数是6,当前是5,则要把之前的3 4 显示出来,而不仅仅是4
    {$pgstart = $totalpages - $maxpages + 1;$pgend = $totalpages;}
    else{
    $pgstart=(($page <=$pagepre)?1:($page-$pagepre));//当前页面是1时,只会是1 2 3 4 > >>而不会是 0 1 2 3 > >>
    $pgend=(($pgstart==1)?$maxpages:($pgstart+$maxpages-1));
    }
    $search = urlencode($search);
    for($pg=$pgstart;$pg <=$pgend;$pg++){ //跳转菜单
    if($pg == $page) echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$pg&Town=$search\"> <font color=\"#ff0000\">$pg </font> </a> ";
    else echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$pg&Town=$search\">$pg </a> ";
    }
    if($page != $totalpages)
    {echo " <a href='".$_SERVER['PHP_SELF'].'?page='.$next."&Town=$search'>> </a> ";}
    echo " <a href='".$_SERVER['PHP_SELF'].'?page='.$totalpages."&Town=$search'>>> </a> ";
    ?>
    <select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
    <option value="">Select </option>
    <?php for($pg1=1;$pg1 <=$totalpages;$pg1++) {
    echo " <option value=\"".$_SERVER['PHP_SELF']."?page=$pg1&Town=$search\">".$pg1." </option>";
    }?>
    </select>
    </td> </form>
    </tr>
    </table>
    <?php } else {//记录集为空时显示?>
    <tr class="tr">
    <td height="24"> <div align="center">No Result </div> </td>
    </tr>
    </table>
    <?php }?>
    </body>
    </html>
    <?php
    mysql_free_result($result1);
    mysql_free_result($result);
    ?>
    新数据库写入页面 input.PHP
    <?php 
    $dbhost='localhost'; // 数据库主机 
    $dbuser='root'; //数据库用户 
    $dbpass=''; //数据库密码 
    $dbname='Sheet2'; // 数据库名 
    $conn=mysql_connect($dbhost, $dbuser, $dbpass);// or die ("无法连接数据库主机"); 
    $value=NULL;
    mysql_query("SET NAMES UTF8"); 
    mysql_select_db($dbname,$conn); 
    if ($checkbox!=""){
    $add_num=count($checkbox);
    for($i=0;$i>$add_num;$i++){
    mysql_query("insert into Sheet1 ('Company') value ('$checkbox[$i]')");
    }
    echo("<script type='test/javascript'>alert('Add Complete');history.back();</script>");
    }else{
    echo("<script type='text/javascript'>alert('None');history.back();</script>");
    }
    ?>
    现在我发现checkbox根本没有传递数据,数组都是空的,非常郁闷,还请大家多帮助我啊!
      

  9.   

    建议LZ给表加一个ID,通过ID做索引,而且name="checkbox[]"这里根本就没有给checkbox加数组处理
    你要在test1.php里做一个再加一个for($i=1;条件你自已设;$i++)循环,把name="checkbox[]"改成name="checkbox[<?php echo $i;?>]",在input.php页里做$checkbox=$_POST['checkbox']
    $num=count($checkbox);
    for($i=1;$i<=$num;$i++)
    {
    if($checkbox[$i]!='')
    {
    $sql="select * from 表名 where id=$checkbox[$i]";
    $rount=mysql_query($sql);//找出相应ID的内容
    下面的就是添加到新的数据表的处理我就不多写了
    }
    }