0

I have a problem inserting data into my database although it is successfully inserted but then the data inserted into the database turn into integers.

<?php
//connect to database
include "databaseconnection.php";   

?>

<body bgcolor="#e5edf8">

<form name="addform" method="post" action="../insert.php">
<table width="1110" height="184" border="0" cellpadding="0" cellspacing="0">
<tr>
  <td width="393" height="28"><strong> TICKET NO :
    <?php
        $sql = mysql_query("SELECT * FROM troubleticket");
        $record = mysql_fetch_array($sql);
        echo '<input type="text" name="ticketnofld" readonly value="    '.$record['ticketno'].' " >'; 
     ?>
  </strong>
  </td>

  <td width="717"> <strong>TECHNICAL NAME :
  <?php 
        echo "<select  name='techname' type='text'>";
        echo '<option id="0">'.'--Select technical Name--'.'</option>';
        $sql = mysql_query("SELECT * FROM technical");
        while($record = mysql_fetch_assoc($sql)) 
               {
                echo '<option value=" '.$record['ID'].'">'.$record['FNAME']. '</option>';
               }        
        echo '</select>';
    ?>

  </strong></td>
</tr>
<tr>
  <td><strong>COMPANY NAME : 
     <?php 
        echo "<select name='companyname' type='text'>";
        echo '<option id="0">'.'--Select Company Name--'.'</option>';
        $sql = mysql_query("SELECT * FROM client");
        while($record = mysql_fetch_assoc($sql)) 
               {
                echo '<option value=" '.$record['ID'].'">'.$record['NAME']. '</option>';
               }        
        echo '</select>';
     ?>
  </select> 
  </strong></td>
  <td><strong>TYPE OF SERVICE :
   <?php 
        echo " <select name='typeofservice' type='text'>";
        echo '<option id="0">'.'--Select type of service--'.'</option>';
        $sql = mysql_query("SELECT * FROM typeofservice");
        while($record = mysql_fetch_assoc($sql)) 
               {
                echo '<option value=" '.$record['tosid'].'">'.$record['typeofservice']. '</option>';
               }        
        echo '</select>';

    ?>
  </strong></td>
</tr>
<tr>
  <td colspan="2"><p><strong>PROBLEM :<textarea type='text' rows="10" cols="100" name="problemfld" id="problemfld"> </textarea>
     </strong></p>
</td>
</tr>
<tr>
  <td colspan="2"><input type="submit" name="Addbutton" value="ADD" >
                  <input type="button" name="Cancelbutton" value="CANCEL" ></td>
</tr>
</table>
</form>

and this is the insert.php

 <?php
 $con=mysql_connect("127.0.0.1","root","","ojt") or die('Could not reach the database'.mysql_error());

 $techname=isset($_POST['techname']);
 $companyname=isset($_POST['companyname']);
 $typeofservice=isset($_POST['typeofservice']);
 $problem=isset($_POST['problemfld']);

 $techname=stripslashes($techname);
 $companyname=stripslashes($companyname);
 $typeofservice=stripslashes($typeofservice);
 $problem=stripslashes($problem);

 $techname=mysql_real_escape_string($techname);
 $companyname=mysql_real_escape_string($companyname);
 $typeofservice=mysql_real_escape_string($typeofservice);
 $problem=mysql_real_escape_string($problem);   

 $sql="INSERT INTO `ojt`.`troubleticket` (`ticketno`, `technicalname`, `services`, `problem`, `companyname`, `remarks`) VALUES (' ','$techname', '$typeofservice', '$problem', '$companyname', 'NEW')";
$query=mysql_query($sql,$con);
if($query)
{
echo '1 Data Added';
}
else
{
echo 'Unsuccessfully Saved';
}
?>
4
  • MySQL is deprecate in PHP 5.5.x, consider switching to MySQLi. Commented May 9, 2014 at 1:53
  • Definitely something to do with isset in $techname=isset($_POST['techname']); etc. Commented May 9, 2014 at 1:54
  • 1
    @Fred-ii- isset returns a boolean, the proper way would be, if(isset($_POST['techname'])) $techname = $_POST['techname']; Commented May 9, 2014 at 2:03
  • 1
    This whole thing could've easily been avoided, including a rewrite if PDO were used or prepared statements. Commented May 9, 2014 at 2:05

2 Answers 2

2

You're storing integers instead of data, check this code:

 $techname=isset($_POST['techname']);
 $companyname=isset($_POST['companyname']);
 $typeofservice=isset($_POST['typeofservice']);
 $problem=isset($_POST['problemfld']);

it sets variables to 1, as result of isset

you need to change it to:

 $techname=isset($_POST['techname']) ? $_POST['techname'] : '';
 $companyname=isset($_POST['companyname']) ? $_POST['companyname'] : '';
 $typeofservice=isset($_POST['typeofservice']) ? $_POST['typeofservice'] : '';
 $problem=isset($_POST['problemfld']) ? $_POST['problemfld'] : '';
Sign up to request clarification or add additional context in comments.

Comments

1

I have a problem inserting data into my database although it is successfully inserted but then the data inserted into the database turn into integers.

Yes, because that is what you have in your <option> tags. For example, here is the select list for “Company Name”:

 <?php 
    echo "<select name='companyname' type='text'>";
    echo '<option id="0">'.'--Select Company Name--'.'</option>';
    $sql = mysql_query("SELECT * FROM client");
    while($record = mysql_fetch_assoc($sql)) 
           {
            echo '<option value=" '.$record['ID'].'">'.$record['NAME']. '</option>';
           }        
    echo '</select>';
 ?>

The data being passed by the form is $record['ID'] and not $record['NAME']. So you would have to change that to something like this:

 <?php 
    echo "<select name='companyname' type='text'>";
    echo '<option id="">'.'--Select Company Name--'.'</option>';
    $sql = mysql_query("SELECT * FROM client");
    while($record = mysql_fetch_assoc($sql)) 
           {
            echo '<option value=" '.$record['NAME'].'">'.$record['NAME']. '</option>';
           }        
    echo '</select>';
 ?>

Note how I changed the <option id="0"> to be <option id=""> and set the '<option value=" '.$record['NAME'].'">'.

Also, you have this:

 $techname=isset($_POST['techname']);
 $companyname=isset($_POST['companyname']);
 $typeofservice=isset($_POST['typeofservice']);
 $problem=isset($_POST['problemfld']);

And isset just returns a 0 or 1. So it is just checking if the $_POST value exists and not much else. To quickly test, just change it to this:

 $techname=$_POST['techname'];
 $companyname=$_POST['companyname'];
 $typeofservice=$_POST['typeofservice'];
 $problem=$_POST['problemfld'];

But looking at the repetitive nature of your code, I would recommend condensing it to this instead:

// Set the database connection.
$con = mysql_connect("127.0.0.1","root","","ojt") or die('Could not reach the database'.mysql_error());

// Set an array of post values.
$post_array = array('techname', 'companyname', 'typeofservice', 'problemfld');

// Roll through the post values, validate & assign them.
foreach ($post_array as $post_key => $post_value) {
  $$post_key = '';
  if (isset($_POST[$post_key])) {
    $$post_key = mysql_real_escape_string(stripslashes($_POST[$post_key]));
  }
}

// Set the query.
$sql = "INSERT INTO `ojt`.`troubleticket` (`ticketno`, `technicalname`, `services`, `problem`, `companyname`, `remarks`) VALUES (' ','$techname', '$typeofservice', '$problemfld', '$companyname', 'NEW')";

// Run the query.
$query = mysql_query($sql,$con);

// Check if the query ran.
if ($query) {
  echo '1 Data Added';
}
else {
  echo 'Unsuccessfully Saved';
}

And yes, mysql_* extensions are depreciated in PHP 5.3 & 5.4 and will be eliminated in version 5.5 so you should learn about mysqli_* usage. Which is similar to mysql_*, but up to you to handle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.