将数据插入表中

时间:2013-12-29 15:18:33

标签: php mysql post insert

我有三张桌子:演员,电影和角色。表角色解决了多对多关系的问题。现在我正在创建一个页面来添加移动到数据库并分配一个actor。

表单将电影添加到表格电影中,但它不会向表格角色添加记录。 这是我的代码:

<form action="" method="post">
<table class="addactor">
<tr>
<td class="right">Movie code:</td> <td><input type="text" class="textfield" name="moviecode" value="" required></td>
</tr>
<tr>
<td class="right">Title:</td><td><input type="text" class="textfield" name="title" value="" required></td>
</tr>
<tr>
<td class="right">Date:</td><td><input type="text" class="textfield" name="dateofissue" value="" required></td>
</tr>
<tr>
<td class="right">Description:</td><td><input type="text" class="textfield" name="desc" value="" required></td>
</tr>
<tr>
<td class="right">Link to the image:</td><td><input type="text" class="image" name="image1" value="" required></td>
</tr>


</table>
<?php
$sqlquery="SELECT artistId, firstname, lastname from $artists order by 2";
$result = mysqli_query($connect, $sqlquery);
if($result)
    {
        echo "<table class=\"addactor\">";
        echo "<tr>
    <td id=\"text\" colspan=\"2\"><h3>Assign an actor to the movie</h3></td>
    </tr>";
        while  ($sqlRow=mysqli_fetch_array($result, MYSQL_ASSOC)) 
            {


                echo "<tr>";
                echo "<td>";
                echo"<input type=\"checkbox\" name=\"".$sqlRow['artistId']."\" value=\"".$sqlRow['artistId']."\"/> ".$sqlRow['firstname']." ".$sqlRow['lastname']."</td><td><input type=\"text\" name=\"role\"></td>";
                echo "</tr>";



            }

        echo"<tr><td align=\"right\"><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Add\"></td><td><input type=\"reset\" name=\"reset\" id=\"reset\" value=\"Reset\"></td></tr></table>;";

    }
print '</table>';






if ($_POST)
    {
        foreach($_POST as $key=>$value)
            {
                $sqlqr="INSERT INTO $roll (artistId, movieCode, roleDescription) VALUES ('".$sqlRow['artistId']."', '".$_POST['moviecode']."', '".$_POST['role']."')";
            }
        $query = "INSERT INTO $movies(movieCode, title, dateOfIssue, description, image) VALUES ('".$_POST['moviecode']."', '".$_POST['title']."', '".$_POST['dateofissue']."', '".$_POST['desc']."', '".$_POST['image1']."')";


        if (!mysqli_query($connect,$query)||!mysqli_query($connect, $sqlqr))
            {
                die('Error: ' . mysqli_error($connect));
            }
        else
            {
                echo "<h4>1 record added</h4>";
            }



        print '</form>';
    }
    ?>  

如果你能解决这个问题,我将非常感激!

1 个答案:

答案 0 :(得分:1)

将$ roll替换为角色,将$ movies替换为电影并尝试此

INSERT INTO roll (artistId, movieCode, roleDescription) VALUES ('".$sqlRow['artistId']."', '".$_POST['moviecode']."', '".$_POST['role']."')";
$query = "INSERT INTO movies(movieCode, title, dateOfIssue, description, image) VALUES ('".$_POST['moviecode']."', '".$_POST['title']."', '".$_POST['dateofissue']."', '".$_POST['desc']."', '".$_POST['image1']."')";