Issue
<?php
include_once('database1.php');
$sql = "select * from klients ";
$result = mysqli_query($conn, $sql);
?>
//Doctype
<div class="container" >
<form action="editClientsCode.php" method="POST">
<table class="responsive-table">
<tr>
<td>ID</th>
<td>Name</th>
<td>Second Name</th>
<td>Adress</th>
<td>Phone Number</th>
<td>Button</th>
</tr>
<tbody>
<tr>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<td><input class="input2" name="ClientId" value=<?php echo $row['ClientId']?>></td>
<td><input class="input2" name="Name" value=<?php echo $row['Name']?>></td>
<td><input class="input2" name="SecondName" value=<?php echo $row['SecondName']?>></td>
<td><input class="input2" name="Adress" value=<?php echo $row['Adress']?>></td>
<td><input class="input2" name="PhoneNumber" value=<?php echo $row['PhoneNumber']?>></td>
<td>
<button type="submit" class="button-confirm">Update</button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>
</div>
<?php
include_once 'database1.php';
// Check if "ClientId" key exists in the $_POST array
if (isset($_POST["ClientId"])) {
$id = $_POST["ClientId"];
} else {
// Handle the case where "ClientId" is not set
echo "ClientId is not set.";
exit();
}
$Name = $_POST["Name"];
$SecondName = $_POST["SecondName"];
$Adress = $_POST["Adress"];
$PhoneNumber = $_POST["PhoneNumber"];
// Use prepared statement to prevent SQL injection
$sql = "UPDATE klients SET Name=?, SecondName=?, Adress=?, PhoneNumber=? WHERE ClientID=?";
$stmt = mysqli_prepare($conn, $sql);
// Check for errors in preparing the statement
if (!$stmt) {
echo "Error preparing statement: " . mysqli_error($conn);
exit();
}
// Bind parameters
mysqli_stmt_bind_param($stmt, "sssii", $Name, $SecondName, $Adress, $PhoneNumber, $id);
// Execute the statement
$result = mysqli_stmt_execute($stmt);
// Check for errors in executing the statement
if (!$result) {
echo "Error updating record: " . mysqli_stmt_error($stmt);
exit();
}
// Check the number of affected rows
if (mysqli_stmt_affected_rows($stmt) > 0) {
// Records updated successfully
header("Location: ../appp/ShowKlients.php?signup=success");
exit();
} else {
// No records updated, handle accordingly
echo "No records updated.";
}
// Close the statement and connection
mysqli_stmt_close($stmt);
mysqli_close($conn);
?>
After I'd clicked the button Update, nothing changed. SQL is connected succesfully. I want to update my data , but instead its just do nothing and have 0 errors. ClientId should not to change.But without id changes , it show me the error Undefinded array ClientID. It seems like there might be an issue with how the ClientId is being passed to my PHP script. In my HTML form, each input field seems to have the name "ClientId," but when submitting the form, only one of them will be sent, and it might not be the one i expect.
Solution
The issue with your PHP code not updating the SQL data likely stems from the form structure in your HTML. Each row in your table generates input fields with the same name
attributes. When you submit the form, only the last row's data is sent to the server, because input names must be unique within a single form for the server to distinguish between them.
To address this, you can append the row's unique identifier (e.g., ClientId
) to each input field's name. This ensures that each input field is unique and allows you to update each row individually.
Here's a revised code snippet for your form and PHP processing script:
<!-- Revised Form with Unique Input Names -->
<div class="container">
<form action="editClientsCode.php" method="POST">
<table class="responsive-table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Second Name</th>
<th>Adress</th>
<th>Phone Number</th>
<th>Button</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($result)) : ?>
<tr>
<td><input class="input2" name="ClientId[<?php echo $row['ClientId'] ?>]" value="<?php echo $row['ClientId'] ?>"></td>
<td><input class="input2" name="Name[<?php echo $row['ClientId'] ?>]" value="<?php echo $row['Name'] ?>"></td>
<td><input class="input2" name="SecondName[<?php echo $row['ClientId'] ?>]" value="<?php echo $row['SecondName'] ?>"></td>
<td><input class="input2" name="Adress[<?php echo $row['ClientId'] ?>]" value="<?php echo $row['Adress'] ?>"></td>
<td><input class="input2" name="PhoneNumber[<?php echo $row['ClientId'] ?>]" value="<?php echo $row['PhoneNumber'] ?>"></td>
<td><button type="submit" class="button-confirm">Update</button></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</form>
</div>
In your PHP script, you'll need to iterate over the submitted data to update each row:
// PHP Processing Script
include_once 'database1.php';
// Iterate over the submitted ClientIds
foreach ($_POST['ClientId'] as $id => $value) {
$Name = $_POST['Name'][$id];
$SecondName = $_POST['SecondName'][$id];
$Adress = $_POST['Adress'][$id];
$PhoneNumber = $_POST['PhoneNumber'][$id];
// Use a prepared statement to prevent SQL injection
$sql = "UPDATE klients SET Name=?, SecondName=?, Adress=?, PhoneNumber=? WHERE ClientID=?";
$stmt = mysqli_prepare($conn, $sql);
if (!$stmt) {
echo "Error preparing statement: " . mysqli_error($conn);
continue;
}
mysqli_stmt_bind_param($stmt, "sssii", $Name, $SecondName, $Adress, $PhoneNumber, $id);
$result = mysqli_stmt_execute($stmt);
if (!$result) {
echo "Error updating record for ClientId $id: " . mysqli_stmt_error($stmt);
continue;
}
}
mysqli_close($conn);
Recap: The issue is likely due to the HTML form's input fields having the same name
attribute across multiple rows, causing only the last row's data to be sent to the server. The solution is to make each input field's name unique by appending the row's ClientId
to the input name. In the PHP script, iterate over the submitted data to update each row individually.
Answered By - suchislife
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.