php - 确认是否插入MySql数据库

标签 php mysql forms

我有一个 PhP 注册表,将各种条目提交到 MySQL 数据库中。

代码如下:

if($conn)
    {

    $sql = "INSERT INTO Participants
       (ID,
        title, 
        surname,
        name,
        organization1,
        organization2,
        address1,
        address2,
        country,
        email,
        phone,
        type_reg,
        abstract,
        bm1,
        bm2,
        bm3,
        bm4                 )

        VALUES         
       ('$auth_id',
        '$auth_title', 
        '$auth_sname',
        '$auth_name',
        '$auth_org_line1',
        '$auth_org_line2',
        '$auth_add_line1',
        '$auth_add_line2',
        '$auth_country',
        '$auth_email',
        '$auth_phonen',
        '$reg_type',
        '$new_file_name',
        '$bm1',
        '$bm2',
        '$bm3',
        '$bm4'          )";


    mysql_query($sql);
    if( mysql_errno() !== 1062) {
        print '<script type="text/javascript">'; 
        print 'alert("Your pre-registration has been submitted successfully. You will receive a confirmation e-mail soon.")';
        print '</script>';
        print '<script type="text/javascript">'; 
        print 'parent.location.href = "Success.html"';
        print '</script>';
mysql_close($conn);
    }
    else
    {
    echo 'Data base error. Try later.';
    }

因为我正在向我发送带有抄送的确认电子邮件,所以我知道注册的人,当我检查数据库时,我发现其中一些人没有被插入到数据库中。

我正在寻找一种方法,仅在条目插入 MySQL 表时验证注册。

有没有简单的方法?

我应该验证查询是否成功吗?怎么办?

我应该查找 table 上的 ID 并验证它是否存在吗?如何? (条目ID设置为唯一)

最佳答案

是的。 mysql_affected_rows() 将返回受查询影响的行数。如果为零,则未插入。如果 >= 1,则为。

所以:

if ( mysql_affected_rows() >= 1 ){/* 插入!现在做点什么... */}

如果您使用自动递增列作为行 ID,则也可以使用 mysql_insert_id():

if ( mysql_insert_id() > 0 ) {/* 已插入!现在做点什么... */}

关于php - 确认是否插入MySql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16803390/

相关文章:

php - 如何编写 REST API?

c# 将新对象添加到对象上下文返回 "error occurred while updating the entries",主键的重复条目

mysql - 当分配的值不在枚举中时,枚举的默认值

c# - 如何从 Windows 应用程序 C# 发布 word 文件

javascript - 在 <form> 外部显示数据

PHP XPath。如何返回带有html标签的字符串?

javascript - 如何从firebase存储中获取所有下载网址?

php - Client-Server-Communication with php and android

mysql - 以某种语言获取行的 SQL 查询(使用默认语言)

javascript - 没有提交按钮和 anchor 链接 onclick 重定向并输入值的 html 表单