how to pass id from one table to another table in php, can anyone tell me with example

Recommended Answers

All 7 Replies

using comma separator.

It's unclear (to me anyway) what you mean. Do you want to move an id from one database table to another? Move a cell from one HTML table to another? And the comma separator is supposed to do what, exactly? Split the id's?

<?php
include('admin/include/connect.php');
$p_id=$_REQUEST['proid'];
$cat_id=$_REQUEST['catid'];
$name   = $_POST['name'];
$email  = $_POST['email'];
$phno   = $_POST['phone'];

$a=mysql_query("select * from tbl_interest where email = '$email' and phone = '$phno' ")or die(mysql_error());
$count_a=mysql_num_rows($a);
if($count_a  > 0)
{
    $row_a=mysql_fetch_array($a);
    $m_id[]=$row_a['id'];
    $list_id = implode('","', $m_id);
    mysql_query("update tbl_products set interest='$list_id' where id=$row_a['id']")or die(mysql_error());
    echo "<script type='text/javascript'>alert('Already you shown your interest!'); window.location.href='product_deta.php?pro_id=$p_id&&cat_id=$cat_id';</script>";
}
else
{
mysql_query("insert into tbl_interest(name,email,phone)values('$name','$email','$phno')")or die(mysql_error());
echo "<script type='text/javascript'>alert('Thank you for your interest!'); window.location.href='product_deta.php?pro_id=$p_id&&cat_id=$cat_id';</script>";
}

?>

I`m getting error here:::
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in 

syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in

in.. the suspense is killing me.

One of those variables in the queries is probably not set properly. You should also not be using the deprecated mysql but one of the improved mysqli or PDO API's.

By using prepared statements you can prevent variables messing up your syntax. By allowing variables to be set directly into the query you're open to security risks.

edit: I see what you mean by using comma separator now

$list_id = implode('","', $m_id);

You're splitting on "," and not just ,. So do implode(',', $m_id) if there's only a comma or implode(', ', $m_id) if there's a comma and a space. And consider not storing it as a comma separated string in your database but in its own table.

your implode has '""' if you need the middle "" you should add addslashes()
to allow it to work plus you should refrain from using mysql like traevel said plus if you are staying with the mysql you should clean the post's

<?php
    include('admin/include/connect.php');
    $p_id=$_REQUEST['proid'];
    $cat_id=$_REQUEST['catid'];
    $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $phone = filter_input(INPUT_POST, 'phone', NUMBER_INT);
    $a=mysql_query("select * from `tbl_interest` WHERE email = '".$email."' and phone = '".$phone."' ")or die(mysql_error());
    $count_a=mysql_num_rows($a);
    if($count_a > 0)
    {
    $row_a=mysql_fetch_array($a);
    $m_id[]=$row_a['id'];
    $list_id = implode(',', $m_id);
    mysql_query("update `tbl_products` set interest = '".$list_id."' where id=".$row_a['id']."")or die(mysql_error());
    echo "<script type='text/javascript'>alert('Already you shown your interest!'); window.location.href='product_deta.php?pro_id=$p_id&&cat_id=$cat_id';</script>";
    }
    else
    {
    mysql_query("insert into `tbl_interest` (`name` ,`email`,`phone` )values('".$name."','".$email."','".$phone."')")or die(mysql_error());
    echo "<script type='text/javascript'>alert('Thank you for your interest!'); window.location.href='product_deta.php?pro_id=".$p_id."&&cat_id=".$cat_id."';</script>";
    }
    ?>

Thank you, I you want to store an id from one database table to another.
For example:1,2,3,4,5.
Suppose, from the above, if 2 is already registerd , it should not store like 1,2,3,4,5,2. its wrong. it sholud save 1,2,3,4,5. 2 should not store again. from the above code what are the changes to be made?

from the above code I`m getting output but if i give another id for the same, it overwrites new id, instead of using comma for old and new id.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.