为什么mysql插入新行而不是更新它?

时间:2013-12-09 10:35:00

标签: php mysql

我的代码应该检查数据库以查看custID是否存在,如果存在,则更新信息。它没有,它需要将客户信息添加到数据库。

目前,当我使用我的代码时,每次在网站上下订单时,都会向数据库添加一个新的custID。

正在发生这些错误:

  • 当新客户订购时,会插入新行。没有任何信息 从字段中放入数据库,只是一个空行。
  • 当回头客户订购时,他们的信息来自 上一页上的数据库,但在此页面上,它会插入一个新行和新字段 留空。

如果这些信息不充分或不清楚,我很乐意提供更多代码和解释。

//The information is passed through a session object from a previous page.

if (ISSET($_SESSION['fname'])) {
session_start();
$email = $_SESSION['email'];
$fname = $_SESSION['fname'];
$lname = $_SESSION['lname'];
$street = $_SESSION['street'];
$city = $_SESSION['city'];
$state = $_SESSION['state'];
$zip = $_SESSION['zip'];
$safeID = $_SESSION['safeID'];
$custID = $safeID / 507921;
}
include_once("Connection.php");
include_once("header.html");

//check if customer is already in database

$sql = "SELECT *
        FROM bookcustomers 
        where custID = '$custID'";

 $result = mysqli_query($link, $sql)
     or die('SQL syntax error: ' . mysqli_error($link));

 if (mysqli_num_rows($result) > 0 ) {
 $sql = "UPDATE bookcustomers
         set fname = '$fname',
            lname = '$lname',
            email = '$email',
            street = '$street',
            city = '$city',
            state = '$state',
            zip = '$zip'
         WHERE custID = '$custID'";


$result = mysqli_query($link, $sql)
     or die('SQL syntax error: ' . mysqli_error($link));
}
else {
    $sql = "INSERT into bookcustomers (fname,
            lname,
            email,
            street,
            city,
            state,
            zip) 
            VALUES ('$fname',
            '$lname',
            '$email',
            '$street',
            '$city',
            '$state',
            '$zip')";

    $result = mysqli_query($link, $sql)
     or die('SQL syntax error: ' . mysqli_error($link));

    $custID = mysqli_insert_id($link);
}

2 个答案:

答案 0 :(得分:0)

你可以试试这个吗,移动session_start(); if (ISSET($_SESSION['fname'])) {的顶部。

<?php
    session_start();
    if (ISSET($_SESSION['fname'])) {

    $email = $_SESSION['email'];
    $fname = $_SESSION['fname'];
    $lname = $_SESSION['lname'];
    $street = $_SESSION['street'];
    $city = $_SESSION['city'];
    $state = $_SESSION['state'];
    $zip = $_SESSION['zip'];
    $safeID = $_SESSION['safeID'];
    $custID = $safeID / 507921;
   }
    include_once("Connection.php");
    include_once("header.html");

        //check if customer is already in database

        $sql = "SELECT *
                FROM bookcustomers 
                where custID = '$custID'";

         $result = mysqli_query($link, $sql)
             or die('SQL syntax error: ' . mysqli_error($link));

         if (mysqli_num_rows($result) > 0 ) {
         $sql = "UPDATE bookcustomers
                 set fname = '$fname',
                    lname = '$lname',
                    email = '$email',
                    street = '$street',
                    city = '$city',
                    state = '$state',
                    zip = '$zip'
                 WHERE custID = '$custID'";


        $result = mysqli_query($link, $sql)
             or die('SQL syntax error: ' . mysqli_error($link));
        }
        else {
            $sql = "INSERT into bookcustomers (fname,
                    lname,
                    email,
                    street,
                    city,
                    state,
                    zip) 
                    VALUES ('$fname',
                    '$lname',
                    '$email',
                    '$street',
                    '$city',
                    '$state',
                    '$zip')";

            $result = mysqli_query($link, $sql)
             or die('SQL syntax error: ' . mysqli_error($link));

            $custID = mysqli_insert_id($link);
        }   



?>

答案 1 :(得分:0)

应在if子句之前调用

session_start

  

session_start()根据通过GET或POST请求传递的会话标识符创建会话或恢复当前会话,或通过cookie传递。

如果在php文件中更改top if

session_start();

if (ISSET($_SESSION['fname'])) {
    $email = $_SESSION['email'];
    $fname = $_SESSION['fname'];
    $lname = $_SESSION['lname'];
    $street = $_SESSION['street'];
    $city = $_SESSION['city'];
    $state = $_SESSION['state'];
    $zip = $_SESSION['zip'];
    $safeID = $_SESSION['safeID'];
    $custID = $safeID / 507921;
}
include_once("Connection.php");
include_once("header.html");

只要您正确创建会话并在上一页设置fname会话变量,这将恢复您的会话。

如果你正确设置了值并将if子句更改为上面的那个,那么它应该可以工作。