用于使用多个外键的简单 PHP 代码


Simple PHP code for using multiple foreign keys

我正在尝试编写订单流程。我在单个数据库(dbphesemaas)中有3个不同的表(订单,产品,用户)。

到目前为止我尝试过的方法不起作用:

<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('dbphesemaas');
$username=$_POST["username"];
$area=$_POST["area"];
$product=$_POST["product"];
$address=$_POST["address"];
$dol_quantity=$_POST["quantity"];

$query="INSERT INTO orders (id, product_id, address, quantity) VALUES ('$id', '$id2', '$address', '$dol_quantity')";
mysql_close();
?> 

有人可以让这段代码工作吗,id 是用户的外键,而product_id是产品的外键?

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 &quot;dbphesemaas&quot;'); // 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')";

这里有几个问题:

  1. 您编写了查询,但未执行它。
  2. 您在引号内使用变量($id$id2等)。在错误的情况下,它将在数据库中插入$id而不是实际值。
  3. 再一次,没有错误处理。
  4. 一点也不玷污。用户可以添加到您的查询中并更改查询,从而使可能的泄漏和被黑客入侵的机会更大。我们将通过mysql_real_escape_string:http://php.net/manual/en/function.mysql-real-escape-string.php 来防止这种情况
  5. 看起来有点乱,但这只是一个视觉问题。

让我们解决这些问题:

$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>';
}

使用 '". (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. 保留此内容以备将来使用

我编写这些代码的方式对未来很有用。每次打开/添加新括号({)时保留使用选项卡。

更多信息 - 默认的 mysql_* 函数将从 PHP 5.5 开始被弃用 - 将来使用 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.';
}

希望这对你有帮助。