我能想到的两件事高于我的头;
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)是不安全的! –
你有什么错误? –