使用内部联接将数据返回到输入字段以进行更新操作

时间:2016-10-03 18:47:06

标签: php mysql

我正在尝试建立一个系统来从前端更新数据库中的数据,该前端已经使用它在数据库中找到的信息填充输入,这样您就能够看到它现在是怎样的然后输入新值。

问题是,当我返回名称,电子邮件,手机等值时,它会起作用。但是当我尝试加入我的表时,对于城市,我在输入字段中不会有像159这样的值,它是我数据库中特定城市的关键值。

刹车。打印出这个错误。

SQL错误

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous' in ....PATH TO FILE......: PDOStatement->execute(Array) #1 {main} thrown in .....PATH TO FILE....... on line 98

破码

这里有你在脚本的那部分运行的代码。

     else {
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT
  Users.id,
  Users.firstname,
  Users.lastname,
  Users.email,
  Users.age,
  Users.phone,
  Universities.univ AS university,
  Internship.internship_type AS Internship,
  Orase.oras AS city,
  Interes.interes AS interest,
  User_status.user_status AS userstatus,
  Users.filename,
  Users.account,
  Users.reg_date
FROM
  Users
INNER JOIN
  Orase ON Users.city = Orase.cheie
INNER JOIN
  Universities ON Users.university = Universities.id
INNER JOIN
  Interes ON Users.interest = Interes.cheie
INNER JOIN
  Internship ON Users.Internship_type = Internship.cheie
INNER JOIN
  User_status ON Users.user_status = User_status.id 
WHERE id = ?";
        $q = $pdo->prepare($sql);
        $q->execute(array($id));
        $data = $q->fetch(PDO::FETCH_ASSOC);
        $name = $data['firstname'];
        $lastname = $data['lastname'];
        $email = $data['email'];
        $age = $data['age'];
        $mobile = $data['phone'];
        $city = $data['city'];
        Database::disconnect();
    }

实际运作的代码

此外,相同的脚本在没有内部联接的情况下工作,以便我的城市返回一个数值。

else {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM Users where id = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($id));
    $data = $q->fetch(PDO::FETCH_ASSOC);
    $name = $data['firstname'];
    $lastname = $data['lastname'];
    $email = $data['email'];
    $age = $data['age'];
    $mobile = $data['phone'];
    $city = $data['city'];
    Database::disconnect();
}

?>

使用工作代码

的表单的图像

上下文图片,以便您了解我为什么尝试使用内部联接作为城市名称 Image for context so that you understand why I'm trying to use inner joins for the name of the city

1 个答案:

答案 0 :(得分:1)

只需替换WHERE id = ?

WHERE Users.id = ?即可