PHP / Mysqli Insert语句不是写入数据库

时间:2015-11-15 04:56:01

标签: php mysql mysqli

我的问题是我的PHP插入代码没有写入数据库 - 我觉得这将是一个小错误与下面的HTML表单,但我不能放置它。

HTML表单(抱歉缩进)

<form method='post' action='form.php'>
          <div class='form-group'>  
            <input type='email' class='form-control' id='email' placeholder='Email : xyz@domain.com'>
          </div>
          <div class='form-group'>  
            <input type='text' class='form-control' id='name' placeholder='Name : Joe Bloggs'>
          </div>
          <div class='form-group'>
            <input type='textarea' class='form-control' id='message' placeholder='Message : Hey, can you do this job?'>
          </div>
                <button type='submit' class='btn btn-default'>Submit</button>
                </form>

以下是我的&#34; form.php&#34;我用来将数据发送到数据库的代码。

<?php
//PHP Opened

// Insert Data
$username = "username";
$password = "password";
$dbname = "dbname" ;

// Create connection
$con=mysqli_connect("localhost",$username,$password,$dbname);

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// From Form 
$email = isset($_POST['email']) ? $_POST['email'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';

echo '<p>'.$email.'</p>';

$sql="INSERT INTO comment (id, email, name, message)VALUES(null,.$email.,.$name.,.$message.)";

$result = (mysqli_query($con,$sql));

  if (false === $result){
    echo 'failed';
  }
  else{
    echo 'Record added';
  }

mysqli_close($con)
//Close PHP
?>

1 个答案:

答案 0 :(得分:1)

这是因为您没有为这些标签定义name属性。

新的HTML代码:

<form method='post' action='form.php'>
          <div class='form-group'>  
            <input type='email' name='email' class='form-control' id='email' placeholder='Email : xyz@domain.com'>
          </div>
          <div class='form-group'>  
            <input type='text' name='name' class='form-control' id='name' placeholder='Name : Joe Bloggs'>
          </div>
          <div class='form-group'>
            <input type='textarea' name='message' class='form-control' id='message' placeholder='Message : Hey, can you do this job?'>
          </div>
                <button type='submit' name='post' class='btn btn-default'>Submit</button>
                </form>

此外,您需要在PHP中进行一些修复:

<?php
//PHP Opened

// Insert Data
$username = "username";
$password = "password";
$dbname = "dbname" ;

// Create connection
$con=mysqli_connect("localhost",$username,$password,$dbname);

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// From Form 
if(isset($_POST['post'])) {
    $email = isset($_POST['email']) ? $_POST['email'] : '';
    $name = isset($_POST['name']) ? $_POST['name'] : '';
    $message = isset($_POST['message']) ? $_POST['message'] : '';

    echo '<p>'.$email.'</p>';

    $sql = "INSERT INTO comment (id, email, name, message) VALUES(null, " . $email . " , " . $name . ", " . $message . ")";

$result = (mysqli_query($con,$sql));

  if (false === $result){
    echo 'failed';
  }
  else{
    echo 'Record added';
  }

}
mysqli_close($con)
//Close PHP
?>