回应sql查询添加一个新行

时间:2013-03-11 22:04:17

标签: php

当我回显我的sql查询时,它会显示

the volunteer id is 312INSERT INTO volunteer_events (event_id, volunteer_id) VALUES ('91  
', '312')

它在'91之后添加了一个新行,而不是将它全部放在一行中。 我想知道我在做什么导致它添加一个新行。

public function addUserToEvent($event,$volunteer)
{
    $event_id = $event;
    echo "the event id is ". $event_id;
    $volunteer_id = $volunteer;
    echo "the volunteer id is ". $volunteer_id;
    $sql = "INSERT INTO volunteer_events (event_id, volunteer_id) VALUES ('$event_id', '$volunteer_id')";
    echo $sql;
    return mysql_query($sql);
}

我在这里调用addUserToEvent

if(isset($_POST["events"])){
    $eventIds = $_POST["events"];
    $N = count($eventIds);
    for($i=0; $i < $N; $i++) {
      echo "</br>";
      echo "The events IDs are";
      echo "</br>";
      echo($eid = $eventIds[$i]);

     $sql = "SELECT distinct v.id 
            FROM volunteers v
            WHERE v.first_name = '$fname'
            AND v.last_name = '$lname'
            AND v.email = '$email'";

     $results = $db->q($sql);
     if($row = $results->getAssoc()) 
      {
            $vID = $row['id'];
            echo "the vid is ". $vID;
      }
      echo "</br>";
      echo " the eid is blah ".$eid;
     echo $db->addUserToEvent($eid, $vID);
    }


}

1 个答案:

答案 0 :(得分:2)

修复非常简单,只需转换为int。

public function addUserToEvent($event,$volunteer)
{
    $event = (int) $event;
    $volunteer = (int) $volunteer;
    $event_id = $event;
    echo "the event id is ". $event_id;
    [...]

我在想的是字段volunteer.id的数据类型是一个varchar,它应该是一个INT。投射变量总是很好的做法。