2014-09-30 60 views
0

我看起来像这样。它应该插入这些值,但我不能让ID的由php插入。我无法得到正确的语法,请帮助。使用select语句和php变量插入到MySQL


$insertQuery = "insert into appointment (appointmentID, doctorid, appointmentDate, symptoms, patientid, time) 
    values($id,(select doctorid from doctors where doctorName like '$docName'),$date,$symptoms, 
    (select patientid from patient where patientFName like '$nameOfUser'),$time)"; 

我得到一个无效的查询错误,但是当我vardump THES变量($docName, $id, $nameOfUser)他们变成是在正确的格式。我已经尝试在MySQL表中手动输入,并且它已成功插入。

+0

你定义'预约ID'与PRIMARY_KEY和AUTO_INCREMENT?如果是的话,你可以删除'约会ID'字段或设置为'空' – 2014-09-30 01:20:43

+0

仍然没有成功。 – Gokigooooks 2014-09-30 01:45:17

回答

1

首先,您通过使用(select patientid from patient where patientFName like '$nameOfUser')来选择已经使用的ID而犯了一个错误。我建议patientid是主键和整数数据类型。

当您创建一个表。使用此语法使其自动增加:

CREATE TABLE example (
    id MEDIUMINT NOT NULL AUTO_INCREMENT, 
    name CHAR(30) NOT NULL, 
    PRIMARY KEY (id) 
) ENGINE=MyISAM; 

而当您插入表中时,您不必插入id。数据库引擎会自动计算最后一个ID。

INSERT INTO example(name)values('example'); 

但是!如果你已经不auto_increment命令创建这个表,你必须使用这个表“太远”,只要使用此解决方案:

mysql_connect('your host','database user','password'); 
mysql_select_db('your database name'); 
$query=mysql_query('SELECT MAX(patientid) FROM yourtable;'); 
$read_id = mysql_fetch_row($query)); 
$next_id = $read_id[0] + 1; 
$query = mysql_query('INSERT INTO yourtable(patientid)values('.$next_id.');'); 

欲了解更多信息,了解它here

+0

这是有用的信息!虽然我想澄清,是'('。$ next_id。')'把它放在值为php的时候用正确的方法来包装它?因为我解决了我的问题,这是因为我用插入引号'values('$ id','$ date'.. etc)包含了每个php变量' – Gokigooooks 2014-09-30 02:09:11

+0

点符号用于插入变量到字符串中。例如$ insert ='Hi'; $ concat = $ insert。'。你好吗?';打印$ concat =>嗨。你好吗?。我使用点符号的原因是因为在编写我的程序时,我总是在Notepad ++中输入它们,所以它会给出不同的颜色,这会让我更容易知道。它不会改变?但我建议你使用点。 – 2014-09-30 02:47:38

1
$insertQuery = "INSERT INTO appointment 
    (appointmentID 
    , doctorid 
    , appointmentDate 
    , symptoms 
    , patientid 
    , time 
    ) 
SELECT '" . $id . "' 
    , n.doctorid 
    , '" . $date . "' 
    , '". $symptoms ."' 
    , p.patientid 
    FROM (SELECT e.doctorid 
      FROM doctors e 
      WHERE e.doctorName LIKE '" . $docName . "' 
      LIMIT 1 
     ) d 
CROSS 
    JOIN (SELECT q.patientid 
      FROM patient q 
      WHERE q.patientName LIKE '" . $nameOfUser ."' 
      LIMIT 1 
     ) p "; 

这种说法是主题到SQL注入。为了缓解这种情况,您需要转义包含在SQL文本中的“不安全”值,或者使用带有绑定占位符的准备好的语句。

假设你正在使用的mysqli接口的程序风格的功能,连接被命名为$con

$insertQuery = "INSERT INTO appointment 
    (appointmentID 
    , doctorid 
    , appointmentDate 
    , symptoms 
    , patientid 
    , time 
    ) 
SELECT '" . mysqli_real_escape_string($con, $id) . "' 
    , n.doctorid 
    , '" . mysqli_real_escape_string($con, $date) . "' 
    , '" . mysqli_real_escape_string($con, $symptoms) ."' 
    , p.patientid 
    FROM (SELECT e.doctorid 
      FROM doctors e 
      WHERE e.doctorName LIKE '" . mysqli_real_escape_string($con, $docName) . "' 
      LIMIT 1 
     ) d 
CROSS 
    JOIN (SELECT q.patientid 
      FROM patient q 
      WHERE q.patientName LIKE '" . mysqli_real_escape_string($con, $nameOfUser) ."' 
      LIMIT 1 
     ) p "; 

已准备语句会使用绑定的占位符代替文字:

$insertQuery = "INSERT INTO appointment 
    (appointmentID 
    , doctorid 
    , appointmentDate 
    , symptoms 
    , patientid 
    , time 
    ) 
SELECT ? 
    , n.doctorid 
    , ? 
    , ? 
    , p.patientid 
    FROM (SELECT e.doctorid 
      FROM doctors e 
      WHERE e.doctorName LIKE ? 
      LIMIT 1 
     ) d 
CROSS 
    JOIN (SELECT q.patientid 
      FROM patient q 
      WHERE q.patientName LIKE ? 
      LIMIT 1 
     ) p ";