1

The following query works fine in SQL but not as an SQL Query in PHP for some reason. The error message is as follows:

Incorrect integer value: 'SELECT id FROM colours WHERE colour = ' for column databasename.numbers.col_id at row 1

$redID = "SELECT id FROM colours WHERE colour = $red";

$populate = "INSERT INTO numbers (col_id) VALUES ('$redID')";

$result=$conn->query($populate);

Database Table - numbers

Database Table - colours

1
  • Learn to use parameters to pass values into queries. Commented Apr 25, 2020 at 12:05

1 Answer 1

1

Your populate query cannot accept an SQL String as a col_id.

$redIDSQL = "SELECT id FROM colours WHERE colour = '$red'";
$redIDQuery=$conn->query($redIDSQL);

$redIDArray=array();
while($redIDResult=mysqli_fetch_assoc($redIDQuery)){
    $redIDArray[]=$redIDResult['id'];
}

if(count($redIDArray)>0){
    $populateSQL = "INSERT INTO numbers (col_id) VALUES ('".implode("'),('",$redID)."')";
    $populateQuery=$conn->query($populateSQL);
}else{
    echo "redID could not be found with colour matching ".$red;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the above. Hwever I get the eroor 'Parse error: syntax error, unexpected '}' with this code. To fix it I added a ; after $redID=$redIDResult['id'] However, now I am getting the error 'Fatal error: Uncaught Error: Call to undefined method mysqli_result::fetch() in
@MrMan Sorry, I assumed you use PDO connection. I've updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.