What is going on with the variable? Subquery not being executed?

Viewed 36

I think I may be onto something. But I'm still having a problem. What am I missing here to define the Task1?. Some info you may need is I am using MySqli PDO.

$sql = "INSERT INTO Tasks 
(FirstName, LastName, 
ListName, Phone, 
HomeAddress, 
Task1) VALUES ('$FirstName', 
'$LastName', '$ListName', 
'$Phone', 
'$HomeAddress', 
(SELECT Task1 FROM 
 AutoPilotTaskLists WHERE 
 ListName = 
'$ListName'))";
 $result = $conn->query($sql);

 if ($conn->query($sql) === 
TRUE) {
echo "Here is the 
information";
$Task1<br />
";

This outputs:

Notice: Undefined variable: Task1 in /filepath/ on line 57

EDIT: Just to provide more info on the progress of discovering the solution.

PHP Version is 7.3.33. The following is the entire code (after some edits from the original above). This doesn't work and throws an error saying: Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::get_result() in /Filepath/testpage.php:34 Stack trace: #0 {main} thrown in /Filepath/testpage.php on line 34

<?php
$servername = "servername";
$username = "username";
$password = "password!";
$dbname = "DBname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$id = $_POST['id'];
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$ListName = $_POST['ListName'];
$Phone = $_POST['Phone'];
$HomeAddress = $_POST['HomeAddress'];

}


$stmt = $conn->prepare('SELECT Task1 FROM AutoPilotTaskLists WHERE 
ListName 
= ?');
$stmt->bind_param("s", $ListName);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if ($row) {
$Task1 = $row['Task1'];
$stmt = $conn->prepare("INSERT INTO Tasks 
    (FirstName, LastName, ListName, Phone, HomeAddress, Task1) 
    VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $FirstName, $LastName, $ListName, 
$Phone, 
$HomeAddress, $Task1);
if ($stmt->execute()) {
    echo "Here is the information $Task1<br />";
}
}
$conn->close();

?>
2 Answers

You have to do a separate SELECT query to get the value of Task1.

$stmt = $conn->prepare('SELECT Task1 FROM AutoPilotTaskLists WHERE ListName = ?');
$stmt->bind_param("s", $ListName);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if ($row) {
    $Task1 = $row['Task1'];
    $stmt = $conn->prepare("INSERT INTO Tasks 
        (FirstName, LastName, ListName, Phone, HomeAddress, Task1) 
        VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssss", $FirstName, $LastName, $ListName, $Phone, $HomeAddress, $Task1);
    if ($stmt->execute()) {
        echo "Here is the information $Task1<br />";
    }
}

I have figured out that my first code DID submit task1 from one table to the other. BUT even though it IS INDEED being recorded to the Tasks table it is still not showing it on the page under "YOUR INPUT." Instead it tells me Notice: Undefined variable: Task1 in /Path/TestPage.php on line 56.

So I could still use help on that. But the code below does actually do the job. For those that want to see what worked:

$sql = "INSERT INTO Tasks (FirstName, LastName, ListName, Phone, 
HomeAddress, Task1) VALUES ('$FirstName', '$LastName', '$ListName', 
'$Phone', '$HomeAddress', (SELECT Task1 FROM AutoPilotTaskLists WHERE 
ListName = '$ListName'))";


if ($conn->query($sql) === TRUE) {
echo "Here is the information";
echo "<h2>YOUR INPUT:</h2>";
echo "$FirstName<br />
$LastName<br />
$ListName<br />
$Phone<br />
$HomeAddress<br />
$Task1<br />
";
}
Related