1.错误处理
您只需连接并执行查询。
好吧,不——你如何确保一切正常?
让我们从错误处理开始。
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('dbphesemaas');
?>
连接是否有效?数据库选择成功了吗?
您可以使用该if
模块来检查它是否有效。
<?php
// IF $link = mysql_connect('localhost', 'root', '') did not work (note the ! in front of it)
if(!$link = mysql_connect('localhost', 'root', '')){
die('Could not connect to localhost'); // The message displayed. die() will prevent the rest of the script from executing.
}
// IF database "dbphesemaas" did not get selected succesfully (note the ! in front of it)
if(!mysql_select_db('dbphesemaas', $link)){
die('Could not select the database "dbphesemaas"'); // The message displayed. die() will prevent the rest of the script from executing.
}
?>
现在我们的连接工作了。如果出现问题,脚本将停止执行并抛出自定义错误。
2. 不必要的变量
$username=$_POST["username"];
$area=$_POST["area"];
$product=$_POST["product"];
$address=$_POST["address"];
$dol_quantity=$_POST["quantity"];
现在是我的问题,为什么?仅在查询中使用它们没有任何问题。您只制作变量的唯一原因是旧变量很长(因此错字的可能性更大)和/或您认为代码过于混乱。因为在这段代码中使用 $_POST 变量没有问题,所以我们要从头开始这段代码。
3.实际查询
$query="INSERT INTO orders (id, product_id, address, quantity) VALUES ('$id', '$id2', '$address', '$dol_quantity')";
这里有几个问题:
- 您编写了查询,但没有执行它。
- 您在引号内使用变量(等)
$id
。$id2
在错误的情况下,它将插入$id
数据库而不是实际值。
- 再一次,没有错误处理。
- 完全没有一丝不苟。用户可以添加到您的查询中并更改查询,从而使可能的泄漏和被黑客入侵的机会更大。我们将通过以下方式防止这种情况:http
mysql_real_escape_string
: //php.net/manual/en/function.mysql-real-escape-string.php
- 看起来有点乱,但这只是一个视觉问题。
让我们解决这些问题:
$query="
INSERT INTO
orders
(
id,
product_id,
address,
quantity
)
VALUES
(
'". mysql_real_escape_string($_POST['id']) ."',
'". mysql_real_escape_string($_POST['id2']) ."',
'". mysql_real_escape_string($_POST['adress']) ."',
'". mysql_real_escape_string($_POST['quantity']) ."'
)
";
if(mysql_query($query)){
echo 'Succesfully executed the query.';
}
else
{
echo 'Query not executed - MySQL error. <br>';
echo '<pre>'. mysql_error() .'</pre>';
}
Using'". (random php code) ."'
允许在字符串中执行 php 代码。例如:
$variable = 'This is text '. strtoupper('this is capitalized since strtoupper makes this capital. note that this is inside the string.') .' and this is once again lowercase.';
4. 保留这个以备不时之需
我编写这些代码的方式对未来很有用。每次打开/添加新括号 ( {
) 时都保留使用选项卡。
更多信息 - 自 PHP 5.5 起,默认的 mysql_* 函数将被弃用 - 将来使用 MySQLi,它是改进的版本。信息:http ://www.php.net/manual/en/book.mysqli.php
5.针对您的实际问题
一个人mysql_query
只能执行一个查询。你可以这样做:
$queries = array();
$errors = array();
$queries[] = 'INSERT INTO ... '; // using $variable[] will add another entry to the $variable array.
$queries[] = 'INSERT INTO ... ';
$queries[] = 'UPDATE bla SET ...';
foreach($queries as $query){
// Foreach will seperate the entries in an array
// IF mysql query failed
if(!mysql_query($query)){
$errors[] = mysql_error(); // We'll add the errors to an array aswell.
}
}
// Check if there are entries in the $failures array.
if(count($errors) > 0){
echo 'We had some MySQL errors.';
echo '<ul>';
foreach($errors as $failure){
echo '<li>'. $failure .'</li>';
}
echo '</ul>';
}
else
{
echo 'No errors - MySQL queries executed succesfully.';
}
希望这对您有所帮助。