如何从一个表中获取数据并插入到php中的另一个表中

时间:2013-09-12 09:54:16

标签: php mysql

<?php
    $m        = "arushi";
    $em       = "SELECT emailid FROM tblregister WHERE name='$m'";
    $q        = mysql_query($em);
    $n        = mysql_fetch_assoc($q);
    $fullName = mysql_real_escape_string($_POST['name']);
    $address  = mysql_real_escape_string($_POST['address']);
    $mobNo    = mysql_real_escape_string($_POST['dinner']);
    $summary  = "jsf";
    $sql      = "INSERT INTO tbljcustomer VALUES('$m', '$n', '$fullName',     
    '$address','$mobNo', '$summary')";

    if(!(mysql_query($sql)))
    {
        echo "Sorry!!! we were unable to process please try again";
    }
    else
    {
        echo "customized";
    }
?>

执行此操作一切正常,但它不从tblregister获取emailid而是显示数组或有时显示资源ID#10。提前致谢

7 个答案:

答案 0 :(得分:2)

使用此:

$n=mysql_fetch_assoc($q);
$emailid = $n['emailid'];

,您的查询将更改为

$sql="insert into tbljcustomer values('$m', '$emailid', '$fullName','$address','$mobNo', '$summary')";

答案 1 :(得分:0)

您必须使用:$n['emailid']

答案 2 :(得分:0)

$ n是一个关联数组。查看mysql_fetch_assoc的文档。如果要查看数组的结构,请使用var_dump($n);。来自文档:

  

mysql_fetch_assoc返回与之对应的字符串关联数组   获取的行,如果没有更多行,则返回FALSE。

答案 3 :(得分:0)

mysql_fetch_assoc返回一个数组 尝试:

insert into tbljcustomer values('$m', '".$n['emailid']."', '$fullName',...

答案 4 :(得分:0)

替换

$n

$n['emailid']
插入查询中的

答案 5 :(得分:0)

它将是

$email = $n['emailid'];

$sql="insert into tbljcustomer values('$m', '$email', '$fullName',     
'$address','$mobNo', '$summary')";

答案 6 :(得分:0)

替换

$sql      = "INSERT INTO tbljcustomer VALUES('$m', '$n', '$fullName',     
'$address','$mobNo', '$summary')";

$sql      = "INSERT INTO tbljcustomer VALUES('$m', '".$n['emailid']."', '$fullName',     
'$address','$mobNo', '$summary')";