只有当新客户不存在时,我才尝试插入新客户。我认为 SQL 语句有问题,但我也想在 PHP 中提供上下文:
$sql = "INSERT INTO clients
(`studentEmail`, `studentPassword`, `parentEmail`, `parentPassword`,
`studentFirstName`, `studentLastName`, `studentPhone`, `parentFirstName`,
`parentLastName`, `parentPhone`, `school`)
VALUES ('$studentEmail', '$studentPassword', '$parentEmail',
'$parentPassword', '$studentFirstName', '$studentLastName',
'$studentPhone', '$parentFirstName', '$parentLastName', '$parentPhone', '$school')
SELECT studentEmail
FROM clients
WHERE not exists (select * from clients where studentEmail == '"$studentEmail"')";
最佳答案
在插入语句中,select
和values
是互斥的。而不是:
insert table1 (col1, col2, col3, ...)
values (1, 2, 3, ...)
select col1
where not exists (...)
尝试:
insert table1 (col1, col2, col3, ...)
select 1, 2, 3, ...
where not exists (...)
关于php - 在插入数据之前检查现有条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8566569/