Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提交表单时查询为空_Php_Mysql_Forms - Fatal编程技术网

Php 提交表单时查询为空

Php 提交表单时查询为空,php,mysql,forms,Php,Mysql,Forms,/表单设计/ <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form name="form1" met

/表单设计/

    <html>
        <head>
    <title>Untitled Document</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

        <body>
    <form name="form1" method="post" action=""><label><center>Register Form</center></label>
      <p><center></center><label>Name:</label>
      &nbsp; 
      <input type="text" name="name"></center>
    </p>
      <p><label>Rollno:</label>&nbsp; 
        <input type="text" name="rno"> 
      </p>
      <p><label>Address:</label>&nbsp; 
        <input type="text" name="add"> 
      </p>
      <p>
        <input type="submit" name="Submit" value="Submit">
    </p>
    </form>
    <?php 
    $con=mysql_connect("localhost","root","");
    mysql_select_db("alpha") or die( "Unable select database");
    // Check connection
    // escape variables for security
    $name = mysql_real_escape_string($_POST['name'],$con);
    $rno = mysql_real_escape_string( $_POST['rno'],$con);
    $add = mysql_real_escape_string($_POST['add'],$con);

    $sql=mysql_query("INSERT INTO test (name, rno, add)
    VALUES ('$name','$rno','$add')",$con);
    $result=mysql_query($sql) or die(mysql_error());
    if (!mysql_query($result,$con)) {
      die(mysql_error($con));
    }
    echo "1 record added";
        mysql_close($con);
    ?>
    </body>
    </html>

无标题文件
登记表
姓名:

罗尔诺:

地址:

/***********PHP******************/ /表格提交/

    <html>
        <head>
    <title>Untitled Document</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

        <body>
    <form name="form1" method="post" action=""><label><center>Register Form</center></label>
      <p><center></center><label>Name:</label>
      &nbsp; 
      <input type="text" name="name"></center>
    </p>
      <p><label>Rollno:</label>&nbsp; 
        <input type="text" name="rno"> 
      </p>
      <p><label>Address:</label>&nbsp; 
        <input type="text" name="add"> 
      </p>
      <p>
        <input type="submit" name="Submit" value="Submit">
    </p>
    </form>
    <?php 
    $con=mysql_connect("localhost","root","");
    mysql_select_db("alpha") or die( "Unable select database");
    // Check connection
    // escape variables for security
    $name = mysql_real_escape_string($_POST['name'],$con);
    $rno = mysql_real_escape_string( $_POST['rno'],$con);
    $add = mysql_real_escape_string($_POST['add'],$con);

    $sql=mysql_query("INSERT INTO test (name, rno, add)
    VALUES ('$name','$rno','$add')",$con);
    $result=mysql_query($sql) or die(mysql_error());
    if (!mysql_query($result,$con)) {
      die(mysql_error($con));
    }
    echo "1 record added";
        mysql_close($con);
    ?>
    </body>
    </html>
试试这个

<?php 

$con=mysql_connect("localhost","root","");
mysql_select_db("alpha") or die( "Unable select database");
// Check connection
// escape variables for security
$name = mysql_real_escape_string($_POST['name']);
$rno = mysql_real_escape_string( $_POST['rno']);
$add = mysql_real_escape_string($_POST['add']);

$sql=mysql_query("INSERT INTO test (name, rno, add) VALUES ('$name','$rno','$add')",$con);

if ($sql) {
    echo "1 record added";        
}
else {
    die(mysql_error($con));
}
mysql_close($con);

mysql\u查询应执行以下查询:


首先,如果您将html表单代码和php代码保存在同一页面中,则需要一个条件运算符来检查表单是否已提交。在上面的代码中,即使表单未提交,也会执行php代码

确保使用以下任一方法获取表单数据:

var_dump($_POST);
print_r($_POST);
如果它显示了您输入的数据,那么到目前为止您还不错

在上面的代码中,您有:

$result=mysql_query($sql) or die(mysql_error());
完成上述操作后,您将不需要以下代码,因为die()函数也会这样做。下面的代码还将使您的查询运行两次

    if (!mysql_query($result,$con)) {
      die(mysql_error($con));
    }

echo$sql并检查您是否在查询中获得值您应该使用
print\r($something)
对于代码中您认为可能错误的行,您可以自己找到问题所在,并且我认为
action=“
为空,请再次检查。请尝试避免类似
name=“name”
的属性。顺便提一下,
mysql\u real\u escape\u string()
已弃用,请参阅