duandaotuo5542 2018-05-28 15:09
浏览 47
已采纳

$ _POST插入多对多,错误的PHP语法?

EDIT: IGNORE ANY SQL INJECTIONS OR VULNERABLE CODE STATEMENTS :D (School Project).

I wish to create a insert form on my webpage where I can select an artist from a table, including a song from a table and combine them for an insert into a combined foreign key table.

I have managed to do selects and insert with only individual artist and song drop-downs on my web-page, but would wish for combining the two ID's from each table to combine them to a many to many relative table. But when I press the submit button nothing happens, and I'm a beginner and don't know if I'm missing any important bits of actually Posting the information.

For troubleshooting I have tried my code, and tested it. I see if I remove my code theres no problem, so the problem persists on the syntax I believe, as the first dropdown shows, alongside the second dropdown and submit button, but the problem is within the actual processing and SQL query part, where it never goes to the DB..

The problem: enter image description here

As you can see below I have a the text Song Name appear with a drop-down menu in the bottom left corner including the Artist Name with a submit button. But my problem persists as the select and then insert from the two drop downs into the combined table does not work, it does not actually submit, I want it to post into the DB what can I do. But somethings off? I would appreciate any questions or help, this community is so amazing and wonderful to operate in!

Database Database in use in code PHP

  <form method='POST'>
<?php

include('connect_mysql.php');
if(isset($_POST["mangetilmange"])) {
  $song_id = $_POST["song_id"];
  $artist_id = $_POST["artist_id"];


$sql ="INSERT INTO artist_has_song (song_id, artist_id) VALUES 
('$song_id', '$artist_id')";

if($conn->query($sql)) {
    echo "Completed";

} else {
    echo "Blablalbablablablablablablabl $sql
    ($conn->error.";
}
}
?>

Song Name

<?php
$sql = "SELECT * FROM song";
$resultat = $conn->query($sql);

echo "<select name='song_id'>";

while ($rad = $resultat->fetch_assoc()) {
$song_id = $rad["song_id"];
$songname = $rad["songname"];

echo "<option value='$song_id'>$songname</option>";
}

echo "</select>";
?>

Artist Name

<?php
$sql = "SELECT * FROM artist";
$resultat = $conn->query($sql);

echo "<select name='artist_id'>";

while ($rad = $resultat->fetch_assoc()) {
  $artist_id = $rad["artist_id"];
  $artistname = $rad["artistname"];

echo "<option value='$artist_id'>$artistname</option>";
}

echo "</select>";
?>
</form>
<input type="submit" name="mangetilmange" value ="Submit">
  • 写回答

1条回答 默认 最新

  • dqrq93879 2018-05-28 15:16
    关注

    change you code to this:

     <form method='POST'>
     <?php
    
     include('connect_mysql.php');
     if(isset($_POST["mangetilmange"])) {
     $song_id = $_POST["song_id"];
     $artist_id = $_POST["artist_id"];
    
    
     $sql ="INSERT INTO artist_has_song (song_id, artist_id) VALUES 
     ('$song_id', '$artist_id')";
    
     if($conn->query($sql)) {
     echo "Completed";
    
     } else {
     echo "Blablalbablablablablablablabl"; 
     }
     }
     ?>
    
     Song Name
     <?php
     $sql = "SELECT * FROM song";
     $resultat = $conn->query($sql);
    
     echo "<select name='song_id'>";
    
     while ($rad = $resultat->fetch_assoc()) {
     $song_id = $rad["song_id"];
     $songname = $rad["songname"];
    
     echo "<option value='$song_id'>$songname</option>";
     }
    
     echo "</select>";
     ?>
    
     Artist Name
    
     <?php
     $sql = "SELECT * FROM artist";
     $resultat = $conn->query($sql);
    
     echo "<select name='artist_id'>";
    
     while ($rad = $resultat->fetch_assoc()) {
     $artist_id = $rad["artist_id"];
     $artistname = $rad["artistname"];
    
     echo "<option value='$artist_id'>$artistname</option>";
     }
    
     echo "</select>";
     ?>
     <input type="submit" name="mangetilmange" value ="Submit">
     </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?