dqqlziv195281 2011-05-02 21:57
浏览 104
已采纳

PHP表单到MSSQL 2005,自动生成ID(alter table add)

Can someone please help? I do get connection to db and can retrieve data and "post", the only break point is ContactID record which is primary and required. I believe it has to do something with my Alter table line, I just do not know where.

Here is the scenario:

  • Form is written in php.
  • Form is posting data to MSSQL 2005
  • ContactID can not be null and need to be assigned automatically.
  • On submit I receive the exception: String or binary data would be truncated. The statement had been terminated.

Here is the php code:

    if(isset($_GET['action']))
    {

    if($_GET['action'] == 'add')

    {
    // this is where inserting data beggins

    $insertSql = "INSERT INTO sys.CONTACT (LASTNAME, FIRSTNAME, EMAIL)

    VALUES (?,?,?)";

    $params = array(("Alter table sys.CONTACT add ProgramID int IDENTITY(9000001,1) NOT NULL, CONTACTID  AS ('CCRMS'+CONVERT(varchar(7),ProgramID,(0)))"),
        &$_POST['lastName'],
        &$_POST['firstName'],
        &$_POST['emailAddress']);

    $stmt = sqlsrv_query($conn, $insertSql, $params);

    if($stmt === false)

    {/*Handle the case of a duplicte e-mail address.*/

        $errors = sqlsrv_errors();

            if($errors[0]['code'] == 2601)

            {
                echo "The e-mail address you entered has already been used.</br>";
            }

            /*Die if other errors occurred.*/

            else
            {
                die(print_r($errors, true));
            }
     }

     else

        {
            echo "Registration complete.</br>";
        }
    }}
  • 写回答

1条回答 默认 最新

  • duandai0373 2011-05-02 22:12
    关注

    Your exception is:

    String or binary data would be truncated. The statement had been terminated.

    It appears your ALTER string is actually being placed into the LASTNAME column. SQL Server is telling you that your long string has been truncated to the length of the LASTNAME column.

    You're basically supplying 4 string values to the array. The first 3 are being accepted as args to the statement. One or more doesn't fit the length of the column it's trying to be fit into on the table.

    Check back on the varchar() definitions of those columns. Are they smaller than the strings that are being supplied?

    Use this instead:

      $params = array(&$_POST['lastName'],
                      &$_POST['firstName'],
                      &$_POST['emailAddress']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line