php和mysql - 插入工作但更新无法正常工作

时间:2013-03-14 18:12:23

标签: php mysql

我的代码出了什么问题,因为插入工作正常,但更新无效。我已经从phpmyadmin复制了updaet的查询,并将静态值更改为变量

<?php
if (isset($_POST['submitContactInfo'])) {
    $socityId = $_SESSION['socityid'];
    $city = $_POST['city'];
    $pin = $_POST['pin'];
    $state = $_POST['state'];
    $telephone = $_POST['telephone'];
    $mobile = $_POST['mobile'];
    $email = $_POST['email'];
    $address = $_POST['address'];

    $sql = "INSERT INTO `_abc1`.`profile` (SOCIETY_ID,ADDR,CITY,PIN,STATE,TEL,MOBILE,EMAIL) VALUES ('$socityId','$address','$city','$pin','$state','$telephone','$mobile','$email');";


    $sql = "UPDATE `_abc1`.`profile` SET `ADDR` = '$address', `CITY` = '$city', `PIN` = '$pin', `STATE` = '$state', `TEL` = '$telephone', `MOBILE` = '$mobile', `EMAIL` = '$email' WHERE `society_profile`.`SOCITY_ID` = '$socityId'; ";

    $res = mysql_query($sql);
}
?>

3 个答案:

答案 0 :(得分:1)

您的更新中有拼写错误。在第一个查询中,您有SOCITY_ID,它是SOCIETY_ID

$sql = "UPDATE `_abc1`.`profile` SET `ADDR` = '$address', `CITY` = '$city', `PIN` = '$pin', `STATE` = '$state', `TEL` = '$telephone', `MOBILE` = '$mobile', `EMAIL` = '$email' WHERE `SOCIETY_ID` = '$socityId'; ";

此外,如上所述,您应该使用mysqli_query而不是这些旧的弃用函数。

http://www.php.net/manual/en/mysqli.query.php

答案 1 :(得分:0)

  • 首先,您应该考虑使用mysqli或PDO而不是标准的mysql类。
  • 其次,您还没有加入表格,那么您希望它与您尝试查询的两个表格相匹配?

答案 2 :(得分:0)

试试这个..

$sql = "UPDATE '_abc1'.'profile' SET 'ADDR' = '".$address."', 'CITY' = '".$city."', 'PIN' = '".$pin."', 'STATE' = '".$state."', 'TEL' = '".$telephone."', 'MOBILE' = '".$mobile."', 'EMAIL' = '".$email."' WHERE 'society_profile'.'SOCITY_ID' = '".$socityId."'; ";