使用单选按钮从动态注册表单中插入数据 [英] Insert Data from a dynamic registration form with radio buttons

查看:22
本文介绍了使用单选按钮从动态注册表单中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从表单中将数据插入到我的数据库中,当我选择一种类型(医生或患者)时,将数据发送到相应的表中.

这是代码

  帐户已创建请登录.

");}}}}?><头><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><身体><form method="post" action="signup.php"><字段集><legend>注册表</legend><标签>名字<input type="text" name="fname" required="required"/><br/><br/><标签>姓氏<input type="text" name="lname" required="required"/><br/><br/><标签>用户名<input type="text" name="username" required="required"/><br/><br/><标签>电子邮件<input type="text" name="email" required="required"/><br/><br/><标签>密码<input type="text" name="password" required="required"/><br/><br/>用户类型:<br/>医生<input type="radio" name="answer" value="Doctor"/>病人<input type="radio" name="answer" value="Patient"/><!--医生选项--><div id="expandDoctor" style="display:none;"><label id="Male">Male</label><input type="radio" name="DoctorG" value="male" id="DoctorG"><label id="Female">Female</label><input type="radio" name="DoctorG" value="female" id="DoctorG"><br/><br/><label id="Age">年龄:</label><input type="text" name="DoctorAge" id="DoctorAge"/><br/><br/><label id="专长">专长:</label><select id="SelectSpecialty" name="specialty"><option value="A">A</option><option value="B">B</option><option value="C">C</option><option value="D">D</option></选择><br/><br/><label id="ID">医生 ID:</label><input type="text" name="Doctor_ID" id="Doctor_ID"/>

<!--患者选项--><div id="expandPatient" style="display:none;"><label id="Male">Male</label><input type="radio" name="PatientG" value="male" id="PatientGM"><label id="Female">Female</label><input type="radio" name="PatientG" value="female" id="PatientGF"><br/><br/><label id="Age">年龄:</label><input type="text" name="PatientAge" id="PatientAge"/><br/><br/><label id="疾病">疾病:</label><select id="SelectDisease" name="专科"><option value="A">A</option><option value="B">B</option><option value="C">C</option><option value="D">D</option></选择><br/><br/><label id="SPID">SPID:</label><input type="text" name="PatientSPID" id="PatientSPID"/>

</fieldset><input type="submit" value="Register" name="register"/></表单><脚本>$("input[type='radio'][name='answer']").change(function() {$("[id^=expand]").hide();$("#expand" + $(this).val()).show();});</html>

我收到这个错误注意:Undefined offset: 0 in E:\xampp\htdocs\ptixiaki\signup.php on line 21

PS:我没有所有需要的插入物,我只是想为医生测试一下,如果对他们有用,我也会为患者做

解决方案

错误是我应该有 if( $_POST["answer"] === "Doctor" ) 而不是 if($_POST["answer"=== "Doctor"]) 然后它显示了 INSERT INTO 中的第二个错误,它是 Doctor_ID 中的下划线

这是新代码....

  帐户已创建请登录.

");}}}}?><头><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><身体><form method="post" action="signup.php"><字段集><legend>注册表</legend><标签>名字<input type="text" name="fname" required="required"/><br/><br/><标签>姓氏<input type="text" name="lname" required="required"/><br/><br/><标签>用户名<input type="text" name="username" required="required"/><br/><br/><标签>电子邮件<input type="text" name="email" required="required"/><br/><br/><标签>密码<input type="text" name="password" required="required"/><br/><br/>用户类型:<br/>医生<input type="radio" name="answer" value="Doctor"/>病人<input type="radio" name="answer" value="Patient"/><!--医生选项--><div id="expandDoctor" style="display:none;"><label id="Male">Male</label><input type="radio" name="DoctorG" value="male" id="DoctorG"><label id="Female">Female</label><input type="radio" name="DoctorG" value="female" id="DoctorG"><br/><br/><label id="Age">年龄:</label><input type="text" name="DoctorAge" id="DoctorAge"/><br/><br/><label id="专长">专长:</label><select id="SelectSpecialty" name="specialty"><option value="A">A</option><option value="B">B</option><option value="C">C</option><option value="D">D</option></选择><br/><br/><label id="ID">医生 ID:</label><input type="text" name="Doctor_ID" id="Doctor_ID"/>

<!--患者选项--><div id="expandPatient" style="display:none;"><label id="Male">Male</label><input type="radio" name="PatientG" value="male" id="PatientGM"><label id="Female">Female</label><input type="radio" name="PatientG" value="female" id="PatientGF"><br/><br/><label id="Age">年龄:</label><input type="text" name="PatientAge" id="PatientAge"/><br/><br/><label id="疾病">疾病:</label><select id="SelectDisease" name="专科"><option value="A">A</option><option value="B">B</option><option value="C">C</option><option value="D">D</option></选择><br/><br/><label id="SPID">SPID:</label><input type="text" name="PatientSPID" id="PatientSPID"/>

</fieldset><input type="submit" value="Register" name="register"/></表单><脚本>$("input[type='radio'][name='answer']").change(function() {$("[id^=expand]").hide();$("#expand" + $(this).val()).show();});</html>

I want to insert data to my database from a form and when i select a type (doctor or patient) to send the data to the appropriate table.

HERE IS THE CODE

    <?php



    include_once 'header.php';


    if (isset($_SESSION['username'])) destroySession();
    if(isset($_POST['register']))
    {
    if (isset($_POST['username']))
    {
        $fname = sanitizeString($_POST['fname']);
        $lname = sanitizeString($_POST['lname']);
        $username = sanitizeString($_POST['username']);
        $email = sanitizeString($_POST['email']);
        $password = sanitizeString($_POST['password']);
        if($_POST["answer" === "Doctor"])
        {
            $DoctorG = sanitizeString($_POST['DoctorG']);
            $DoctorAge = ($_POST['DoctorAge']);
            $specialty = sanitizeString($_POST['specialty']);
            $Doctor_ID = ($_POST['Doctor_ID']);

                 if (mysql_num_rows(queryMysql("SELECT * FROM doctor
                      WHERE username='$username'")))
                    $error = "That username already exists<br /><br />";
                else
                  {
                    queryMysql("INSERT INTO doctor (fname,lname,username,email,password,gender,age,specialty,doctorID) VALUES('$fname','$lname','$username','$email', '$password','$DoctorG','$DoctorAge','$specialty','$DoctorID')");
                    die("<h4>Account created</h4>Please Log in.<br /><br />");
                  }
        }




    }
    }
    ?>
    <html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<form method="post" action="signup.php">
<fieldset>
    <legend>Registration Form</legend>
    <label>First Name
        <input type="text" name="fname" required="required" />
    </label>
    <br/>
    <br/>
    <label>Last Name
        <input type="text" name="lname" required="required" />
    </label>
    <br />
    <br />
    <label>Username
        <input type="text" name="username" required="required" />
    </label>
    <br />
    <br />
    <label>Email
        <input type="text" name="email" required="required" />
    </label>
    <br />
    <br />
    <label>Password
        <input type="text" name="password" required="required" />
    </label>
    <br/><br/>
 User Type:
  <br/>Doctor
  <input type="radio" name="answer" value="Doctor" />
  Patient
  <input type="radio" name="answer" value="Patient" />

  <!--DOCTOR OPTIONS -->
  <div id="expandDoctor" style="display:none;">
    <label id="Male">Male</label>
    <input type="radio" name="DoctorG" value="male" id="DoctorG">
    <label id="Female">Female</label>
    <input type="radio" name="DoctorG" value="female" id="DoctorG">
    <br/>
    <br/>
    <label id="Age">Age:</label>
    <input type="text" name="DoctorAge" id="DoctorAge" />
    <br/>
    <br/>
    <label id="Specialty">Specialty:</label>
    <select id="SelectSpecialty" name="specialty">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
    </select>
    <br/>
    <br/>
    <label id="ID">Doctor ID:</label>
    <input type="text" name="Doctor_ID" id="Doctor_ID" />
  </div>

  <!--PATIENT OPTIONS -->
  <div id="expandPatient" style="display:none;">
    <label id="Male">Male</label>
    <input type="radio" name="PatientG" value="male" id="PatientGM">
    <label id="Female">Female</label>
    <input type="radio" name="PatientG" value="female" id="PatientGF">
    <br/>
    <br/>
    <label id="Age">Age:</label>
    <input type="text" name="PatientAge" id="PatientAge" />
    <br/>
    <br/>
    <label id="Disease">Disease:</label>
    <select id="SelectDisease" name="specialty">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
    </select>
    <br/>
    <br/>
    <label id="SPID">SPID:</label>
    <input type="text" name="PatientSPID" id="PatientSPID" />
  </div>
</fieldset>

<input type="submit" value="Register" name="register"/>
</form>
</body>
<script>
$("input[type='radio'][name='answer']").change(function() {
  $("[id^=expand]").hide();
  $("#expand" + $(this).val()).show();
});</script>

</body>
</html>

I get this error Notice: Undefined offset: 0 in E:\xampp\htdocs\ptixiaki\signup.php on line 21

PS: I dont have all the needed inserts , I just want to test it for the Doctor and if it works for them I will do for the patient also

解决方案

The error was that i should have if( $_POST["answer"] === "Doctor" ) instead of if($_POST["answer" === "Doctor"]) and then it shows a second error in the INSERT INTO which was an underscore in the Doctor_ID

HERE IS THE NEW CODE....

 <?php

    /**
     * @author Nick Bourlai
     * @copyright 2014
     */

    include_once 'header.php';


    if (isset($_SESSION['username'])) destroySession();
    if(isset($_POST['register']))
    {
    if (isset($_POST['username']))
    {
        $fname = sanitizeString($_POST['fname']);
        $lname = sanitizeString($_POST['lname']);
        $username = sanitizeString($_POST['username']);
        $email = sanitizeString($_POST['email']);
        $password = sanitizeString($_POST['password']);
        if( $_POST["answer"] === "Doctor" )
        {
            $DoctorG = sanitizeString($_POST['DoctorG']);
            $DoctorAge = ($_POST['DoctorAge']);
            $specialty = sanitizeString($_POST['specialty']);
            $Doctor_ID = ($_POST['Doctor_ID']);

                 if (mysql_num_rows(queryMysql("SELECT * FROM doctor
                      WHERE username='$username'")))
                    $error = "That username already exists<br /><br />";
                else
                  {
                    queryMysql("INSERT INTO doctor (fname,lname,username,email,password,gender,age,specialty,doctorID) VALUES('$fname','$lname','$username','$email', '$password','$DoctorG','$DoctorAge','$specialty','$Doctor_ID')");
                    die("<h4>Account created</h4>Please Log in.<br /><br />");
                  }
        }




    }
    }
    ?>
    <html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </head>
    <body>
    <form method="post" action="signup.php">
    <fieldset>
        <legend>Registration Form</legend>
        <label>First Name
            <input type="text" name="fname" required="required" />
        </label>
        <br/>
        <br/>
        <label>Last Name
            <input type="text" name="lname" required="required" />
        </label>
        <br />
        <br />
        <label>Username
            <input type="text" name="username" required="required" />
        </label>
        <br />
        <br />
        <label>Email
            <input type="text" name="email" required="required" />
        </label>
        <br />
        <br />
        <label>Password
            <input type="text" name="password" required="required" />
        </label>
        <br/><br/>
     User Type:
      <br/>Doctor
      <input type="radio" name="answer" value="Doctor" />
      Patient
      <input type="radio" name="answer" value="Patient" />

      <!--DOCTOR OPTIONS -->
      <div id="expandDoctor" style="display:none;">
        <label id="Male">Male</label>
        <input type="radio" name="DoctorG" value="male" id="DoctorG">
        <label id="Female">Female</label>
        <input type="radio" name="DoctorG" value="female" id="DoctorG">
        <br/>
        <br/>
        <label id="Age">Age:</label>
        <input type="text" name="DoctorAge" id="DoctorAge" />
        <br/>
        <br/>
        <label id="Specialty">Specialty:</label>
        <select id="SelectSpecialty" name="specialty">
          <option value="A">A</option>
          <option value="B">B</option>
          <option value="C">C</option>
          <option value="D">D</option>
        </select>
        <br/>
        <br/>
        <label id="ID">Doctor ID:</label>
        <input type="text" name="Doctor_ID" id="Doctor_ID" />
      </div>

      <!--PATIENT OPTIONS -->
      <div id="expandPatient" style="display:none;">
        <label id="Male">Male</label>
        <input type="radio" name="PatientG" value="male" id="PatientGM">
        <label id="Female">Female</label>
        <input type="radio" name="PatientG" value="female" id="PatientGF">
        <br/>
        <br/>
        <label id="Age">Age:</label>
        <input type="text" name="PatientAge" id="PatientAge" />
        <br/>
        <br/>
        <label id="Disease">Disease:</label>
        <select id="SelectDisease" name="specialty">
          <option value="A">A</option>
          <option value="B">B</option>
          <option value="C">C</option>
          <option value="D">D</option>
        </select>
        <br/>
        <br/>
        <label id="SPID">SPID:</label>
        <input type="text" name="PatientSPID" id="PatientSPID" />
      </div>
    </fieldset>

    <input type="submit" value="Register" name="register"/>
    </form>
    </body>
    <script>
    $("input[type='radio'][name='answer']").change(function() {
      $("[id^=expand]").hide();
      $("#expand" + $(this).val()).show();
    });</script>

    </body>
    </html>

这篇关于使用单选按钮从动态注册表单中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆