0

On the front end of my PHP, I want the user to decided what field to insert into among multiple fields. I can do that with multiple 'if' statements like I have below, but that makes for a long code. Can I use a variable for the field, i.e Song_id, Album_id? I've tried doing something like "Insert into Song(?) values (?)" but that doesn't seem to work.

    case "insert":
    if ($table=="Song"){
        if($field=="Song_id"){
            $stmt = $db->prepare("INSERT into Song(Song_id) values (?)");
            $stmt->bindValue(1, $_POST['value'], PDO::PARAM_INT);
            $stmt->execute();
            echo "done";}
        break;}
    if ($table=="Song"){
        if($field=="Alubm_id"){
            $stmt = $db->prepare("INSERT into Song(Album_id) values (?)");
            $stmt->bindValue(1, $_POST['value'], PDO::PARAM_INT);
            $stmt->execute();
            echo "done";}
        break;}

etc, etc, etc.

Thanks!

  • You cannot bind columns. Read this http://stackoverflow.com/a/15990488/3885509 – Charlotte Dunois Dec 06 '16 at 18:43
  • But you can generate them before binding values. Something like `$db->prepare("INSERT into Song($field_id) values (?)");`. Then it can be elaborated to also make table dynamic the same way. – cFreed Dec 06 '16 at 18:46
  • The duplicate answer deals with PDO, but it is a similar issue with MySQLi - http://stackoverflow.com/questions/1504192/mysqli-parameter-binding-issue – Sean Dec 06 '16 at 18:49

0 Answers0