У меня есть форма, которая создается через строку запроса а на странице в форму помещается много данных.
Я их оформляю и все вместе обновляю. Теперь у меня две проблемы:
Вот код:
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';
?>
Это не то, как делать запросы к базе данных. Посмотрите подготовленные заявления и узнайте, как их использовать!






вы пропустили атрибут 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.
данные несколько. поэтому он будет использовать array []. потому что данные - это несколько значений, извлекаемых из таблицы.
но где вы определили
у вас есть поле выбора, которое выбирает одну запись. у вас есть скрытое поле, которое определяется один раз. где несколько значений
есть цикл, и он не определен один раз
Можете ли вы включить отчет об ошибках и вставить результат сюда. Проверить здесь