我有一个通过查询字符串生成的表单 在页面上,大量数据放在表单中。
我将它们放在表格中并一起更新。 现在我遇到两个问题:
以下是代码:
include_once 'header.php';
include_once 'lab-sidebar.php';
include_once 'conn.php';
include_once 'functions.php';
$data = display_conditional_data(" name LIKE '".$_REQUEST['alpha']."%' order by name asc ",'tb_test');
$sample_type = dis_all_data('tb_sample_type');
$departments = dis_all_data('tb_test_method');
$testconditions = dis_all_data('tb_test_conditions');
$display_data = display_data('id',$_SESSION['user_id'],'tb_labs');
if(isset($_POST['btnSubmit'])){
$sql = "insert into test_menu_details ( test_id ,sample_type , price, discounted_price, lab_id, dep_id, reporting_time, automatic, remarks , city,testcondition) values ";
$count_length = count($_POST['price']);
for($i = 0 ; $i< $count_length; $i++){
if($_POST['price'][$i] != ''){
$sql .= "('".$_POST['test_name'][$i]."','" .$_POST['sample_type'][$i]."','".$_POST['price'][$i]."','".$_POST['discount_price'][$i]."','".$_SESSION['user_id']."','".$_POST['departments'][$i]."','".$_POST['reporting_time'][$i]."','".$_POST['automation'][$i]."','".$_POST['reviews'][$i]."','". $display_data['city'] ."','".$_POST['testcondition'][$i]."' )";
$sql .= ',';
}
}
$sql = rtrim($sql,', ');
$sql .= ' ON DUPLICATE KEY UPDATE sample_type=VALUES(sample_type) , price = VALUES(price) , discounted_price = VALUES(discounted_price) , dep_id = VALUES(dep_id) , reporting_time = VALUES(reporting_time) , automatic = VALUES(automatic) , remarks = VALUES(remarks) , city = VALUES(city) , testcondition = VALUES(testcondition) ';
echo '$sql$sql '.$sql;
if(!mysqli_query($conn,$sql)){
// echo mysqli_errno($conn);
if(mysqli_errno($conn) == 1062){
$_SESSION['message'] = '<div class="alert alert-danger">Some values needed to be unique. Please read instructions carefully.</div>';
}
}
else{
if(mysqli_affected_rows($conn) == 1){
$conn->close();
$_SESSION['message'] = '<div class="alert alert-success">Data inserted. And data that already exists has been updated. </div>';
echo $_SESSION['message'];
//echo '<script type="text/javascript">window.location = "add-test-menu.php"</script>';
}
else{
//error
$_SESSION['message'] = '<div class="alert alert-danger">Data inserted. And data that already exists has been updated. </div>';
echo $_SESSION['message'];
}
//echo '<script>window.location = "add-test-menu.php";</script>';
}
}
?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
<?php echo $_SESSION['username']; ?>
</h1>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Test Menu</h3>
<div>
<tr>
<td colspan="3" width="33%" height="40" class="head_black"> Add New. Select Alphabet >> <a class="nlink2" href="test-with-name.php?alpha=1" class="dblinkss">1</a> |
<a class="nlink2" href="test-with-name.php?alpha=2" class="dblinkss">2</a> |
<a class="nlink2" href="test-with-name.php?alpha=5" class="dblinkss">5</a> |
<a class="nlink2" href="test-with-name.php?alpha=a" class="dblinkss">A</a> |
<a class="nlink2" href="test-with-name.php?alpha=b" class="dblinkss">B</a> |
<a class="nlink2" href="test-with-name.php?alpha=c" class="dblinkss">C</a> |
<a class="nlink2" href="test-with-name.php?alpha=d" class="dblinkss">D</a> |
<a class="nlink2" href="test-with-name.php?alpha=e" class="dblinkss">E</a> | <a class="nlink2" href="test-with-name.php?alpha=f" class="dblinkss">F</a> | <a class="nlink2" href="test-with-name.php?alpha=g" class="dblinkss">G</a> | <a class="nlink2" href="test-with-name.php?alpha=h" class="dblinkss">H</a> | <a class="nlink2" href="test-with-name.php?alpha=i" class="dblinkss">I</a> | <a class="nlink2" href="test-with-name.php?alpha=j" class="dblinkss">J</a> | <a class="nlink2" href="test-with-name.php?alpha=k" class="dblinkss">K</a> | <a class="nlink2" href="test-with-name.php?alpha=l" class="dblinkss">L</a> | <a class="nlink2" href="test-with-name.php?alpha=m" class="dblinkss">M</a> | <a class="nlink2" href="test-with-name.php?alpha=n" class="dblinkss">N</a> | <a class="nlink2" href="test-with-name.php?alpha=o" class="dblinkss">O</a> | <a class="nlink2" href="test-with-name.php?alpha=p" class="dblinkss">P</a> | <a class="nlink2" href="test-with-name.php?alpha=q" class="dblinkss">Q</a> | <a class="nlink2" href="test-with-name.php?alpha=r" class="dblinkss">R</a> | <a class="nlink2" href="test-with-name.php?alpha=s" class="dblinkss">S</a> | <a class="nlink2" href="test-with-name.php?alpha=t" class="dblinkss">T</a> | <a class="nlink2" href="test-with-name.php?alpha=u" class="dblinkss">U</a> | <a class="nlink2" href="test-with-name.php?alpha=v" class="dblinkss">V</a> | <a class="nlink2" href="test-with-name.php?alpha=w" class="dblinkss">W</a> | <a class="nlink2" href="test-with-name.php?alpha=x" class="dblinkss">X</a> | <a class="nlink2" href="test-with-name.php?alpha=y" class="dblinkss">Y</a> | <a class="nlink2" href="test-with-name.php?alpha=z" class="dblinkss">Z</a></td>
</tr>
</div>
</div>
</div>
<!-- /.box -->
</div>
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Test Menu</h3>
</div>
<form method="POST">
<table class="table table-bordered">
<tr>
<th>Test Name </th>
<th>Pre Test Information</th>
<th>Sample </th>
<th>Methodology</th>
<th>Reporting Time</th>
<th>Automation</th>
<th>Price</th>
<th>Discount Price</th>
<!-- <th>Remarks</th> -->
</tr>
<?php
foreach($data as $d){
$mtd = explode(',',$d['tb_test_method']);
$tc = explode(',',$d['tb_test_condition']);
$sty = explode(',',$d['tb_sample_type']);
?>
<tr>
<td><?php echo $d['name']; ?><br><b>Remarks</b><textarea name="reviews[]" class="form-control"></textarea></td>
<input type="hidden" name="test_name[]" value="<?php echo $d['id']; ?>">
<td>
<select name="testcondition[]" class="form-control">
<option value="">Select</option>
<?php
foreach($testconditions as $st){
?>
<option value="<?php echo $st['id']; ?>" <?php if( $st['id'] == $tc[0]){ echo ' selected'; } ?> ><?php echo $st['name']; ?></option>
<?php
}
?>
</select>
</td>
<td>
<select name="sample_type[]" class="form-control">
<option value="">Select</option>
<?php
foreach($sample_type as $st){
?>
<option value="<?php echo $st['id']; ?>" <?php if( $st['id'] == $sty[0]){ echo ' selected'; } ?> ><?php echo $st['name']; ?></option>
<?php
}
?>
</select>
</td>
<td>
<select name="departments[]" class="form-control">
<option value="">Select</option>
<?php
foreach($departments as $dep){
?>
<option value="<?php echo $dep['id']; ?>" <?php if( $dep['id'] == $mtd[0]){ echo ' selected'; } ?> ><?php echo $dep['name']; ?></option>
<?php
}
?>
</select>
</td>
<td>
<select name="reporting_time[]" class="form-control">
<option selected="" value="">--Select One--</option>
<option value="Same Day">Same Day</option>
<option value="Next Day">Next Day</option>
<option value="2 - 5 Days">2 - 5 Days</option>
<option value="5 - 10 Days">5 - 10 Days</option>
<option value="10 - 20 Days">10 - 20 Days</option>
<option value="20 - 30 Days">20 - 30 Days</option>
<option value="30 - 60 Days">30 - 60 Days</option>
<option value="N.A.">N.A.</option>
</select>
</td>
<td>
<select name="automation[]" class="form-control">
<option selected="" value="0">--Select One--</option>
<option value="Manual">Manual</option>
<option value="Semi Automatic">Semi Automatic</option>
<option value="Fully Automatic">Fully Automatic</option>
<option value="N.A.">N.A.</option>
</select>
</td>
<td><input type="text" name="price[]" class="form-control"></td>
<td><input type="text" name="discount_price[]" class="form-control"></td>
<!-- <td></td> -->
</tr>
<?php } ?>
</table>
<input type="submit" value="Add" class="btn btn-primary" name="btnSubmit">
</form>
</div>
<!-- /.box -->
</div>
<!-- /.row -->
</section>
<!-- /.content -->
</div>
<?php
include_once 'footer.php';
?>
答案 0 :(得分:0)
您在表单中遗漏了action
属性。
你的表格标签应该是这样的
<form method="POST" action="yourTargetFile.php">
你正在为单个值传递数组。
见这里
<input type="hidden" name="test_name[]" value="<?php echo $d['id']; ?>">
和
<select name="testcondition[]" class="form-control">
如果您没有多个值,为什么要尝试传递数组? 你需要像这样传递。并改变所有不是多个值的字段。
<input type="hidden" name="test_name" value="<?php echo $d['id']; ?>">
插入没有for
和foreach
循环的数据。