这里我有一个HTML表单和PHP MySQL表,在表中输入值后,按下提交按钮后,不会显示任何输出。
这是我用于创建、插入和显示MySQL表的PHP代码:
<?php
$id=$_POST["id"];
$cs=$_POST["cs"];
$tab="create table stock1 (id INT PRIMARYKEY, Closing_Stock INT)";
$in="INSERT into stock1 values('$id','$cs')";
$q="SELECT * FROM stock1";
$con=new mysqli("localhost","root","","test");
mysqli_query($con,$tab);
mySqli_query($con,$in);
$re=mysqli_query($con,$q)or die(mysql_error());
while($row=mysqli_fetch_array($re))
{
echo $row[0]."   ".$row[1]."   ".$row[2]."   ".$row[3]."   ".$row[4];
}
?>
这是我的HTML表单代码:
<html>
<head>
<title>Database</title>
</head>
<body>
<form name="form1" action="stk1.php" method="post" >
<table cellpadding=5 cellspacing=10 align="center" width="600" height="200">
<caption><font size=20 font color=black>DETAILS</font></caption>
<tr>
<td>
<font color="white">Id </font>
</td>
<td>
<input type="text" name="id"/>
</td></tr><br>
<tr>
<td>
<font color="white">Stock /Opening : </font>
</td>
<td>
<input type="text" name="cs" />
</td></tr>
<tr><td>
<input type="submit" />
</td>
<td>
<input type="reset" />
</td>
</tr>
</table>
</form>
</body>
</html>
阅读第行的注释,其中您已经犯了错误,需要改进代码。
$id = $_POST["id"];
$cs = $_POST["cs"];
if (isset($id) && isset($cs)) {// check for input values
$tab = "CREATE TABLE IF NOT EXISTS `stock1` (
`id` int(11) NOT NULL DEFAULT '0',
`Closing_Stock` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";// use TABLE IF NOT EXISTS here
$in = "INSERT INTO stock1 (id,Closing_Stock) values('$id','$cs')";// forget column name
$q = "SELECT * FROM stock1";
$con = new mysqli("localhost", "root", "", "test");
mysqli_query($con, $tab);
mysqli_query($con, $in);// check spelling of mysqli
$re = mysqli_query($con, $q) or die(mysql_error());
while ($row = mysqli_fetch_array($re,MYSQLI_NUM)) {// add MYSQLI_NUM for numeric array
echo $row[0] . "   " . $row[1];// only two column in your code
}
}
首先需要更改创建表查询
$tab="CREATE TABLE IF NOT EXISTS `stock1` (
`id` int(15) NOT NULL,
`Closing_Stock` int(15) NOT NULL
) ";
第二次在页面开始时检查页面是否使用提交
if(isset($_POST) && count($_POST)> 0){
//other database operation code goes here
}
第三步,在执行查询以获取记录后,使用检查结果是否到来
if($re->num_rows > 0){
//show row data here
}
四你在查询和打印中只选择了两列,用代替html中的col
echo $row[0]."   ".$row[1]."   ";
五在执行插入查询时,您拼写错误的mysqli方法替换为
$result=mySqli_query($con,$in);