列计数与第1行的值计数不匹配_数据库插入错误

时间:2019-02-10 07:19:28

标签: php mysql function insert

从事此工作一段时间。由于某些原因,即使我有足够的值和列,也会收到“列数与第1行的值数不匹配”的错误。

我尝试将$ values传递为数组,因为它的数组,所以不起作用。 尝试将$ values和$ kk放入''中,但没有用。 此版本的代码产生的错误最少。

<?php



    $dbh = new mysqli("localhost", "admin", "password", "database");

    $dta= mysqli_query($dbh,"SELECT * FROM agents");

    include_once("class.php");

    $agntProperties = new agntObjects;

    $ary = array ($agntProperties->agntID=$_GET['id'] => array(
    $agntProperties->agntFName=$_GET['fname'],//error occurs"unknown column in field list
    $agntProperties->agntMiddleInitial=$_GET['mname'],
    $agntProperties->agntLastname = $_GET['lname'],
    $agntProperties->agntBusPhone = $_GET['agntbusphone'],
    $agntProperties->agntEmail = $_GET['email'],
    $agntProperties->agntPosition = $_GET['agntpos'],
    $agntProperties->agencyId = $_GET['agencyId']));

    $arryy = array ((int)$_GET['id'],$_GET['fname'],$_GET['mname'],$_GET['lname'],
    $_GET['agntbusphone'],$_GET['email'],$_GET['agntpos'],(int)$_GET['agencyId']);







function insertDta(array $agntArray, $database, $table)
{
    $agntProperties = $GLOBALS['agntProperties'];

    $database=$GLOBALS['dbh'];//it should be something that changes depending on agent


    $agntdata= get_class($agntProperties)::$fields; // $fields properties are below for reference

    //public static $fields = array ('AgentId','AgtFirstName', 'AgtMiddleInitial', 'AgtLastName','AgtBusPhone','AgtEmail', 'AgtPosition','AgencytId')

    $agntprops= implode(',', $agntdata);

    $vals= array();

    foreach($agntArray as $k=>$v)
    {
        $kk=$k;
        $vals= $v;        

    }

    $values= implode(',',$vals);


    $sql = "INSERT INTO agents ($agntprops) VALUES ($kk,'$values')";


    $result= mysqli_query($database,$sql);

    if($result)
    {
        echo "all good";
    }else {
        echo "update failed"."<br>";
        echo  mysqli_error($database);
    }


}


    insertDta($ary,"", "");

    $dbh-> close();


?>

预期结果将是:能够将所有值(例如agentid,firstname等)插入数据库。

0 个答案:

没有答案