php代码不会将数据添加到myphpadmin

时间:2018-02-13 19:39:39

标签: php html mysql

我试图创建一个包含不同输入的网站页面。名称和电子邮件(以及提交按钮,如果算上的话)。我的问题是,它不是将我在html表单中输入的数据返回给phpmyadmin,而是发送一个"空结果集"。我不是html的新手,但我是php新手,所以我认为这就是我的问题所在。这是我的代码......

    <html>
    <body>

    <form action="#" method="POST"> 
     <table width="20%" border="0" cellspacing="2" cellpadding="1"> 
      <tr> 
       <td>Name:</td> 
       <td colspan="2"><input type="text" name="name"></td> 
      </tr> 
      <tr> 
       <td>Email:</td> 
       <td colspan="2"><input type="text" name="email"></td> 
      </tr>  
      <tr> 
       <td>&nbsp;</td> 
       <td colspan="2"><input type="submit" name="submit" value="Submit></td> 
      </tr> 
     </table> 
    </form> 

    </body> 
    </html>

    <?php
       $con=mysqli_connect("localhost","root","");
       mysqli_select_db($con,"mpet");
       if(isset($_POST['submit']))
       {    
         $name=$_POST['name'];
         mysqli_query($con,"insert into user(name) values('$name')");
         $email=$_POST['email'];
         mysqli_query($con,"insert into user(email) values('$email')");
       }


    ?>

感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:1)

在像大多数人建议的那样研究参数化查询之后,我能够让我的页面正常工作。这是代码,以防任何想要看到它...

<html>
<body>
<form action="connect.php" method="post"> 
<table width="20%" border="0" cellspacing="2" cellpadding="1"> 
<tr> 
  <td>Name:</td> 
  <td colspan="2"><input type="text" name="name"></td> 
</tr> 
<tr> 
  <td>Email:</td> 
  <td colspan="2"><input type="text" name="email"></td> 
</tr>  
<tr> 
  <td>&nbsp;</td> 
  <td colspan="2"><input type="submit" name="submit" value="Submit"></td> 
</tr> 
</table> 
</form> 

</body> 
</html> 

这是我的php文件......

<?php
$link = mysqli_connect("localhost", "root", "", "mypet");

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql = "INSERT INTO user (name,email) VALUES (?,?)";

if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "ss", $name,$email);

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];

if(mysqli_stmt_execute($stmt)){
    echo "<p>Thank you</p>";
} else{
   echo "ERROR: Could not execute query: $sql. " . mysqli_error($link);
}
} else{
    echo "ERROR: Could not prepare query: $sql. " . mysqli_error($link);
}

mysqli_stmt_close($stmt);

mysqli_close($link);
?>

到目前为止,这一直在进行,我已经多次测试过了。