Overlap of the variable in mysql, my column are set to my query result
- by foodil
The question is not clear , let me clarify it:
try{
$sql = '
SELECT UserID <==== (***********Used here for query ******)
FROM user
WHERE Rights != ?';
$stmt = $conn->prepare($sql);
$stmt->execute(array('admin'));
$result= $stmt->fetchAll();
}
catch(PDOException $e)
{
die ($e->getMessage().'<a href="add.php"> Back</a>');
}
foreach ($result as $set)
{
if ($set['UserID']==$_SESSION['username'])
$rights='edit';
else
{$rights=$_POST[$set['UserID']];}
if (($rights != 'default' || $set['UserID']==$_SESSION['username']) && $_POST['public']==0 )
{
$user=$set['UserID'];
try {
$query="INSERT INTO user_list(UserID <==== (***********Used here for insert ******),ListID,UserRights) VALUES ($user,$lastID,$rights)";
$stmt = $conn->prepare($query);
$stmt->execute();
}
catch(PDOException $e)
{
die ($e->getMessage().'<a href="add.php"> Back</a>');
$conn->rollBack();
}
}
}
As you can see the UserID is a query result but it is also the column i need to insert, so when i insert into the table it will casued an error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'UserA_ID' in 'field list'
because the column is modify by my query result
from:
$query="INSERT INTO user_list(UserID,ListID,UserRights) VALUES ($user,$lastID,$rights)";
to:
$query="INSERT INTO user_list(query_result_id,ListID,UserRights) VALUES ($user,$lastID,$rights)";
How to fix it ?Thank you.