[英]I cant get the form data to go into database. What am I doing wrong?
代碼更新,仍然沒有工作。 我知道我顯然使用的mysql函數已經過時了。 但是現在我想要的只是這個代碼才能運行。 我想知道我做錯了什么:(
我是php和數據庫的新手......我一直在努力讓簡單的html表單數據進入數據庫表。 我無法讓它工作:(任何人都可以幫助,看看我的代碼有什么問題嗎?我剛剛在數據庫中用字段ID,FIRSTNAME和SURNAME完成了一個簡單的表。這是代碼:
<?php
//connect to database
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db = 'test';
if (!mysql_connect ($mysql_host, $mysql_user, $mysql_pass)||!mysql_select_db ($mysql_db) ) {
die(mysql_error());
}
// Code
if (isset($_POST['firstname'])&&
isset($_POST['surname'])) {
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
if (!empty($username)&&!empty($password)) {
$query = "INSERT INTO `test`.`test_tabell`
VALUES ('', '" . mysql_real_escape_string($firstname) . "', '" . mysql_real_escape_string($surname) . "')";
/*$query = "INSERT INTO `test`.`test_tabell` VALUES (``, `.$firstname.`, `.$surname.`)"; */
$query_run = mysql_query($query);
if (!$query_run) echo mysql_error();
}
}
?>
<form action="add.php" method="POST">
Firstname:<br> <input type="text" name="firstname" value="<?php if (isset($firstname)) { echo $firstname; } ?>"><br><br>
Surname:<br> <input type="text" name="surname" value="<?php if (isset($surname)) { echo $surname; } ?>"><br><br>
<input type="submit" value="Submit">
</form>
謝謝!
不要使用mysql特定的語法,它已經過時了,當你需要做一些高級的東西時,它開始變得很煩人,你不能切換到sqlite或postgresql。
我建議使用PDO,你可以這樣做:
// Usage: $db = connectToDataBase($dbHost, $dbName, $dbUsername, $dbPassword);
// Pre: $dbHost is the database hostname,
// $dbName is the name of the database itself,
// $dbUsername is the username to access the database,
// $dbPassword is the password for the user of the database.
// Post: $db is an PDO connection to the database, based on the input parameters.
function connectToDataBase($dbHost, $dbName, $dbUsername, $dbPassword)
{
try
{
return new PDO("mysql:host=$dbHost;dbname=$dbName;charset=UTF-8", $dbUsername, $dbPassword);
}
catch(PDOException $PDOexception)
{
exit("<p>An error ocurred: Can't connect to database. </p><p>More preciesly: ". $PDOexception->getMessage(). "</p>");
}
}
然后初始化變量(我想你忘了定義數據庫的名稱);
$host = 'localhost';
$user = 'root';
$dataBaseName = 'databaseName';
$pass = '';
現在您可以通過訪問您的數據庫
$GLOBALS['db'] = connectToDataBase($host , $databaseName, $user, $pass);
現在您有一個PDO數據庫donnection的實例。
我想指出的一件事是,你是對sql注入的生物,你想在查詢中使用預准備語句,如:
$query = "INSERT INTO test(first_name, sur_name) VALUES (:firstname, :surname);";
我們將在查詢中執行兩個變量$ firstName和$ surName,使它們替換:firstName和:surName的值,讓我先創建一個簡單的插入函數給你看:
function insertFunction($db, $query, $firstName, $surName)
{
$statement = $db->prepare($query);
return $statement->execute(array(":firstName" => $firstName, ":surName" => $surName));
}
所以你很容易做類似的事情
$firstName = 'Smith';
$surName = 'John';
$db = $GLOBALS['db'];
$success = insertFunction($db, $query, $firstName, $surName);
現在,您可以通過檢查$ success是true還是false來檢查它是否成功。
如果你想看到更高級的PDO使用(多行等),那么你可以在這里查看我的一條評論: Javascript函數如何? (不是最高評論)。
我希望這有幫助。 如果有什么奇怪的話請評論。
很難說沒有看到你的架構,但試試這個:
$query = "INSERT INTO `test`.`test_tabell` VALUES ('', '$firstname', '$surname')";
$query_run = mysql_query($query);
你正在使用反引號而不是撇號。 此外,您在嘗試定義查詢之前嘗試執行查詢。
您的插入查詢是錯誤的,也可以打開SQL注入 。 這是應該如何:
$query = "INSERT INTO `test`.`test_tabell`
VALUES ('', '" . mysql_real_escape_string($firstname) . "', '" . mysql_real_escape_string($surname) . "')";
注意將所有backticks
更改為apostrophe
。
此外,您正在嘗試在定義查詢之前執行查詢。
根據與表定義相關的信息,您可以跳過表中的id
字段。 INSERT查詢將變為:
$query = "INSERT INTO `test`.`test_tabell` (`FIRSTNAME`, `SURNAME`)
VALUES ('" . mysql_real_escape_string($firstname) . "', '" . mysql_real_escape_string($surname) . "')";
$query_run = mysql_query( $query );
正如在評論中發布的那樣,你真的不應該使用/學習/練習任何以“mysql_”開頭的函數,因為它不會在PHP更新后立即生效。 這些功能正在逐漸消失。 最好學習使用PHP和SQL數據庫 - 只需確保您學習的東西將來會有用。 請務必閱讀與PHP相關的面向對象編程(OOP)以及PDO和mysqli_ *函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.