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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题