I am trying to make a discord bot in PHP (which works, i know not the best choice of language but that is what i am most comfortable with for my own needs for now) with a dashboard.
The idea was to be able to add and edit the commands on the website.
I am selecting my current commands using mysql for the form like this:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$disable = $row['Disabled'];
echo "<input type='hidden' name='id[]' value='";
echo $row['Id'];
echo "'>";
echo '<tr>';
echo '<td class="tg-515c"><input type="text" name="command[]" value="';
echo $row['Command'];
echo '" class="form-control"></td>';
echo '<td class="tg-wp8o"><input type="text" name="text[]" value="';
echo $row['Text'];
echo '" class="form-control"></td>';
echo '<td class="tg-wp8o"><select class="form-control" name="disabled[]">';
if($disable == '0')
{
echo "<option value='1'>Yes</option><option value='0' selected>No</option>";
}
else
{
echo "<option value='1' selected>Yes</option><option value='0'>No</option>";
}
echo "</select>";
echo '</td>';
echo '</tr>';
}
and i have the form outside of my php syntax.
That seems to be working fine.
// Create connection
$command = $_POST['command'];
$text = $_POST['text'];
$id = $_POST['id'];
$disable = $_POST['disabled'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="UPDATE dashboard SET Command='$command[0]', Text='$text[0]', Disabled='$disable[0]' WHERE Id='$id[0]'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
header("Location: /");
$conn->close();
I have just copy/pasted this code few times and changed the array from [0] to [1], etc.
This now works, however, i have another file which adds more commands to database, so i need it to be automatized each time i add a new command and not manually type for each input.
Edited the code to updated version.
2
Answers
I was able to figure out a solution by doing this:
So i decided to do a for loop and count how many inputs i have, so for each input, it automatically creates a new query updating it.
I made an example for you that I think works for you and it works now and I tried it
The second page is where the values are entered
I know that your code calls all the data and determines through them what data you want to change