我的PHP表单在我的MYSQL数据库中写了一个空行。
这是我的代码。我究竟做错了什么?在这一点上我很沮丧。当我点击提交时,它会在数据库中创建行,但没有数据。
<?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>
答案 0 :(得分:0)
我会将插入内容中的第一个变量改为null
而不是''
,即
"INSERT INTO recordofbugs VALUES(null,'$vdesc','$vproductname','$vproductver', '$vtypeofhard','$vosname','$vfreqofocc','$vsolution')"
我还建议您添加列名。
无论如何,我会输出你的查询,验证数据是否存在。我在脚本中看不到会导致数据空白的任何内容,但值得仔细检查。
另外请确保您没有任何mysql错误,但我想如果您看到错误,您将不会看到空行。
就像我说的那样,我没有看到任何会导致php脚本看不到提交的表单数据的重大错误,但可能会导出您的表架构,以便我们可以看到它的样子。我认为这就是问题所在
答案 1 :(得分:0)
这是我导出到csv文件时得到的结果。 (不会让我发布图片)我也列出了数据库的结构。感谢所有回复!
;; “0” ;;;;多次我尝试使用表单输入数据。
1 desc text latin1_swedish_ci否无
2产品名称文本latin1_swedish_ci否无
3 productver int(11)否无
4 typeofhard text latin1_swedish_ci否无
5 osname文本latin1_swedish_ci否无
6 freqofocc文本latin1_swedish_ci否无
7解决方案文本latin1_swedish_ci
否无
答案 2 :(得分:0)
尝试var_dump($_POST)
并检查是否所有值都正确传递。然后打印var_dump($query)
,如果您有权访问phpMyAdmin
,可以尝试手动发送此sql查询并检查返回的错误。