Ashman
Ashman

Reputation: 3

PHP explode function into MYSQL database

How do I get this to work? Trying to use the explode function and insert into my DB table, but the values are not being sent to my DB table.

HTML:

<select id="customer_name" name="customer_name" required>
    <option value="">Select customer...</option>
    <?php
    $sqli = "SELECT * FROM customer order by customer_name";
    $result = mysqli_query($conn, $sqli);
    while ($row = mysqli_fetch_array($result)) {
    echo '<option>'.$row['customer_name'].'|'.$row['customer_ID'].'</option>';
    }
    ?>
    </select>

PHP:

<?php
include_once 'database.php';

$stmt1 = $conn->prepare("INSERT INTO persons(person_ID, customer_name, customer_ID) VALUES (?,?,?)");

$result = $_POST['customer_name'];
$result_explode = explode('|', $result);

$person_ID = $_REQUEST['person_ID'];
$customer_name= $_REQUEST['$result_explode[0]'];
$customer_ID= $_REQUEST['$result_explode[1]'];

$stmt1->bind_param("sss", $person_ID, $customer_name, $customer_ID);

$stmt1->execute();

echo "<strong>Record saved....</strong>"; echo 'returning to add another person. If no more persons to add please close this window.';

mysqli_close($conn);
?>

The above returns the errors: Undefined index: $result_explode[0] Undefined index: $result_explode[1] However I'm not sure how to define these (I'm self learning on a step curve)

Upvotes: 0

Views: 267

Answers (1)

Mahiwagang Kamote
Mahiwagang Kamote

Reputation: 46

Refer here for solution: https://www.stechies.com/undefined-index-error-php/

While working in PHP, you will come across two methods called $_POST and $_GET. These methods are used for obtaining values from the user through a form. When using them, you might encounter an error called “Notice: Undefined Index”.

This error means that within your code, there is a variable or constant that has no value assigned to it. But you may be trying to use the values obtained through the user form in your PHP code.

The error can be avoided by using the isset() function. This function will check whether the index variables are assigned a value or not, before using them.

Upvotes: 2

Related Questions