所以基本上我有一个充满输入的表,这些输入充满了从mysql数据库中获取的值。我也使用单选按钮选择其中一行,然后编辑它,该行应该在数据库上更新,但它一直让我失去"默认"值,我的意思是,现有数据是旧值而不是新值。这是我的整个代码:
<?php
include 'config.php';
$dbname = "pruebasPHP";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Conexión fallida: " . mysqli_connect_error());
}
echo '<html><head><title> Modificar datos Mysql PHP </title>
<style> td, th { padding: 5px; border: 1px solid black;} input {border: 0px;}</style>
<script type="text/javascript">
//<![CDATA[
window.onload = function(){
var cuantos = document.getElementById("valorI").value;
for(var x=0; x<cuantos; x++){
var nx = document.getElementById("n".concat(x));
nx.readOnly = true;
var ax = document.getElementById("a".concat(x));
ax.readOnly = true;
var ex = document.getElementById("e".concat(x));
ex.readOnly = true;
}
}
function escritura() {
var cuantos = document.getElementById("valorI").value;
for(var x=0; x<cuantos; x++){
var radiox = document.getElementById("r".concat(x));
if (radiox.checked) {
document.getElementById("n".concat(x)).readOnly = false;
document.getElementById("a".concat(x)).readOnly = false;
document.getElementById("e".concat(x)).readOnly = false;
}
else {
document.getElementById("n".concat(x)).readOnly = true;
document.getElementById("a".concat(x)).readOnly = true;
document.getElementById("e".concat(x)).readOnly = true;
}
}
}
//]]>
</script>
</head><body><center></br>';
if (isset($_POST['modificar'])) {
if(isset($_POST['radio'])){
$datos = $_POST['radio'];
$datosSeparados = explode(":", $datos);
$sql = 'UPDATE tabla1 SET
firstname="'.$datosSeparados[0].'",
lastname="'.$datosSeparados[1].'",
email="'.$datosSeparados[2].'"
WHERE id='.$datosSeparados[3].';';
echo $sql;
if (mysqli_query($conn, $sql))
echo 'El registro con el ID=<b>' . $datosSeparados[3] . '</b> se ha modificado satisfactoriamente. </br>';
else
echo 'Error. Ha ocurrido un problema tratando de modificar el registro con ID=<b>' . $datosSeparados[3] . '</b>: </br> ' . mysqli_error($conn);
}
}
echo '<form action="" method="post"> Registros a modificar:<br/><table>';
$sql = "SELECT id, firstname, lastname, email from tabla1;";
$resultado = mysqli_query($conn, $sql);
echo '<tr>';
if ($fila = mysqli_fetch_assoc($resultado)){
foreach($fila as $x => $x_value) {
echo '<th>' . $x . '</th>';
}
}
echo '</tr>';
if (mysqli_num_rows($resultado) > 0) {
$i = 0;
while($fila = mysqli_fetch_assoc($resultado)) {
echo '
<tr>
<td> ' . $fila["id"] . '</td>
<td><input type="text" id="n' . $i . '" name="nombre" value="' . $fila["firstname"] . '" /></td>
<td><input type="text" id="a' . $i . '" name="apellidos" value="' . $fila["lastname"] . '" /></td>
<td><input type="text" id="e' . $i . '" name="email" value="' . $fila["email"] . '" /></td>
<td><input type="radio" id="r' . $i . '" name="radio" value="'.$fila["firstname"].':'.$fila["lastname"].':'.$fila["email"].':'.$fila["id"].'" onchange="escritura();" /></td>
</tr>
';
$i++;
}
echo '<input type="hidden" id="valorI" value="'. $i . '"/>';
}
echo '
</table>
<br/>
<input type="submit" name="modificar" value="Modificar">
<input type="reset" value="Limpiar">
</form>';
echo '</center>
</body>
</html>';
?>
答案 0 :(得分:0)
关于MVC(模型视图控制器)的详细解释有点长,但是你可以在谷歌上找到一堆信息。简而言之,它分层。模型(处理数据,数据库查询),控制器(逻辑构建视图所需的数据),视图(html,css,js)..上面的代码很难遵循,我相信你会受益于MVC图案。
无论如何,要开始分离你的代码,你想要开始组织一下。我会将您的初始SQL查询置于最顶层(模仿数据层),然后如果需要任何逻辑,最后是您的html。
我将告诉你如何重做你的代码。所有这些代码都在一个文件中,仅用于演示目的。最终你想在不同的文件中分开它。
这是您网页的顶部,模拟您从数据库中获取的数据到 PHP 。我会将此数据分配给您的变量$resultado
:
<?php
$resultado = array(
array(
"id" => 1,
"firstname" => "John",
"lastname" => "Doe",
"email" => "j@d.com"
),
array(
"id" => 2,
"firstname" => "Jane",
"lastname" => "Johnson",
"email" => "ja@d.com"
),
array(
"id" => 3,
"firstname" => "Tom",
"lastname" => "Brady",
"email" => "b@d.com"
)
);
?>
然后,由于没有太多的逻辑发生,我将输出&#34;视图&#34;或HTML。在这里,您会注意到我创建了一个不同的功能来锁定&#34;所有你的输入,以及另一个将通过当前无线电和&#34;解锁&#34;那些投入。请注意,任何嵌入式PHP都包含在HTML中的PHP标记中。 :
<html>
<head>
<script type="text/javascript">
window.onload = function () {
lock_inputs();
};
function lock_inputs() {
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type === "text") {
inputs[i].readOnly = true;
}
}
}
function escritura(radio, i) {
lock_inputs();
if (radio.checked) {
document.getElementById("n".concat(i)).readOnly = false;
document.getElementById("a".concat(i)).readOnly = false;
document.getElementById("e".concat(i)).readOnly = false;
}
}
</script>
</head>
<body>
<form action="" method="post">
Registros a modificar:<br/>
<table>
<?php
$i = 0;
foreach ($resultado as $fila) {
?>
<tr>
<td><?php echo $fila["id"] ?></td>
<td>
<input type="text" id="n<?php echo $i ?>" name="nombre" value="<?php echo $fila["firstname"] ?>"/>
</td>
<td>
<input type="text" id="a<?php echo $i ?>" name="apellidos" value="<?php echo $fila["lastname"] ?>"/>
</td>
<td>
<input type="text" id="e<?php echo $i ?>" name="email" value="<?php echo $fila["email"] ?>"/>
</td>
<td>
<input type="radio" id="r<?php echo $i ?>" name="radio" value="<?php echo $fila["firstname"] . ':' . $fila["lastname"] . ':' . $fila["email"] . ':' . $fila["id"] ?>" onchange="escritura(this,<?php echo $i ?>);"/>
</td>
</tr>
<?php
$i++;
}
?>
</table>
</form>
</body>
</html>
我希望你现在能更好地理解这段代码。我确实删除了很多其他代码,以便您可以了解并理解分离的重要性。
希望这有帮助。