skip to Main Content

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


  1. Chosen as BEST ANSWER

    I have figured out that my first code DID submit task1 from one table to the other. I have got it working. 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'))";
    
    $result = $conn->query("SELECT Task1 FROM AutoPilotTaskLists WHERE ListName = '$ListName'");
    if ($result->num_rows > 0) {
      // output data of each row
      while($row = $result->fetch_assoc()) {
    $Task1 = $row["Task1"];
    }}
    
    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 />
    ";
    }
    

  2. 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 />";
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search