我有php函数更新数据表中的所有记录,需要通过html中的单击按钮运行。
我的php函数看起来像这样:
<?php
try {
$sql = 'SELECT id_data, date_record, value1, value2, value3 FROM data ';
$s = $pdo->prepare($sql);
$s->execute();
} catch (PDOException $e) {
$error = 'Error with select data' . $e->getMessage();
include 'error.html.php';
exit();
}
while ($row = $s->fetch()) {
$dane[] = array(
'id_data' => $row['id_data'],
'date_record' => $row['date_record'],
'value1' => $row['value1'],
'value2' => $row['value2'],
'value3' => $row['value3']
);
}
if (isset($_GET['edytion'])) {
foreach ($data as $data2) {
try {
$sql = 'UPDATE data SET date_record = :date_record, value1 = :value1, value2 = :value2, value3 = :value3 WHERE id_data= :id_data';
$s = $pdo->prepare($sql);
$s->bindValue(':date_record', $_POST['date_record']);
$s->bindValue(':value1', $_POST['value1']);
$s->bindValue(':value2', $_POST['value2']);
$s->bindValue(':value3', $_POST['value3']);
$s->bindValue(':id_data', $_POST['id_data']);
$s->execute();
} catch (PDOException $e) {
$error = 'Edit data error ' . $e->getMessage();
include 'error.html.php';
exit();
}
}
Header("Location: theme3.php");
}
?>
我在html中的表单,我尝试运行这个样子:
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Value 1</th>
<th>Value 2</th>
<th>Value 3</th>
</tr>
</thead>
<?php if (isset($data)): ?>
<?php foreach ($data as $data1): $a = 0 ?>
<form action="?edytion" method="post" id='ed'>
<tr class="bg-primary">
<input type="hidden" name="id_data" id="id_data" value="<?php echo $data1['id_data']; ?>">
<td><input type="date" name="date_record" id="date_record" value="<?php echo $data1['date_record']; ?>"> </td>
<td><input type="text" name="value1" id="value1" value="<?php echo $data1['value1']; ?>"> </td>
<td><input type="text" name="value2" id="value2" value="<?php echo $data1['value2']; ?>"> </td>
<td><input type="text" name="value3" id="value3" value="<?php echo $data1['value3']; ?>"> </td>
<!-- <input type="hidden" ondblclick="default" id="id_buttona" value="Edit"/> -->
</tr>
</form>
<?php $a++;
endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<input type="submit" id="id_buttona" onclick="document.getElementById('ed').submit();" value="Edit"/>
</div>
最终,当我尝试更新数据时,它只更新表中的第一条记录,其余部分是不变的 谁知道什么是错的,并且知道它有多正确吗?我将不胜感激!
答案 0 :(得分:1)
您使用相同的id
创建了太多表单。使用<form>
围绕桌子,仅foreach
<tr>
。
像这样的东西
<div class="table-responsive">
<form action="?edytion" method="post" id='ed'>
<table class="table table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Value 1</th>
<th>Value 2</th>
<th>Value 3</th>
</tr>
</thead>
<?php if (isset($data)): ?>
<?php $a = 0; foreach ($data as $data1): ?>
<tr class="bg-primary">
<input type="hidden" name="id_data<?php echo $a; ?>" id="id_data" value="<?php echo $data1['id_data']; ?>" />
<td><input type="date" name="date_record<?php echo $a; ?>" value="<?php echo $data1['date_record']; ?>"> </td>
<td><input type="text" name="value1<?php echo $a; ?>" value="<?php echo $data1['value1']; ?>"> </td>
<td><input type="text" name="value2<?php echo $a; ?>" value="<?php echo $data1['value2']; ?>"> </td>
<td><input type="text" name="value3<?php echo $a; ?>" value="<?php echo $data1['value3']; ?>"> </td>
</tr>
<?php $a++;
endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<input name="row_count" value="<?php echo isset($a) ? $a : 0; ?>" type="hidden"/>
</form>
<input type="submit" id="id_buttona" onclick="document.getElementById('ed').submit();" value="Edit"/>
</div>
您为每一行创建每个表单的方式效果不佳,因为:(1)所有表单都具有相同的javascript id
,当您执行getElementById
时,只有第一个表单受到影响,( 2)当您提交一个表单时,页面重新加载并且对其他行的所有更改都将丢失。
一种解决方案是仅制作1个表单并且对所有字段具有不同的名称。表单字段由name
和value
发送,因此您需要为所有字段指定不同的名称,而且您并不真正需要ID。
您可以在表单中的某处添加行计数,然后将php更改为以下内容:
$row_count = $_POST['row_count'];
for($i = 0; i < $row_count; i++) {
try {
$sql = 'UPDATE data SET
date_record = :date_record,
value1 = :value1,
value2 = :value2,
value3 = :value3
WHERE id_data= :id_data';
$s = $pdo->prepare($sql);
$s->bindValue(':date_record', $_POST['date_record' . $i]);
$s->bindValue(':value1', $_POST['value1' . $i]);
$s->bindValue(':value2', $_POST['value2' . $i]);
$s->bindValue(':value3', $_POST['value3' . $i]);
$s->bindValue(':id_data', $_POST['id_data' . $i]);
$s->execute();
}
catch (PDOException $e) {
$error = 'Edit data error ' . $e->getMessage();
include 'error.html.php';
exit();
}
}
答案 1 :(得分:0)
这是所有html文件
<!DOCTYPE HTML PUBLIC>
<html>
<head>
<title>Tematyka2</title>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
</head>
<body>
<div id="container">
<div id="heading"></div>
<table border="0" width="766" cellpadding="0" cellspacing="0" align="center">
<tr>
<td class="t1"><p>Example title</p></td><td class="t2"> <a href="logout.php"><b class="linkW" style="padding-left: 60%">Logout</b></a></td>
</tr>
<tr>
<td class="top1"><div class="inscription">Your Page</div></td>
<td class="top2" valign="top"></td>
</tr>
</table>
<table align="center" cellpadding="0">
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0">
<tr><td class="topm">Main menu</td></tr>
<tr><td class="tlom">
:: <a href="index.php">Main page</a> <br>
</td></tr>
<tr><td class="dolm"></td></tr
</table>
<table cellpadding="0" cellspacing="0">
<tr><td class="topm">Them</td></tr>
<tr><td class="tlom">
:: <a href="them1.php">Them 1</a> <br>
:: <a href="them2.php"><b>Them 2</b></a> <br>
:: <a href="them3.php">Them 3</a> <br>
</td></tr>
<tr><td class="dolm"></td></tr
</table>
<table cellpadding="0" cellspacing="0">
<tr><td class="topm">Charts</td></tr>
<tr><td class="tlom">
:: <a href="">Chart1</a> <br>
:: <a href="">Chart2</a> <br>
</td></tr>
<tr><td class="dolm"></td></tr>
</table>
<br>
</td>
<td width="1"></td>
<td valign="top">
<table cellpadding="0" cellspacing="0">
<tr><td class="topn"></td></tr>
<tr><td class="tlon">
<div class="span7 center">
<h2>
EDYTION
</h2>
</div>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Value 1</th>
<th>Value 2</th>
<th>Value 3</th>
</tr>
</thead>
<?php if(isset($data)): ?>
<?php foreach($data as $data1): $a=0?>
<form action="?edytion" method="post" id='ed'>
<tr class="bg-primary">
<input type="hidden" name="id_data" id="id_data" value="<?php echo $data1['id_data']; ?>">
<td><input type="date" name="date_record" id="date_record" value="<?php echo $data1['date_record']; ?>"> </td>
<td><input type="text" name="value1" id="value1" value="<?php echo $data1['value1']; ?>"> </td>
<td><input type="text" name="value2" id="value2" value="<?php echo $data1['value2']; ?>"> </td>
<td><input type="text" name="value3" id="value3" value="<?php echo $data1['value3']; ?>"> </td>
<!-- <input type="hidden" ondblclick="default" id="id_buttona" value="Edit"/> -->
</tr>
</form>
<?php $a++; endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<input type="submit" id="id_buttona" onclick="document.getElementById('ed').submit();" value="Edit"/>
</div>
</td></tr>
<!-- <tr><td class="doln"></td></tr> -->
</table>
</td>
</tr>
</table>
<table align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="foot1"> My page</td>
<td class="foot2"><a href="" title="szablony"><img src="images/dol2.jpg" alt="anything"></a></td>
</tr>
</table>
和Php部分:
<?php
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
include $_SERVER['DOCUMENT_ROOT'] . '/connection_to_database.php';
include $_SERVER['DOCUMENT_ROOT']. '/check_login.php'; try {
$sql = 'SELECT id_data, date_record, value1, value2, value3 FROM data ';
$s = $pdo->prepare($sql);
$s->execute();
} catch (PDOException $e) {
$error = 'Error with select data' . $e->getMessage();
include 'error.html.php';
exit();
}
while ($row = $s->fetch()) {
$dane[] = array(
'id_data' => $row['id_data'],
'date_record' => $row['date_record'],
'value1' => $row['value1'],
'value2' => $row['value2'],
'value3' => $row['value3']
);
}
if (isset($_GET['edytion'])) {
foreach ($data as $data2) {
try {
$sql = 'UPDATE data SET
date_record = :date_record,
value1 = :value1,
value2 = :value2,
value3 = :value3
WHERE id_data= :id_data';
$s = $pdo->prepare($sql);
$s->bindValue(':date_record', $_POST['date_record']);
$s->bindValue(':value1', $_POST['value1']);
$s->bindValue(':value2', $_POST['value2']);
$s->bindValue(':value3', $_POST['value3']);
$s->bindValue(':id_data', $_POST['id_data']);
$s->execute();
}
catch (PDOException $e) {
$error = 'Edit data error ' . $e->getMessage();
include 'error.html.php';
exit();
}
}
Header("Location: theme3.php");
}
?>
这就是更新数据。 Php和html在一个文件中,我知道它看起来不专业,我不应该这样做。