这里我有一个HTML表单和PHP MySQL表,在表中输入值后按下提交按钮后没有输出

时间:2021-10-29 06:43:55

Here I have an HTML form and PHP MySQL table, after entering values in the table and after pressing submit button no output appears.

这里我有一个HTML表单和PHP MySQL表,在表中输入值后按下提交按钮后没有输出。

This is my PHP code for creating, inserting and displaying MySQL table:

这是我用于创建,插入和显示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]."&nbsp &nbsp".$row[1]."&nbsp &nbsp".$row[2]."&nbsp &nbsp".$row[3]."&nbsp &nbsp".$row[4];
        }
    ?>

This is my HTML form code:

这是我的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>

2 个解决方案

#1


0  

Read the comment at line where you have done mistakes and need to improve your code.

在您遇到错误的行上阅读评论并需要改进您的代码。

$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] . "&nbsp &nbsp" . $row[1];// only two column in your code
    }
}

#2


0  

First you need to change your create table query

首先,您需要更改创建表查询

$tab="CREATE TABLE IF NOT EXISTS `stock1` (
  `id` int(15) NOT NULL,
  `Closing_Stock` int(15) NOT NULL
) ";

second at the beginning of the page check whether page submitted or not using

第二页在页面开头检查页面是否提交使用

if(isset($_POST) && count($_POST)> 0){
   //other database operation code goes here
}

third after executing query to get records check whether result comes or not using

第三步执行查询后获取记录检查结果是否来使用

 if($re->num_rows > 0){
 //show row data here
 }

four you have only selected two column in query and printing for col in html replace it by

四,您只在查询中选择了两列,并在html中打印col替换它

 echo $row[0]."&nbsp &nbsp".$row[1]."&nbsp &nbsp";

five when executing insert query you have misspelled mysqli method replace with

五,执行插入查询时你有错误拼写的mysqli方法替换为

$result=mySqli_query($con,$in);

#1


0  

Read the comment at line where you have done mistakes and need to improve your code.

在您遇到错误的行上阅读评论并需要改进您的代码。

$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] . "&nbsp &nbsp" . $row[1];// only two column in your code
    }
}

#2


0  

First you need to change your create table query

首先,您需要更改创建表查询

$tab="CREATE TABLE IF NOT EXISTS `stock1` (
  `id` int(15) NOT NULL,
  `Closing_Stock` int(15) NOT NULL
) ";

second at the beginning of the page check whether page submitted or not using

第二页在页面开头检查页面是否提交使用

if(isset($_POST) && count($_POST)> 0){
   //other database operation code goes here
}

third after executing query to get records check whether result comes or not using

第三步执行查询后获取记录检查结果是否来使用

 if($re->num_rows > 0){
 //show row data here
 }

four you have only selected two column in query and printing for col in html replace it by

四,您只在查询中选择了两列,并在html中打印col替换它

 echo $row[0]."&nbsp &nbsp".$row[1]."&nbsp &nbsp";

five when executing insert query you have misspelled mysqli method replace with

五,执行插入查询时你有错误拼写的mysqli方法替换为

$result=mySqli_query($con,$in);