我有一个表单,其中的复选框将所选行发布到$ _POST ['select'] [$ i]数组中,该数组用于从db选择数据。 sql的结果用于用换行符将结果写入文本文件,但仅写入第一行,其余为空行。 请帮忙。 我的代码如下。
<?php
$rowCountd = count($_POST["select"]);
for($i=0;$i<$rowCountd;$i++) {
$rowwd=$_POST['select'][$i];
$path_stu_test = $_SERVER['USERPROFILE'].'\Documents\performance/student_test.txt';
$file_stu_test = fopen($path_stu_test, 'w');
$stmt = $db->prepare('SELECT row_no, adms_no, full_name from stu_perform where row_no= :rowwd1');
$stmt->bindParam(':rowwd1', $postrowwd1, SQLITE3_TEXT); $postrowwd1 = $rowwd;
$ret = $stmt->execute();
while($row = $ret->fetchArray(SQLITE3_ASSOC)){
$adms_not = $row['adms_no'];
$full_namet = $row['full_name'];
$file2 = "$adms_not~$full_namet\r\n";
fwrite($file_stu_test, $file2);
}
}
?>
我认为问题是我在for()循环中使用While(){}块使用fwrite的方式,因为下面的代码有效,即它显示了写入所有sql结果的结果
<?php
$rowwd = 02;
$path_stu_test = $_SERVER['USERPROFILE'].'\Documents\performance/student_test.txt';
$file_stu_test = fopen($path_stu_test, 'w');
$stmt = $db->prepare('SELECT row_no, adms_no, full_name from stu_perform where row_no= :rowwd1');
$stmt->bindParam(':rowwd1', $postrowwd1, SQLITE3_TEXT);
$postrowwd1 = $rowwd;
$ret = $stmt->execute();
while($row = $ret->fetchArray(SQLITE3_ASSOC)){
$adms_not = $row['adms_no'];
$full_namet = $row['full_name'];
$file2 = "$adms_not~$full_namet\r\n";
fwrite($file_stu_test, $file2);
}
?>
答案 0 :(得分:0)
两种可能的方法-均未经过测试。我在原始代码中注意到的一件事是创建预准备语句的方式-即在循环内部。 Prepared语句的优点和优点之一是您应该创建一次,但是要执行多次〜因此,下面的第一个示例在循环外部准备查询,并在循环内部使用每次递增的数组值使用不同的值多次执行查询。
<?php
if( !empty( $_POST['select'] ) ){
$bytes=array();
$count=count( $_POST['select'] );
$outputfile=sprintf( '%s/Documents/performance/student_test.txt', $_SERVER['USERPROFILE'] );
$sql='select `row_no`, `adms_no`, `full_name` from `stu_perform` where `row_no`=:id;';
$stmt=$db->prepare( $sql );
if( $stmt ){
$stmt->bindParam( ':id', $id, SQLITE3_TEXT );
for( $i=0; $i < $count; $i++ ){
$id = $_POST['select'][ $i ];
$result = $stmt->execute();
if( $result ){
while( $rs=$result->fetchArray( SQLITE3_ASSOC ) ){
$data = sprintf( '%s~%s', $rs['adms_no'], $rs['full_name'] );
$bytes[]=file_put_contents( $outputfile, $data . PHP_EOL, FILE_APPEND );
}
}
}
if( !empty( $bytes ) ) printf( '%d entries and %s bytes written to file',count( $bytes ), array_sum( $bytes ) );
}
}
?>
您可以在许多数据库引擎中使用in
关键字(例如select * from tbl where id in (1,2,3,99)
等)来代替执行多次,
<?php
if( !empty( $_POST['select'] ) ){
$bytes=array();
$count=count( $_POST['select'] );
$outputfile=sprintf( '%s/Documents/performance/student_test.txt', $_SERVER['USERPROFILE'] );
$sql='select `row_no`, `adms_no`, `full_name` from `stu_perform` where `row_no` in ( :ids );';
$stmt=$db->prepare( $sql );
if( $stmt ){
$stmt->bindParam( ':ids', $ids, SQLITE3_TEXT );
$ids = implode( ',', $_POST['select'] );
$result = $stmt->execute();
if( $result ){
while( $rs=$result->fetchArray( SQLITE3_ASSOC ) ){
$data = sprintf( '%s~%s', $rs['adms_no'], $rs['full_name'] );
$bytes[]=file_put_contents( $outputfile, $data . PHP_EOL, FILE_APPEND );
}
}
if( !empty( $bytes ) ) printf( '%d entries and %s bytes written to file', count( $bytes ), array_sum( $bytes ) );
}
}
?>
作为测试以上内容的快速演示,我在阅读您的评论后将以下内容放在一起。显然,这与我的系统和数据库(mysql)有关,但严格遵循上述逻辑
<?php
$field='select';
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST[ $field ] ) ){
ob_clean();
require 'pdoconn.php';
$file=__DIR__ . '/tmp-output.txt';
$bytes=array();
$sql='select * from `city` where `id`=:id';
$stmt=$db->prepare( $sql );
if( $stmt ){
$stmt->bindParam( ':id', $id, PDO::PARAM_INT );
foreach( $_POST[ $field ] as $index => $id ){
$stmt->execute();
while( $rs=$stmt->fetch( PDO::FETCH_OBJ ) ){
$bytes[]=file_put_contents( $file, sprintf( 'ID:%d, Name:%s, Country Code:%s, District: %s', $rs->ID, $rs->Name,$rs->CountryCode,$rs->District ) . PHP_EOL, FILE_APPEND );
}
}
}
}
?>
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<title>multi-checkboxes - read from db, write to text file</title>
<style>
form{width:50%;float:none;margin:5rem auto 0 auto;}
label{display:inline-block;margin:1rem auto;float:none;width:20%;text-align:center;border:1px dotted gray;box-sizing:border-box;}
input[type='submit']{width:100%;padding:1rem;}
</style>
</head>
<body>
<form method='post'>
<?php
for( $i=1; $i <= 25; $i++ ){
$checked = rand(0,1) == 1 ? 'checked' : '';
$br = $i > 0 && $i % 5==0 ? '<br />' : '';
/* assume that the valud is equal to the ID of whatever record .... */
printf( '<label>%s<input type="checkbox" name="select[]" value="%d" %s /></label>%s', sprintf( 'Item: %d', $i ), $i, $checked, $br );
}
if( !empty( $bytes ) ) printf( '<p>%d entries and %s bytes written to file</p>',count( $bytes ), array_sum( $bytes ) );
?>
<input type='submit' />
</form>
</body>
</html>
-