每次在我的DATABASE中添加行中的值后,都会插入空白行

时间:2016-10-26 17:13:30

标签: php database mysqli

我有以下代码并且它成功符文并且说数据已成功输入但是当我检查数据库表时,每次我插入数据(尽管操作成功)我得到一个空白行。 Inshort没有插入数据,尽管代码表示操作成功

CODE: 的 dbconnect.php

<?php
$host= "host";
$user = "user";
$password = "pass";
$db = "db";

 $con = mysqli_connect($host,$user,$password,$db);
 if (!$con) 
 {  echo "Failed to Connect"; }
 else
 {  echo "Connection successful";}
 ?>

CODE:的 addInfo.php

<?php
require "dbconnect.php";
$username = $_POST["username"];
$email = $_POST["email"];
$mobile = $_POST["mobile"];
$flag = false;
$sql = "insert into userInfo values('$username','$email','$mobile');";
$flag=mysqli_query($con,$sql);
if($flag!==false)
{
    echo "Data inserted successfully!";
}
else
{
echo "Error in insertion" . mysqli_error($con);
}
?>

CODE:的的index.php

<!DOCTYPE html>
<html>
<head>
    <title>Add Information</title>
</head>
<body>
<form action="addinfo.php">
<table>
    <tr>
    <td>Name:</td>
    <td><input type="text" name="name"/></td>
    <td>Email:</td>
    <td><input type="text" name="email"/></td>
    <td>Mobile:</td>
    <td><input type="text" name="mobile"/></td>
    </tr>
</table>
<input type ="submit" value="Submit" />
</form>
</body>
</html>

你们可以做任何类型的帮助,请继续,

1 个答案:

答案 0 :(得分:2)

I)更改

  • $username = $_POST["username"];更改为$username = $_POST["name"];
  • method="POST"
  • 中添加<form>
  • 使用mysqli prepared statement来避免安全漏洞

II)更新了代码

<强> dbconnect.php

<?php
$host= "host";
$user = "user";
$password = "pass";
$db = "db";

$con = new mysqli($host, $user, $password, $db);
if ($con->connect_errno) {
  echo "Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error;
}
?>

<强> addInfo.php

<?php
require "dbconnect.php";

$stmt = $mysqli->prepare("INSERT INTO userInfo (`username`,`email`,`mobile`) VALUES (?, ?, ?)");
$stmt->bind_param('sss', $_POST["name"], $_POST["email"], $_POST["mobile"]);

if($stmt->execute()) {
  echo "Data inserted successfully!";
} else {
  echo "Error in insertion" . $con->errno;
}
?>

<强>的index.php

<!DOCTYPE html>
<html>
  <head>
    <title>Add Information</title>
  </head>
  <body>
    <form action="addinfo.php" method="POST">
      <table>
        <tr>
          <td>Name:</td>
          <td><input type="text" name="name"/></td>
          <td>Email:</td>
          <td><input type="text" name="email"/></td>
          <td>Mobile:</td>
          <td><input type="text" name="mobile"/></td>
        </tr>
      </table>
      <input type ="submit" value="Submit" />
    </form>
  </body>
</html>

III)快速入门

IV)看看