MYSQL读写不同的表

时间:2012-06-07 21:40:01

标签: php mysql forms

我有一个表单,通过电子邮件将表单数据发送到电子邮件地址。我也想将这些信息存储到数据库中。我遇到的问题是我从一个表,online_rental_db中提取信息,以便将来自该表的信息发送给我们。我想将表单信息存储到另一个表online_rental_request中。

<?php

$rentID = $_POST['current_id'];

$db=mysql_connect ("localhost","test","test") or die(mysql_error());
mysql_select_db("rentals");

$table="online_rental_db";
$sql = "SELECT * FROM $table WHERE ID=$rentID";
$query = mysql_query($sql) or die(mysql_error());
$rentals = mysql_fetch_assoc($query);
$description = ucwords(strtolower($rentals['Description']));
$image = $rentals['Image'];
$download = $rentals['PDF'];
$bytes =  filesize($rentals['PDF']);
$ID = $rentals['ID'];
$CTGID = $rentals['CTGID'];
$category = $rentals['Category'];
$model = $rentals['model'];

我已尝试在$ sql上使用

$sql = "SELECT * FROM $table WHERE ID=$rentID INSERT INTO online_rental_request (name, email, id, description, model, category) VALUES ('$_POST[name]','$_POST[email]','$rentID','$description','$model','$category')";

但是它给了我一个sql错误!

2 个答案:

答案 0 :(得分:0)

怎么样

$sql = "INSERT INTO online_rental_request (name, email, id, description, model, category) SELECT '$_POST[name]','$_POST[email]', id, description, model, category  FROM $table WHERE ID=$rentID ";

所以格式为:

INSERT INTO online_rental_request (name, email, id, description, model, category) 
SELECT 
   '$_POST[name]',
   '$_POST[email]', 
   id, 
   description, 
   model, 
   category 
FROM $table 
WHERE ID=$rentID 

通过这种方式,您可以指定姓名和电子邮件,但可以插入$table中的信息并将其插入online_rental_request

答案 1 :(得分:0)

如果您想直接从online_rental_db表插入online_rental_request表,您应该执行以下操作:

$name = mysql_escape_string($_POST['name']);
$email = mysql_escape_string($_POST['email']);
$sql = "INSERT INTO online_rental_request (name, email, id, description, model, category) SELECT '$name', '$email', id, description, model, category FROM $table WHERE ID=$rentID";

顺便说一下,您不应忘记转义用户提交的值。你应该这样做:

$rentID = mysql_escape_string($_POST['current_id']);

或者,更好     $ rentID =(int)$ _ POST ['current_id'];