我能想到的兩件事高於我的頭;
mysql_
已被棄用,因此else
踢英寸
- 你的語法可能錯
mysql_query
?
儘管如此,從頭開始,並用代碼,功能齊全,跟上時代的開始了......
鑑於您的連接是否正常將其更新到新mysqli
語法,這是非常簡單,更高雅:
$connect = new mysqli('localhost', 'USERNAME', 'PASSWORD', 'DATABASE');
// check for an error
if ($this->_connection->connect_error)
{
trigger_error("Connection Error: " . $this->_connection->connect_error(), E_USER_ERROR);
}
現在你已經連接了一個新的代碼流程。
開始通過檢查像你現在是一個submit
$_POST
,這樣你就可以開始運行該腳本:
if (isset($_POST['submit']))
{
// Encode the URL when creating the variables
$name = htmlentities($_POST['name']);
$phone = htmlentities($_POST['phone']);
$cash = htmlentities($_POST['cash']);
$date = date('l jS \of F Y h:i:s A');
// create sql
// DO NOT INSERT VALUES STRAIGHT INTO YOUR QUERY
$sql = "INSERT INTO tbl2 (name, phone, cash, date) VALUES (?, ?, ?, ?)";
注:繼續之前,讓我解釋一下,你不應該插入的內容到你的查詢因爲那會在您的代碼薄霧中拋出原始用戶輸入。現在,大多數用戶絕不會嘗試任何可疑的事情。但是任何人都可以輕鬆地在您的輸入中輸入一些SQL命令,並且可以在您的數據庫內容中輸入DELETE
,SELECT
和UPDATE
,並導致大量問題。
下面是一些參考:https://en.wikipedia.org/wiki/SQL_injection
要解決這個問題,使用prepared statements。您可以在PHP手冊中閱讀所有相關信息;並看到一些真實的例子。
// prepare query
// USE PREPARED STATEMENTS
if ($stmt = $connect->prepare($sql))
{
// bind the params
$stmt->bind_param('ssss', $name, $phone, $cash, $date);
// execute the query
$stmt->execute();
// check for errors
if ($stmt->errno)
{
$message = array(
'is_error' => 'danger',
'message' => 'Error: ' . $stmt->error
);
}
// make sure at least 1 or more rows were affected
if ($stmt->affected_rows > 0)
{
$message = array(
'is_error' => 'success',
'message' => 'Success: ' . $stmt->affected_rows . ' rows were inserted.' // value should be 1
);
}
else
{
// if not, send warning to user
$message = array(
'is_error' => 'warning',
'message' => 'Warning: ' . $stmt->affected_rows . ' rows were updated.'
);
}
// close your connection
$stmt->close();
}
else
{
$message = array(
'is_error' => 'danger',
'message' => 'QUERY: error. Try again.'
);
exit;
}
}
else
{
$message = array(
'is_error' => 'warning',
'message' => 'There was no submission attempt. Try again.'
);
exit;
}
代碼中的注意事項被分解成多個部分,您可以捕獲多個錯誤,這對調試非常重要;它將允許您準確知道代碼出錯的地方,並將您的問題本地化爲其中的一個部分。
***請[停止使用'mysql_ *'功能](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)*** [這些擴展名](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[prepared](http://en.wikipedia.org/wiki/Prepared_statement)語句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart。 prepared-statements.php)並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –
[Little Bobby](http://bobby-tables.com/)說*** [你的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can- I-防止-SQL注入式-PHP)***。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –
你有什麼錯誤? –