使用GET语句插入语句不起作用

时间:2014-03-25 12:36:56

标签: php mysql sql

我认为我的代码错误,因为所选字段根本没有插入表格/字段。

如何从URL中获取以下参考资料。

 $sql="INSERT INTO Instruction (PhysioReference, Physio, PhysiosAddress, postcode, Physiomobile, Number, Physiofax, PhysiosEmail WHERE Reference='$Reference') 
SELECT PhysioReference, Name, Line1, Postcode, Mobile, Tel, Fax, Email from `Physio` WHERE PhysioReference='$PhysioReference'";

示例网址 http://test.com/Physiotoinstruction.php?PhysioReference=100099&a mp;参考= 456789

page.php文件

<?php

require_once('auth.php');

$host=""; // Host name 
$username=""; // Mysql username
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="Instruction"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");



$Reference=mysql_real_escape_string($_GET['Reference']);
$PhysioReference=mysql_real_escape_string($_GET['PhysioReference']);

$sql="INSERT INTO Instruction (PhysioReference, Physio, PhysiosAddress, postcode, Physiomobile, Number, Physiofax, PhysiosEmail WHERE Reference='$Reference') 
SELECT PhysioReference, Name, Line1, Postcode, Mobile, Tel, Fax, Email from `Physio` WHERE PhysioReference='$PhysioReference'";

$result=mysql_query($sql);



//$sql="INSERT INTO Triage (Reference, Forename)  
//SELECT Reference, Forename FROM `Instruction` 
//WHERE Reference='$Reference' LIMIT 1";

 echo "Successful";
 echo "<BR>";
 echo "<a href='insert.php'>View result</a>";
// mysql_error()



 ?>     

4 个答案:

答案 0 :(得分:4)

MySQL INSERT Syntax不支持WHERE子句,因此您的查询将失败。

尝试将PhysiosEmail WHERE Reference='$Reference')替换为PhysiosEmail)

答案 1 :(得分:1)

您可能要做的是更新:

$sql="UPDATE Instruction SET PhysioReference='".$PhysioReference."' WHERE Reference='".$Reference."';";

目前还不清楚你究竟想要实现什么,但是从你的评论中我猜你想要做的是(简化名称):

$reference1=mysql_real_escape_string($_GET['reference1']);
$reference2=mysql_real_escape_string($_GET['reference2']);

$select="SELECT name, tel, email FROM table1 WHERE reference1='".$reference1."';";
$result = mysql_query($select);
$row = mysql_fetch_assoc($result);

$update = "UPDATE table2 SET name='".$row['name']."', tel='".$row['tel']."', email='".$row['email']."' WHERE reference2='".$reference2."';";
$result = mysql_query($update);

更新

Physio(表1) 说明(表2)

$reference1=mysql_real_escape_string($_GET['PhysioReference']);
$reference2=mysql_real_escape_string($_GET['Reference']);

$select="SELECT PhysioReference FROM Physio WHERE PhysioReference='".$reference1."';";
$result = mysql_query($select);
$row = mysql_fetch_assoc($result);

$update = "UPDATE Instruction SET PhysioReference='".$row['PhysioReference']."' WHERE Reference='".$reference2."';";
$result = mysql_query($update);

$result=mysql_query($sql);

答案 2 :(得分:0)

从插入查询中删除 where 条件。它永远不会奏效。您可以在选择查询中使用它。

通过此

更改您的插入查询
$sql="INSERT INTO Instruction (PhysioReference, Physio, PhysiosAddress, postcode, Physiomobile, Number, Physiofax, PhysiosEmail ) 
SELECT PhysioReference, Name, Line1, Postcode, Mobile, Tel, Fax, Email from `Physio` WHERE PhysioReference='$PhysioReference'";

compare your question here

答案 3 :(得分:0)

    $sql="INSERT INTO Instruction (PhysioReference, Physio, PhysiosAddress, postcode, Physiomobile, Number, Physiofax, PhysiosEmail)
    (SELECT PhysioReference, Name, Line1, Postcode, Mobile, Tel, Fax, Email from `Physio` WHERE PhysioReference='".$PhysioReference."'");