0

我有一个将表单数据通过电子邮件发送到电子邮件地址的表单。我也想将此信息存储到数据库中。我遇到的问题是我从一个表 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错误!

4

2 回答 2

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.

于 2012-06-07T22:18:53.680 回答
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'];

于 2012-06-07T22:27:41.583 回答