从textfield和textarea插入sql不起作用

时间:2013-04-28 06:16:17

标签: php mysql sql

请帮助,从我的textfields中插入数据并结合我的textareas到db not working,

请帮助我们如何正确组合我的价值观。我似乎无法弄清楚如何将我的textareas的内爆与textfields中的其他值结合起来。

下面是我的PHP代码:

$Category2 = $_POST['Category2'];
    $Category3 = $_POST['Category3'];
    $Status = $_POST['Status'];
    $Date = $_POST['Date'];
    $Severity = $_POST['Severity'];
    $BanType = $_POST['BanType'];
    $XiD = $_POST['XiD'];
    $Ticket = $_POST['Ticket'];

    //Process the input textareas into arrays
    $PhoneNumber = array_map('mysql_real_escape_string', explode("\n", $_POST['PhoneNumber']));
    $Createdate = array_map('mysql_real_escape_string', explode("\n", $_POST['Createdate']));
    $RemedyTicketNo = array_map('mysql_real_escape_string', explode("\n", $_POST['PhoneNumber']));

    //Determine the values with the least amoutn of elements
    $min_count = min($PhoneNumber, $Createdate, $RemedyTicketNo);

    //Create array to hold INSERT values
    $values = array();

    //Create the INSERT values
    for($index=0; $index<$min_count; $index++)
    {
    $values[] = "('$RemedyTicketNo[$index]','$PhoneNumber[$index]','$Createdate[$index]',
'$Category2','$Category3','$Status','$Date','$Severity','$BanType','$XiD')";     
    }

    $sql=mysql_query("INSERT into tbl_main
    (ars_no,phone_number,create_date,category_1,category_2,status,resolved_date,trouble_type_priority,ban_type,employee_id_name)
    VALUES " . implode (',',$values));
if (!$sql) {
die('Invalid query: ' . mysql_error());
}
    header("Location: smp_backend_test.php");
    }

这是我的HTML代码:这有文本字段,

<td colspan="2"><form id="form1" name="form1" method="post" action="" onsubmit="return validateDBLoad()">
          <table width="435" border="0">
            <tr>
              <td width="115"><div align="left" style="font-size:12px"></div></td>
              <td width="167" align="left">&nbsp;</td>
              <td width="139" align="right"><input type="reset" name="Clear" id="Clear" value="Clear" /></td>
            </tr>
            <tr>
              <td height="24" style="font-size:12px"><strong>Ticket Source:</strong></td>
              <td colspan="2"><select name="Ticket" id="Ticket">
                <option value="tickettracker.php">Please select...</option>
                <option value="wireless_new.php">Wireless - Remedy</option>
                <option value="smp_backend_test.php" selected="selected">SMP - Backend</option>
                </select>
                <input type="submit" value="Go" id="submit"/></td>
            </tr>
            <tr>
              <td height="24" style="font-size:12px"><strong>Date:</strong></td>
              <td colspan="2">
                <label for="datepickerID"></label>
                <input type="text" name="Date" id="Date" /></td>

            </tr>
            <tr>
              <td style="font-size:12px"><strong>XiD, Name:</strong></td>
              <td colspan="2"><span style="font-size:12px">
                <select name="XiD" id="XiD">
                  <option value="Blank" selected="selected">Please Select...</option>
                  <option value="AAA">AAA</option>
                  <option value="BBB">BBB</option>
                  <option value="CCC">CCC</option>
                  </select>
                </span></td>
            </tr>
            <tr>
              <td style="font-size:12px"><strong>Category 2:</strong></td>
              <td colspan="2"><input type="text" name="Category" id="Category" value="SMP_Backend" disabled="disabled" />                <input type="hidden" name="Category2" id="Category2" value="SMP_Backend" />    
                </td>
            </tr>
            <tr>
              <td style="font-size:12px"><strong>Category 3:</strong></td>
              <td colspan="2"><label for="Category3"></label>
                <select name="Category3" id="Category3">
          <option value="Blank" selected="selected">Please Select...</option>      
          <option value="Pending Request">Pending Request</option>
          <option value="Validation Error | Aging">Validation Error|Aging</option>
          <option value="Validation Error | RCM">Validation Error|RCM</option>
          <option value="Validation Error | Lost Stolen">Validation Error|Lost Stolen</option>
          </select></td>
            </tr>
            <tr>
              <td style="font-size:12px"><strong>Status:</strong></td>
              <td colspan="2"><input name="Status1" type="text" id="Status1" value="Resolved" disabled="disabled" />
                 <input name="Status" type="hidden" id="Status" value="Resolved" /> 
              </td>
            </tr>
            <tr>
              <td style="font-size:12px"><strong>Severity:</strong></td>
              <td colspan="2"><input type="text" name="SeverityX" id="SeverityX" value="5" size="5" disabled="disabled"/>
                <input type="hidden" name="Severity" id="Severity" value="5" size="5" />
                </td>
            </tr>
            <tr>
              <td style="font-size:12px"><strong>Ban Type:</strong></td>
              <td colspan="2"><span style="font-size:12px">
                <input type="text" name="BanType2" id="BanType2" value="I" size="5" disabled="disabled"/>
                <input type="hidden" name="BanType" id="BanType" value="I" size="5" />
              </span></td>
            </tr>
            <tr>
              <td style="font-size:12px"><strong>Phone No.:</strong></td>
              <td style="font-size:12px"><strong>Create Date:</strong></td>
              <td style="display:none" >RemedyTicketNo</td>
            </tr>

//这是我的textareas:

            <tr>
              <td style="font-size:12px"><textarea name="PhoneNumber" id="PhoneNumber" cols="15" rows="5"></textarea></td>
              <td><textarea name="Createdate" id="Createdate" cols="25" rows="5"></textarea></td>
              <td style="display:none"><textarea type=hidden name="RemedyTicketNo" id="RemedyTicketNo" cols="15" rows="5"></textarea>
              </td>
            </tr>
            <tr>
              <td style="font-size:12px">&nbsp;</td>
              <td colspan="2"><span style="font-size:12px">
                <input name="DBLoad" type="submit" id="DBLoad" value="DB Load" />
              </span></td>
            </tr>
          </table>
        </form></td>

1 个答案:

答案 0 :(得分:1)

试试这个我编辑了你的代码

<?php 
    if(isset($_POST['DBLoad']))
    {
        $con=mysql_connect('localhost','root','');
        $dbs=mysql_select_db('test',$con);

        //print_r($_POST);
        //die();
        $Category2 = $_POST['Category2'];
        $Category3 = $_POST['Category3'];
        $Status = $_POST['Status'];
        $Date = $_POST['Date'];
        $Severity = $_POST['Severity'];
        $BanType = $_POST['BanType'];
        $XiD = $_POST['XiD'];
        $Ticket = $_POST['Ticket'];

        //Process the input textareas into arrays
        $PhoneNumber = array_map('mysql_real_escape_string', explode("\r\n", $_POST['PhoneNumber']));
        $Createdate = array_map('mysql_real_escape_string', explode("\r\n", $_POST['Createdate']));
        $RemedyTicketNo = array_map('mysql_real_escape_string', explode("\r\n", $_POST['PhoneNumber']));
       //Determine the values with the least amoutn of elements
      $min_count = min(count($PhoneNumber), count($Createdate), count($RemedyTicketNo));

        //Create array to hold INSERT values
        $values = array();

        //Create the INSERT values
        for($index=0; $index<$min_count; $index++)
        {
        $values[$index] = "('{$PhoneNumber[$index]}', '{$Createdate[$index]}', '{$RemedyTicketNo[$index]}')";   
        }

        if (isset($RemedyTicketNo)) 
        {
     $sql="INSERT into tbl_main (phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name) 
            VALUES (\"" . implode (', ', $values) ."\", '".$Category2."', '".$Category3."', '".$Status."', '".$Date."','".$Date."','".$Severity."', '".$BanType."', '".$XiD."') ";
       $result=mysql_query($sql);

       header("Location: smp_backend_test.php");
        }
    }else
    {
    ?>
    <td colspan="2"><form id="form1" name="form1" method="post" action="" >
              <table width="435" border="0">
                <tr>
                  <td width="115"><div align="left" style="font-size:12px"></div></td>
                  <td width="167" align="left">&nbsp;</td>
                  <td width="139" align="right"><input type="reset" name="Clear" id="Clear" value="Clear" /></td>
                </tr>
                <tr>
                  <td height="24" style="font-size:12px"><strong>Ticket Source:</strong></td>
                  <td colspan="2"><select name="Ticket" id="Ticket">
                    <option value="tickettracker.php">Please select...</option>
                    <option value="wireless_new.php">Wireless - Remedy</option>
                    <option value="smp_backend_test.php" selected="selected">SMP - Backend</option>
                    </select>
                    <input type="submit" value="Go" id="submit"/></td>
                </tr>
                <tr>
                  <td height="24" style="font-size:12px"><strong>Date:</strong></td>
                  <td colspan="2">
                    <label for="datepickerID"></label>
                    <input type="text" name="Date" id="Date" /></td>

                </tr>
                <tr>
                  <td style="font-size:12px"><strong>XiD, Name:</strong></td>
                  <td colspan="2"><span style="font-size:12px">
                    <select name="XiD" id="XiD">
                      <option value="Blank" selected="selected">Please Select...</option>
                      <option value="AAA">AAA</option>
                      <option value="BBB">BBB</option>
                      <option value="CCC">CCC</option>
                      </select>
                    </span></td>
                </tr>
                <tr>
                  <td style="font-size:12px"><strong>Category 2:</strong></td>
                  <td colspan="2"><input type="text" name="Category" id="Category" value="SMP_Backend" disabled="disabled" />                <input type="hidden" name="Category2" id="Category2" value="SMP_Backend" />    
                    </td>
                </tr>
                <tr>
                  <td style="font-size:12px"><strong>Category 3:</strong></td>
                  <td colspan="2"><label for="Category3"></label>
                    <select name="Category3" id="Category3">
              <option value="Blank" selected="selected">Please Select...</option>      
              <option value="Pending Request">Pending Request</option>
              <option value="Validation Error | Aging">Validation Error|Aging</option>
              <option value="Validation Error | RCM">Validation Error|RCM</option>
              <option value="Validation Error | Lost Stolen">Validation Error|Lost Stolen</option>
              </select></td>
                </tr>
                <tr>
                  <td style="font-size:12px"><strong>Status:</strong></td>
                  <td colspan="2"><input name="Status1" type="text" id="Status1" value="Resolved" disabled="disabled" />
                     <input name="Status" type="hidden" id="Status" value="Resolved" /> 
                  </td>
                </tr>
                <tr>
                  <td style="font-size:12px"><strong>Severity:</strong></td>
                  <td colspan="2"><input type="text" name="SeverityX" id="SeverityX" value="5" size="5" disabled="disabled"/>
                    <input type="hidden" name="Severity" id="Severity" value="5" size="5" />
                    </td>
                </tr>
                <tr>
                  <td style="font-size:12px"><strong>Ban Type:</strong></td>
                  <td colspan="2"><span style="font-size:12px">
                    <input type="text" name="BanType2" id="BanType2" value="I" size="5" disabled="disabled"/>
                    <input type="hidden" name="BanType" id="BanType" value="I" size="5" />
                  </span></td>
                </tr>
                <tr>
                  <td style="font-size:12px"><strong>Phone No.:</strong></td>
                  <td style="font-size:12px"><strong>Create Date:</strong></td>
                  <td style="display:none" >RemedyTicketNo</td>
                </tr>
                <tr>
                  <td style="font-size:12px"><textarea name="PhoneNumber" id="PhoneNumber" cols="15" rows="5"></textarea></td>
                  <td><textarea name="Createdate" id="Createdate" cols="25" rows="5"></textarea></td>
                  <td style="display:none"><textarea type=hidden name="RemedyTicketNo" id="RemedyTicketNo" cols="15" rows="5"></textarea>
                  </td>
                </tr>
                <tr>
                  <td style="font-size:12px">&nbsp;</td>
                  <td colspan="2"><span style="font-size:12px">
                    <input name="DBLoad" type="submit" id="DBLoad" value="DB Load" />
                  </span></td>
                </tr>
              </table>
            </form></td>



    <?php }?>

简单地检查null验证是否遵循代码,你应该检查更多它只是一个示例如何使用javascript你可以用Jquery做它

 function validateDBLoad () {
        var xid=document.getElementById('XiD').value;
        var PhoneNumber=document.getElementById('PhoneNumber').value;
        var Status=document.getElementById('Status').value;
        var Category2=document.getElementById('Category2').value;
        var Category3=document.getElementById('Category3').value;
        var Createdate=document.getElementById('Createdate').value;
        var Severity=document.getElementById('Severity').value;
        var BanType=document.getElementById('BanType').value;


         if((xid=="Blank") || (PhoneNumber=="") || (Category2) || (Category3=="Blank") || (Status=="") || (Createdate=="") || (Severity=="") || (document.form1.BanType.value=="")) { alert('Please fill up all the fields..'); return false; } else { alert('Ticket upload Successful.'); return true; } }

并按照以下方式制作表格

CREATE TABLE IF NOT EXISTS `tbl_main` (
  `ars_no` int(12) NOT NULL AUTO_INCREMENT,
  `phone_number` varchar(50) NOT NULL,
  `category_1` varchar(50) NOT NULL,
  `category_2` varchar(50) NOT NULL,
  `status` varchar(50) NOT NULL,
  `create_date` varchar(50) NOT NULL,
  `trouble_type_priority` varchar(50) NOT NULL,
  `ban_type` varchar(50) NOT NULL,
  `resolved_date` date NOT NULL,
  `employee_id_name` varchar(50) NOT NULL,
  PRIMARY KEY (`ars_no`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;