carlo yap
carlo yap

Reputation: 79

inserting to sql from textfield and textarea doesn't work

please help, inserting datas from my textfields combined with my textareas to db not working,

please help on how to combine my VALUES properly. I cant seem to figure out how to combine the implode for my textareas and the other values from textfields.

below is my PHP code:

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

here is my html code: this has the textfields,

<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>

//here are my 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>

Upvotes: 2

Views: 657

Answers (1)

Yadav Chetan
Yadav Chetan

Reputation: 1894

try this i edited your code

<?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 }?>

for simply checking validation for null follow the code and you should check more it just a sample how you can do with javascript you can do it with Jquery too

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

and make your table as follow

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 ;

Upvotes: 1

Related Questions