使用php将数据插入数据库的问题

时间:2014-05-09 01:50:18

标签: php mysql

我在将数据插入数据库时​​遇到问题,虽然它已成功插入,但插入数据库的数据会变为整数。

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

这是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';
}
?>

2 个答案:

答案 0 :(得分:2)

您要存储整数而不是数据,请检查以下代码:

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

它将变量设置为1,作为isset

的结果

您需要将其更改为:

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

答案 1 :(得分:1)

  

我在将数据插入数据库时​​遇到问题,尽管如此   成功插入但随后将数据插入数据库   变成整数。

是的,因为这是您<option>标签中的内容。例如,以下是“公司名称”的选择列表:

 <?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>';
 ?>

表单传递的数据是$record['ID']而不是$record['NAME']。所以你必须把它改成这样的东西:

 <?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>';
 ?>

请注意我将<option id="0">更改为<option id="">并设置'<option value=" '.$record['NAME'].'">'

另外,你有这个:

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

isset只返回01。所以它只是检查$_POST值是否存在而不是其他。要快速测试,只需将其更改为:

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

但是考虑到代码的重复性,我建议将其缩写为:

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

是的,在PHP 5.3和&amp ;;中扩展了mysql_*个扩展名。 5.4并将在5.5版中删除,因此您应该了解mysqli_*用法。这类似于mysql_*,但由你来处理。