插入语句不起作用。从一张桌子转移到另一张桌子

时间:2014-02-26 14:47:51

标签: php mysql insert

PHP和MySQL的新手。

我在我的php脚本中创建了一个insert语句,用于将某行数据从一个表传输到下一个表中。唯一的问题是,它似乎没有起作用?

有人可以看到问题所在吗?

<?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=$_REQUEST['Reference'];
 $Forename=$_REQUEST['Forename'];
     $surname=$_REQUEST['surname'];
 $DOB=$_REQUEST['DOB'];
 $Mobile=$_REQUEST['Mobile'];
 $Home=$_REQUEST['Home'];
 $Address=$_REQUEST['Address'];
 $Postcode=$_REQUEST['Postcode1'];
 $Email=$_REQUEST['Email'];
 $Accident=$_REQUEST['Accident'];
 $Details=$_REQUEST['Details'];


//semi colon removed  
$sql="INSERT INTO Triage (Reference,Forename,surname,D.O.B,Mobile Number,Home Number,Address,Postcode1,Email,Accident,Details)
 VALUES('.$Reference.','.$Forename.','.$surname.','.$DOB.','.$Mobile.','.$Home.','.$Address.','.$Postcode1.','.$Email.','.$Accident.','.$Details.')";
 $result=mysql_query($sql);


 echo "Successful";
 echo "<BR>";
 echo "<a href='list_records.php'>View result</a>";


 ?> 

1 个答案:

答案 0 :(得分:0)

你有像Mobile Numbe等列名,你需要使用``对于他们来说连接看起来不正确你应该有一些东西

sql="INSERT INTO 
     Triage 
     (
       Reference, 
       Forename,
       surname,
       `D.O.B`,
       `Mobile Number`,
       `Home Number`,
       Address,
       Postcode1,
       Email,
       Accident,
       Details
      )
     VALUES
  (
    '".$Reference."',
    '".$Forename."',
    '".$surname."',
    '".$DOB."',
    '".$Mobile."',
    '".$Home."',
    '".$Address."',
    '".$Postcode1."',
    '".$Email."',
    '".$Accident."',
    '".$Details."'
 )";

此外,您应该使用mysql_real_escape_string()为所有请求数据添加

$Reference=mysql_real_escape_string($_REQUEST['Reference']);

以及其他人