dongqi8114 2011-12-07 11:32
浏览 36

如果选中复选框,则在多个表中插入数据

I am creating a user registration form with simple requirements.and insert data with simple query

<?php

if (isset($_POST) && isset($_POST["form_register"]))
{
    $insert_query = "INSERT INTO users SET
                        users.first_name='" . mysql_real_escape_string($_POST['fname']) . "',
                        users.last_name='" . mysql_real_escape_string($_POST['lname']) . "',
                        users.email='" . mysql_real_escape_string($_POST['email']) . "',
                        users.password='" . mysql_real_escape_string($_POST['password']) . "';";

    if (mysql_query($insert_query))
    {
        $_SESSION['messageType'] = "success_msg";
    }
    else
    {
        $_SESSION['message'] = "-Registration not Successful.";
        $_SESSION['messageType'] = "error_msg";
    }
}
?>

but now I have 3 extra fields in this form. If I select a checkbox then the other 2 field data go in another table with having this data also. how can i do that?

It is my old code...now I add 3 new columns, 1 checkbox and 2 text boxes.

The query is, if checkbox is selected then other 2 colums values go in another table and if checkbox is not select then working 1 query.

  • 写回答

2条回答 默认 最新

  • dt246813579 2011-12-07 11:40
    关注

    We really need to see your form as what you're providing us is way too vague, also, check the ending syntax for your $insert_query.

    If I comprehended the question right, the final code should look something like this.

    Tell me if I'm wrong

    <?php 
    if(isset($_POST) && isset ($_POST["form_register"])){
        $insert_query1 = "INSERT INTO users SET
            first_name='".mysql_real_escape_string($_POST['fname'])."',
            last_name='".mysql_real_escape_string($_POST['lname'])."',
            email='".mysql_real_escape_string($_POST['email'])."',
            password='".mysql_real_escape_string($_POST['password'])."'";
    
        if(mysql_query($insert_query)){
            $_SESSION['messageType'] = "success_msg";
        } else {
            $_SESSION['message']  = "-Registration not Successful.";
            $_SESSION['messageType'] = "error_msg";
        }
    
        if($_POST['checkbox']) {
            $insert_query2 = ""; //Put your second MYSQL Query here
        }
    
        if(mysql_query($insert_query2)){
            $_SESSION['messageType2'] = "success_msg";
        } else {
            $_SESSION['message2']  = "-Registration not Successful.";
            $_SESSION['messageType2'] = "error_msg";
        }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据