Home > OS >  PHP updating only the first MySQL value in the database
PHP updating only the first MySQL value in the database

Time:12-22

I'm trying to let my users add social links, but for some reason, my code is only updating the first record. I'm using mysqli and I've defined all my variables somewhere up in the code that is not included here. Could someone please take a look and help? Thanks in advance!

<?php
else if ($_GET["page"] == "Socials") {
                echo "<h1 class='center' style='color: red;'>Under construction</h1>";
                echo "<div class='mobile-center justify'>";
                echo "<div id='headSocials'>";
                echo "<h1>Social Links<h1>";
                echo "<hr>";
                echo "<br>";
                echo "<h4 class='row_item'>Links</h4>";
                echo "<form method='POST'>";
                if ($link1 == "Not Set") {
                    echo "<input type='submit' name='addlink1' id='addlink1' value='Add Link' class='btn-static row_item'>";
                }
                if ($link1 != "Not Set" && $link2 == "Not Set") {
                    echo "<input type='submit' name='addlink2' id='addlink2' value='Add Link' class='btn-static row_item'>";
                }
                if ($link1 != "Not Set" && $link2 != "Not Set" && $link3 == "Not Set") {
                    echo "<input type='submit' name='addlink3' id='addlink3' value='Add Link' class='btn-static row_item'>";
                }
                if ($link1 != "Not Set" && $link2 != "Not Set" && $link3 != "Not Set" && $link4 == "Not Set") {
                    echo "<input type='submit' name='addlink4' id='addlink4' value='Add Link' class='btn-static row_item'>";
                }
                if ($link1 != "Not Set" && $link2 != "Not Set" && $link3 != "Not Set" && $link4 != "Not Set") {
                    echo "<p>You've used all your available links. Please delete one before adding another.</p>";
                }

                if (isset($_POST["addlink1"])) {
                    $formstatus1 = "inline;";
                } if (isset($_POST["addlink2"])) {
                    $formstatus2 = "inline;";
                } if (isset($_POST["addlink3"])) {
                    $formstatus3 = "inline;";
                } if (isset($_POST["addlink4"])) {
                    $formstatus4 = "inline;";
                }

                echo "</form>";
                echo "</div>";
                echo "<div style='display: $formstatus1'>";
                echo "<form id='link1form' method='POST'>";
                echo "Link 1 <input type='url' name='url1' id='url1' placeholder='https://example.com' pattern='https://.*' maxlength='60' size='30' class='login-input row_item' required><br>";
                echo "Name &nbsp;<input type='text' name='name1' id='name1' placeholder='Examples: Discord, Website, YouTube' maxlength='60' size='30' class='login-input row_item' required><br>";
                echo "<input type='submit' name='submitLink1' id='submitLink1' value='Submit' class='btn-static row_item'>";
                echo "</form><br>";
                echo "</div>";
                echo "<div style='display: $formstatus2'><hr>";
                echo "<form id='link2form' method='POST'>";
                echo "Link 2 <input type='url' name='url2' id='url2' placeholder='https://example.com' pattern='https://.*' maxlength='60' size='30' class='login-input row_item' required><br>";
                echo "Name &nbsp;<input type='text' name='name2' id='name2' placeholder='Examples: Discord, Website, YouTube' maxlength='60' size='30' class='login-input row_item' required><br>";
                echo "<input type='submit' name='submitLink1' id='submitLink1' value='Submit' class='btn-static row_item'>";
                echo "</form><br>";
                echo "</div>";
                echo "<div style='display: $formstatus3'><hr>";
                echo "<form id='link3form' method='POST'>";
                echo "Link 3 <input type='url' name='url3' id='url3' placeholder='https://example.com' pattern='https://.*' maxlength='60' size='30' class='login-input row_item' required><br>";
                echo "Name &nbsp;<input type='text' name='name3' id='name3' placeholder='Examples: Discord, Website, YouTube' maxlength='60' size='30' class='login-input row_item' required><br>";
                echo "<input type='submit' name='submitLink1' id='submitLink1' value='Submit' class='btn-static row_item'>";
                echo "</form><br>";
                echo "</div>";
                echo "<div style='display: $formstatus4'><hr>";
                echo "<form id='link4form' method='POST'>";
                echo "Link 4 <input type='url' name='url4' id='url4' placeholder='https://example.com' pattern='https://.*' maxlength='60' size='30' class='login-input row_item' required><br>";
                echo "Name &nbsp;<input type='text' name='name4' id='name4' placeholder='Examples: Discord, Website, YouTube' maxlength='60' size='30' class='login-input row_item' required><br>";
                echo "<input type='submit' name='submitLink1' id='submitLink1' value='Submit' class='btn-static row_item'>";
                echo "</form>";
                echo "</div>";
                echo "</div>";

                if (isset($_POST["submitLink1"])) {
                    $stmt = $usdb->prepare("UPDATE `$usr_name` SET link1 = ?, link1name = ?");
                    $stmt->bind_param("ss", $_POST["url1"], $_POST["name1"]);
                    $stmt->execute();
                }
                
                if (isset($_POST["submitLink2"])) {
                    $stmt = $usdb->prepare("UPDATE `$usr_name` SET link2 = ?, link2name = ?");
                    $stmt->bind_param("ss", $_POST["url2"], $_POST["name2"]);
                    $stmt->execute();
                }

                if (isset($_POST["submitLink3"])) {
                    $stmt = $usdb->prepare("UPDATE `$usr_name` SET link3 = ?, link3name = ?");
                    $stmt->bind_param("ss", $_POST["url3"], $_POST["name3"]);
                    $stmt->execute();
                }

                if (isset($_POST["submitLink4"])) {
                    $stmt = $usdb->prepare("UPDATE `$usr_name` SET link4 = ?, link4name = ?");
                    $stmt->bind_param("ss", $_POST["url4"], $_POST["name4"]);
                    $stmt->execute();
                }?>

I've been stuck on this for a while now, I'd very much appreciate it if someone could help me solve it! (If you need context to this code, go to https://nikkiedev.com/users/myprofile/settings/?page=Socials)

CodePudding user response:

echo "<input type='submit' name='submitLink1' id='submitLink1' value='Submit' class='btn-static row_item'>";

Here's your problem, you use this exact line of code for all 4 of your submit buttons - you forgot to update the number on the submitLink name and id, so all four of them hit if (isset($_POST["submitLink1"])):

  • Related