2015-03-03 55 views
-2

即時提出如何創建一個表格,將$ _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(); 

?>` 
+1

您是否嘗試運行的代碼?確切的問題是什麼?你有沒有收到任何錯誤? – phpPhil 2015-03-03 05:06:21

+2

您錯過了表格標籤 – starkeen 2015-03-03 05:09:40

+0

您使用的是Joomla,那麼爲什麼不堅持Joomla API。請仔細閱讀文檔 – Lodder 2015-03-03 17:30:25

回答

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> 
+0

但是如何創建your_page.php?對不起:(( – 2015-03-03 05:37:48

+0

your_page.php是你的服務器端編碼的頁面?這裏是同一頁面本身。 – arunrc 2015-03-03 05:44:34

+0

即時通訊使用joomla所以在哪裏你應該看到page.php嗎?sorryyy again @arunrc – 2015-03-03 05:50:43

0

你錯過$符號的輸入變量

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

我應該在哪裏把$符號? – 2015-03-03 10:49:00

+0

檢查你的$輸入變量在你的插入查詢中 – 2015-03-03 10:52:21