This topic is locked
[SOLVED]

 Insert data into another table with a where clause

8/8/2014 1:22:20 PM
PHPRunner General questions
G
g_parry author

I have an add page that when I save updates some other tables. It works if all of the fields are completed but fails if any of the key fields are empty, I don't want to make these fields required so need to incorporate a where clause to stop the transfer if there is no data.



global $dal;
$tblEvents = $dal->Table("tblTestVehicles");

$tblEvents->Value["vclPersonID"]=$values["appPersonID"];

$tblEvents->Value["vclPermitID"]=$values["ID"];

$tblEvents->Value["vclVehicleID"]=$values["vclVehicleID"];

$tblEvents->Value["vclMake"]=$values["vclMake"];

$tblEvents->Value["vclModel"]=$values["vclModel"];

$tblEvents->Value["vclColour"]=$values["vclColour"];

$tblEvents->Value["vclType"]=$values["vclType"];

$tblEvents->Add();
$tblEvents = $dal->Table("tblTestVehicles");

$tblEvents->Value["vclPersonID"]=$values["appPersonID"];

$tblEvents->Value["vclPermitID"]=$values["ID"];

$tblEvents->Value["vclVehicleID"]=$values["vclVehicleID2"];

$tblEvents->Value["vclMake"]=$values["vclMake2"];

$tblEvents->Value["vclModel"]=$values["vclModel2"];

$tblEvents->Value["vclColour"]=$values["vclColour2"];

$tblEvents->Value["vclType"]=$values["vclType2"];

$tblEvents->Add();
return true;


I have also tried the following but get errors

global $conn;

$strSQLInsert = "INSERT INTO tblTestVehicles (vclPermitID, vclVehicleID, vclPersonID, vclColour, vclMake, vclModel,vclType)

VALUES (".$keys['ID'].",'".

$values["vclVehicleID2"]."','".$values["apnPersonID2"]."','".$values["vclColour2"]."','".$values["vclMake2"]."','".$values["vclModel2"]."','".$values["vclType2"]."') WHERE ".$keys["vclVehicleID2"]" > '1';

db_exec($strSQLInsert,$conn);


The inserts work finw until I add a where statement I need to add Where vclVehicleID2 is not empty on the second insert
I've been struggling with this for hours and would appreciate any help

Sergey Kornilov admin 8/12/2014

Plain INSERT statement doesn't support WHERE clause. You don't need WHERE while adding a record.
More info:

http://webcheatsheet.com/sql/interactive_sql_tutorial/sql_insert.php

G
g_parry author 8/17/2014



Plain INSERT statement doesn't support WHERE clause. You don't need WHERE while adding a record.
More info:

http://webcheatsheet.com/sql/interactive_sql_tutorial/sql_insert.php


Thats for that Sergey - I only wanted to insert the record if certain conditions were met, in the end I did a simple delete from the second table where that condition was met and it works pefectly.

romaldus 8/17/2014

use UPDATE instead of INSERT.