这里我有一个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>
答案 0 :(得分:0)
在您遇到错误的行上阅读评论并需要改进您的代码。
$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
}
}
答案 1 :(得分:0)
首先,您需要更改创建表查询
$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);