-2

I am getting an error in my code where essentially my user input isn't being read and I'm not sure why. I used isset and it skips over, that means that the user input isn't being read in right? Any ideas as to why not? For reference when I submit the form I get another error which says ' Notice: Undefined variable: bound in C:\xampp\htdocs\editartist.php' but I think thats down to fact that when you search you no longer have the id anymore. I've left a bit of code to show.

<?php
if(isset($_GET['id']))
{   
    $artistID = fix_string($_GET['id']);
    $sql = ("SELECT artName FROM artist WHERE artID = '$artistID' ");
    $result = $conn->prepare($sql);
    $result->execute();
    $result->bind_result($bound);
    $result->fetch();
}
else {
    echo "this is broken";
}
?>
<form method="post" action="editartist.php"/>
<?php echo '<input type="text" name="artistname" value= "'.$bound.'">' ?>
<input type="submit" value="Save"/>
</form>

<?php   
if(isset($_GET['id'])) {
    if(isset($_GET['artistname'])) {
    $userinput = $_GET['artistname']; 
    echo "$userinput $artistID";
    $sqltwo = ("UPDATE artist SET artName='$userinput' WHERE artID='$artistID'");
    $stmt = $conn->prepare($sqltwo);
    $stmt->execute();
    } else {
        echo "this is broken 2";
    }
    }
?>

This isn't a duplicate, its not asking about the 2nd error. The primary question is why the user input isn't being read?

Chunelle
  • 27
  • 1
  • 5

1 Answers1

0

because you are using POST method in the html form, while in php is waiting for GET Method

i rewrote your code assuming you are sending the ID in Get request as below

    <?php
if(isset($_GET['id']))
{   
    $artistID = fix_string($_GET['id']);
    $sql = ("SELECT artName FROM artist WHERE artID = '$artistID' ");
    $result = $conn->prepare($sql);
    $result->execute();
    $result->bind_result($bound);
    $result->fetch();
}
else {
    echo "this is broken";
}
?>
<form method="post" action="editartist.php"/>
<?php echo '<input type="text" name="artistname" value= "'.$bound.'">' ?>
<input type="submit" value="Save"/>
</form>

<?php   
//no need to check for the id here , assuming you are posting data now.and you //already checked at the top of script for $_GET['id']
    if(isset($_POST['artistname'])) {
    $userinput = $_POST['artistname']; 
    echo "$userinput $artistID";
    $sqltwo = ("UPDATE artist SET artName='$userinput' WHERE artID='$artistID'");
    $stmt = $conn->prepare($sqltwo);
    $stmt->execute();
    } else {
        echo "this is broken 2";
    }

?>

you need to consider sanitize Sanitize filters

and properly binding params :)

Hatem Ahmed
  • 180
  • 8