2014-05-09 78 views
0

虽然插入数据成功,但插入数据库的数据变成了整数,但我的数据库中插入数据时出现问题。使用php插入数据到数据库的问题

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

什么是您的数据库结构?你确定这些字段有文字吗? – JakeGould

+0

MySQL在PHP 5.5.x中已弃用,请考虑切换到MySQLi。 – OPatel

+0

肯定与'$技术= isset($ _ POST ['技术'])中'isset'有关;'等等 –

回答

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'] : ''; 
+0

我明白了......非常感谢你:D – user3618728

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

是的,mysql_*扩展贬值在PHP 5.3 & 5.4中,并且将在5.5版中被淘汰,因此您应该了解mysqli_*的用法。这与mysql_*类似,但由您来处理。

+0

我明白了..非常感谢你:D – user3618728