PHP 表单转换为 MSSQL 2005,通过(更改表添加)自动生成 ID

发布于 2024-11-04 11:01:54 字数 1449 浏览 3 评论 0原文

有人可以帮忙吗?我确实获得了与数据库的连接,并且可以检索数据并“发布”,唯一的断点是主要且必需的 ContactID 记录。我相信它必须与我的 Alter 表行做一些事情,我只是不知道在哪里。

场景如下:

  • 表单是用 php 编写的。
  • 表单正在向MSSQL 2005发送数据
  • ContactID不能为空,需要自动分配。
  • 提交时我收到异常:字符串或二进制数据将是 被截断。该声明已终止。

这是 php 代码:

    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>";
        }
    }}

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>";
        }
    }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2024-11-11 11:01:54

你的例外是:

字符串或二进制数据将被截断。该声明已终止。

看起来您的 ALTER 字符串实际上被放入 LASTNAME 列中。 SQL Server 告诉您,您的长字符串已被截断为 LASTNAME 列的长度。

您基本上是向数组提供 4 个字符串值。前 3 个被接受作为语句的参数。一个或多个不适合它试图放入表中的列的长度。

返回查看这些列的 varchar() 定义。它们比提供的琴弦小吗?

使用这个代替:

  $params = array(&$_POST['lastName'],
                  &$_POST['firstName'],
                  &$_POST['emailAddress']);

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']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文