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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么