So basically I have a table full of inputs and those inputs are filled with values taken from a mysql database. I am also using radio buttons to select one of those rows and then edit it, that row is supposed to be updated on the database but it keeps throwing me the "default" value, I mean, the exiting data is the old value not the new one. Here is my entire code:
<?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>';
?>