doujing5937 2015-03-27 17:10
浏览 18
已采纳

PHP表单在MySQL中写入一个空行

My PHP form is writing a blank row in my MYSQL database.

Here is my code. What am I doing wrong? I am frustrated at this point. When I hit submit, it's creating the row within the database, but there is no data.

<?php

$servername = "localhost";
$username = "blah";
$password = "blah2";
$database = "blah3";

$vdesc = $_POST['desc'];
$vproductname = $_POST['productname'];
$vproductver = $_POST['productver'];
$vtypeofhard = $_POST['typeofhard'];
$vosname = $_POST['osname'];
$vfreqofocc = $_POST['freqofocc'];
$vsolution = $_POST['solution'];

mysql_connect($servername,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO recordofbugs VALUES('','$vdesc','$vproductname','$vproductver',
'$vtypeofhard','$vosname','$vfreqofocc','$vsolution')";
mysql_query($query);
mysql_close();
?>

<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="buglistcss.css">
<title>Bug List</title>
</head>

<body>
<h1>Bug List</h1>
<?php $servername = "localhost";
$username = "blah";
$password = "blah1";
$database = "blah2";
mysql_connect($servername,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM recordofbugs";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();?>
<div id="data">
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td>
<font face="Arial, Helvetica, sans-serif">Description | </font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Product Name | </font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Product Version | </font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Type of Hardware | </font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Operating system | </font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Frequency of Occurence | </font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Proposed Solution</font>
</td>
</td>
</tr>
<?php $i=0;while ($i < $num) {$f1=mysql_result($result,$i,"desc");
$f2=mysql_result($result,$i,"productname");$f3=mysql_result($result,$i,"productver");
$f4=mysql_result($result,$i,"typeofhard");$f5=mysql_result($result,$i,"osname");$f6=mysql_result($result,$i,"freqofocc");$f7=mysql_result($result,$i,"solution");?>
<tr>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font>
</td>
</tr>
</table>
<?php $i++;}?>
</div>
<h1>New Bug Report</h1><br>
<form action="buglist.php" method="post">
<p>
(*)Bug Description: <input class="field" type="text" name="desc"><br>
(*)Product Name: <input class="field" type="text" name="productname"><br>
(*)Product Version: <input class="field" type="text" name="productver"><br>
Type of Hardware: <input class="field" type="text" name="typeofhard"><br>
Operating System: <input class="field" type="text" name="osname"><br>
Frequency of occurence: <input class="field" type="text" name="freqofocc"><br>
Proposed Solution: <input class="field" type="text" name="solution"><br><br>
<input class="submit" type="submit" name="formSubmit" value="Submit">
</p>
</form>


</body>
</html>
  • 写回答

3条回答 默认 最新

  • doufengsui7449 2015-03-27 18:08
    关注

    I would change the first variable in your insert to be null instead of '', ie

    "INSERT INTO recordofbugs VALUES(null,'$vdesc','$vproductname','$vproductver', '$vtypeofhard','$vosname','$vfreqofocc','$vsolution')"
    

    I would also suggest you add your column names.

    Anyway, I would output your query any verify the data is there. I don't see anything in your script that would cause the data to be blank, but worth double checking.

    Also make sure you don't have any mysql errors, but I would imagine if you are seeing errors you wouldn't being see a blank row.

    Like I said I don't see anything majorly wrong that would result in the php script not seeing the submitted form data, but perhaps export your table schema so we can see what that looks like. I'm thinking that is where the problem lies

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?