如何在表中保存$ _post

时间:2015-03-03 05:01:31

标签: php mysql joomla joomla3.0

我只想问一下如何创建一个表单,将$ _post thingy中的输入保存到表中非常感谢!!或者,您可以帮我修复此代码或创建一个新代码吗?我需要你的帮助,请再次感谢!

<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "tsukishiro";

$id = $_POST['id'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$input = $_POST['input'];


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO connection(ID, name, comment, input) VALUES ('null', '$name', '$comment', 'input')";



if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["name"])) {
     $nameErr = "Name is required";
   } else {
     $name = test_input($_POST["name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $nameErr = "Only letters and white space allowed"; 
     }
   }

     if (empty($_POST["comment"])) {
     $comment = "";
   } else {
     $comment = test_input($_POST["comment"]);
   }


   if (empty($_POST["input"])) {
     $input = "";
   } else {
     $input = test_input($_POST["input"]);
   }



function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

echo "<input type='text' name='id'>";<br><br>
echo "<input type='text' name='name'>";
echo "<input type='text' name='comment'>";
echo "<input type='text' name='input'>";
echo "<input type='submit' name='submit'>";


if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}



$conn->close();

?>`

2 个答案:

答案 0 :(得分:0)

添加<form>代码。

<form name="form_name" action="your_page.php" method="post">
   <input type='text' name='id'>
   <input type='text' name='name'>
   <input type='text' name='comment'>
   <input type='text' name='input'>
   <input type='submit' name='submit'>
</form>

更新:

<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "tsukishiro";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}


if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $id = $_POST['id'];
    $name = $_POST['name'];
    $comment = $_POST['comment'];
    $input = $_POST['input'];

   if (empty($_POST["name"])) {
     $nameErr = "Name is required";
   } else {
     $name = test_input($_POST["name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $nameErr = "Only letters and white space allowed"; 
     }
   }

     if (empty($_POST["comment"])) {
     $comment = "";
   } else {
     $comment = test_input($_POST["comment"]);
   }


   if (empty($_POST["input"])) {
     $input = "";
   } else {
     $input = test_input($_POST["input"]);
   }

    $sql = "INSERT INTO connection(ID, name, comment, input) VALUES ('null', '$name', '$comment', 'input')";

    if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    }

}

$conn->close();

?>
<form name="form_name" action="" method="post">
   <input type='text' name='id'>
   <input type='text' name='name'>
   <input type='text' name='comment'>
   <input type='text' name='input'>
   <input type='submit' name='submit'>
</form>

答案 1 :(得分:0)

您错过输入变量的$ sign

$sql = "INSERT INTO connection(ID, name, comment, input) VALUES ('null', '$name', '$comment', '$input')";