我正在尝试创建一个基本的表格填充,您可以在其中添加一个季节或新条目,并将其发布到数据库中,而我似乎一生都无法弄清楚。当我运行它时,我得到的是新赛季的“添加更多剧集”按钮不起作用,并且赛季标题位于错误的位置。

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // create a variable
    $title=$_POST['Title'];
    $seasonNum=$_POST['SeasonNum'];


    //Execute the query
    $connect = mysqli_connect('localhost','root','123','mydatabase');
    $sql = "INSERT INTO $title(SeasonNum) $SeasonNum(myInputs[])";
    mysqli_query($connect, $sql);

    if(!$connect) {
        die 'Failed to connect because ' . mysqli_connect_errno();
    }
}

?>

<!DOCTYPE html>
<html>
<head></head>
<body>
<span id="responce"></span>
<h2>Season 1</h2>
<form method="post" action="">
<div id="dynamicInput">
      Entry 1<br><input type="text" name="myInputs[]">
</div>
 <br>
<input type="button" value="Add Another Episode"onClick="addInput('dynamicInput');">

<input type="button" value="Add Another Season" onClick="addSeason('dynamicSeason');">

<div id= "dynamicSeason">

</div>

<input type="submit" value="Add Show">
</form>
<script src="script.js" charset="utf-8"></script>
</body>
</html>


JavaScript文件(script.js)

var counter = 1;
 var limit = 3;
 var EpisodeAdd = 2;
 x=1;
 function addInput(divName){

       var newdiv = document.createElement('div');
       newdiv.innerHTML = "Entry " + (counter + 1) + "<br><input type='text' name='myInputs[]'>";
       document.getElementById(divName).appendChild(newdiv);
       counter++;

 }

 function addSeason(divName){
       i=1;
       x++;
       var h1 = document.createElement("h1");
     var h1Text = document.createTextNode("Season " + x);
     h1.appendChild(h1Text);
     document.body.appendChild(h1)
       while(i>0){
         var newdiv = document.createElement('div');
         newdiv.innerHTML = "h1Text Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'><br><input type='button' value='Add Another Episode' onClick='addInput('Dynamic'EpisodeAdd);'>";
         document.getElementById(divName).appendChild(newdiv);
         i--;
         EpisodeAdd++;
       }
       counter++;


 }

最佳答案

第一个问题:您在这里缺少逗号。注意更改。另外,“ myInputs []”也没有意义。不知道您在那做什么。

mysqli_query($connect, "INSERT INTO " . $title(SeasonNum) .
        $SeasonNum(myInputs[]));


另外,您还有很多乱七八糟的东西。首次加载脚本时,您无法在脚本顶部执行插入查询。它仅应在提交表单时执行。因此,将其包装在if语句中:

if($_POST['submit']){
$title=$_POST['Title'];
$seasonNum=$_POST['SeasonNum'];
mysqli_query($connect"INSERT INTO $title(SeasonNum)
            $SeasonNum(myInputs[]);

$connect=mysqli_connect('localhost','root','123','mydatabase');

if(mysqli_connect_errno($connect))
{
    echo 'Failed to connect';
}
}


可能还会有其他问题,但是直到您弄清基础知识,我才知道它们是什么。我建议先整理一下,然后在您接近解决方案时发布一个新问题。

10-07 18:26