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>
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
This is what I get when I export into a csv file. (won't let me post the picture) and I've also got the structure of the database listed as well. thanks for all the responses!
;;"0";;;; multiple times where I tried entering data with my form.
1 desc text latin1_swedish_ci No None
2 productname text latin1_swedish_ci No None 3 productver int(11) No None 4 typeofhard text latin1_swedish_ci No None 5 osname text latin1_swedish_ci No None 6 freqofocc text latin1_swedish_ci No None 7 solution text latin1_swedish_ci
No None
Try var_dump($_POST)
and check if all values are passed correctly. Then print var_dump($query)
and if you have access to phpMyAdmin
you can try sending this sql query manually and check the error returned.