Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我创建的php页面应该向mysql表添加条目,但它没有对表做任何操作_Php_Javascript_Mysql_Html - Fatal编程技术网

我创建的php页面应该向mysql表添加条目,但它没有对表做任何操作

我创建的php页面应该向mysql表添加条目,但它没有对表做任何操作,php,javascript,mysql,html,Php,Javascript,Mysql,Html,首先,我检查了const.php是否位于页面所在的目录中 我正在创建一个页面,允许管理员通过网站向MySQL表添加多个条目。它使用JavaScript来扩展文本框输入字段的数组,这样管理员就不必输入更多的条目。但是如果管理员试图添加的条目已经存在,那么它将不会被添加。运行代码后,会告诉用户哪些条目已添加到表中,哪些条目未添加,因为已经存在这样的条目 下面是将输入数组传递给PHP代码的表单 form id=userform action=“addplayers.php”method=“post”>

首先,我检查了const.php是否位于页面所在的目录中

我正在创建一个页面,允许管理员通过网站向MySQL表添加多个条目。它使用JavaScript来扩展文本框输入字段的数组,这样管理员就不必输入更多的条目。但是如果管理员试图添加的条目已经存在,那么它将不会被添加。运行代码后,会告诉用户哪些条目已添加到表中,哪些条目未添加,因为已经存在这样的条目

下面是将输入数组传递给PHP代码的表单

form id=userform action=“addplayers.php”method=“post”>

玩家信息
  • 玩家
  • 团队
  • 播放器页面路径
  • 播放器图像路径

  • 提交
    在这里,javascript代码动态创建文本框输入字段,以扩展输入数组

    <script language="Javascript" type="text/javascript">
                function addInput(){
                    var newdiv = document.createElement('div');
                    newdiv.innerHTML = "<li><label for=player>Player</label><input id=player type='text' name='player[]'></li>";
                    document.getElementById('dynamicInput').appendChild(newdiv);
                    var newdiv = document.createElement('div');
                    newdiv.innerHTML = "<li><label for=team>Team</label><input id=team type='text' name='team[]'></li>";
                    document.getElementById('dynamicInput').appendChild(newdiv);
                    var newdiv = document.createElement('div');
                    newdiv.innerHTML = "<li><label for=path>Player page path</label><input id=path type='text' name='path[]'></li>";
                    document.getElementById('dynamicInput').appendChild(newdiv);
                    var newdiv = document.createElement('div');
                    newdiv.innerHTML = "<li><label for=image>Player image path</label><input id=image type='text' name='image[]'></li><br/>";
                    document.getElementById('dynamicInput').appendChild(newdiv);
                }
            </script>
    
    
    函数addInput(){
    var newdiv=document.createElement('div');
    newdiv.innerHTML=“
  • Player
  • ”; document.getElementById('dynamicInput').appendChild(newdiv); var newdiv=document.createElement('div'); newdiv.innerHTML=“
  • 团队”
  • ”; document.getElementById('dynamicInput').appendChild(newdiv); var newdiv=document.createElement('div'); newdiv.innerHTML=“
  • 播放器页面路径
  • ”; document.getElementById('dynamicInput').appendChild(newdiv); var newdiv=document.createElement('div'); newdiv.innerHTML=“
  • 播放器图像路径

  • ”; document.getElementById('dynamicInput').appendChild(newdiv); }
    下面是形成POST的php代码。
    include "const.php";
    
    $entry_results = "";
    
    if( isset($_POST['submit']) )
    {
        $conn = mysql_connect(MYSQL_HOST, MYSQL_LOGIN, MYSQL_PASSWORD) or die("Could not connect: " . mysql_error());
        mysql_select_db(MYSQL_DB);
    
        $player = $_POST['player'];
        $team = $_POST['team'];
        $path = $_POST['path'];
        $image = $_POST['image'];
        $invalid = array();
        $valid = array();
        $j = 0;
        $k = 0;
    
        for($i=0; $i<count($player);$i++)
        {
            //Check to see if player is in the database
            $query = "Select name FROM tblPlayers where name = '" . $player[i] ."'";
            $result = mysql_query($query);
            if(!empty($result))//if query gives a result add player to list of invalid entries
            {
                $invalid[$j++] = $player[$i];
            }
            else//otherwise add to database
            {
                $valid[$k++] = $player[$i];
                if(empty($image[$i]))
                    {$image[$i] = '#';}
                if(empty($path[$i]))
                    {$path[$i] = '#';}
                $query = "SELECT entity_id FROM tblTeams WHERE team = '" . $team[$i] . "'";
                $result = mysql_query($query);
                $query = "INSERT INTO tblPlayers ( team_id, name, image, path) VALUES ( 
                    '" . $result . "',
                    '" . $player[$i] . "',
                    '" . $image[$i] . "',
                    '" . $path[$i] . "'
                )";
                $result = mysql_query($query);
            }
        }
        if(!empty($invalid[0]))
        {
            for($i=0;$i<count($invalid);$i++){
                $entry_results  .= $invalid[$i];
                if(($i+1)!=count($invalid))
                    $entry_results .= ', ';
            }
            $entry_results .= "were found in the database and were not enterered to prevent duplicant record. ";
        }
        if(!empty($valid[0]))
        {
            for($i=0;$i<count($valid);$i++){
                $entry_results .= $invalid[$i];
                if(($i+1)!=count($valid))
                    $entry_results .= ', ';
            }
            $entry_results .= "were entered into the players table.";
        }
        mysql_close($conn);
    }
    
    ?>
    
    包括“const.php”;
    $entry_results=“”;
    如果(isset($_POST['submit']))
    {
    $conn=mysql\u connect(mysql\u主机、mysql\u登录、mysql\u密码)或die(“无法连接:”.mysql\u error());
    mysql\u select\u db(mysql\u db);
    $player=$_POST['player'];
    $team=$_POST['team'];
    $path=$_POST['path'];
    $image=$_POST['image'];
    $invalid=array();
    $valid=array();
    $j=0;
    $k=0;
    对于($i=0;$i)
    
    将所有表单元素名称从例如:“player[]”更改为“player”,并在javascript中执行此操作。
    提交表单将自动将具有相同名称的所有元素放入数组。

    您没有正确处理结果集。请看以下代码:

            $query = "SELECT entity_id FROM tblTeams WHERE team = '" . $team[$i] . "'";
            $result = mysql_query($query);
            $query = "INSERT INTO tblPlayers ( team_id, name, image, path) VALUES ( 
                '" . $result . "',
                '" . $player[$i] . "',
                '" . $image[$i] . "',
                '" . $path[$i] . "'
            )";
    
    在第一次查询之后,$result将是一个结果资源,而不是“entity_id”列的值。该代码应重写为:

            $query = "SELECT entity_id FROM tblTeams WHERE team = '" . $team[$i] . "'";
            $result = mysql_query($query);
            if ($row = mysql_fetch_assoc($result)) {
                 $query = "INSERT INTO tblPlayers ( team_id, name, image, path) VALUES ( 
                     '" . $row['entity_id'] . "',
                     '" . mysql_real_escape_string($player[$i]) . "',
                     '" . mysql_real_escape_string($image[$i]) . "',
                     '" . mysql_real_escape_string($path[$i]) . "'
                 )";
            } else {
                 die "Couldn't find entity_id for this team.";
            }
    

    另外,您应该正确地转义数据库查询中使用的所有用户输入。我在上面使用了“mysql\u real\u escape\u string”函数完成了此操作。

    $player[I]在查询中应该是$player[$I]谢谢,我对其进行了修改,但代码仍然没有执行任何操作
            $query = "SELECT entity_id FROM tblTeams WHERE team = '" . $team[$i] . "'";
            $result = mysql_query($query);
            $query = "INSERT INTO tblPlayers ( team_id, name, image, path) VALUES ( 
                '" . $result . "',
                '" . $player[$i] . "',
                '" . $image[$i] . "',
                '" . $path[$i] . "'
            )";
    
            $query = "SELECT entity_id FROM tblTeams WHERE team = '" . $team[$i] . "'";
            $result = mysql_query($query);
            if ($row = mysql_fetch_assoc($result)) {
                 $query = "INSERT INTO tblPlayers ( team_id, name, image, path) VALUES ( 
                     '" . $row['entity_id'] . "',
                     '" . mysql_real_escape_string($player[$i]) . "',
                     '" . mysql_real_escape_string($image[$i]) . "',
                     '" . mysql_real_escape_string($path[$i]) . "'
                 )";
            } else {
                 die "Couldn't find entity_id for this team.";
            }