1

This is how you insert using AdoDB Databse Abstraction Layer.

<?php
include 'adodb5/adodb.inc.php';
$host = 'localhost';
$user = 'user2000';
$pass = 'password2000';
$dbname = 'w3cyberlearning';
$conn1 = &ADONewConnection('mysql');
$conn1->PConnect($host, $user, $pass, $dbname);
// the MySQL insert statement.
$sql = "INSERT INTO user_infor(id,first_name,last_name, email) values(?,?,?,?) ";
     $my_data = array(
     array(1, 'Paul', 'Mark', '[email protected]'),
     array(2, 'Jam', 'Gill', '[email protected]'),
     array(3, 'Mix', 'Alex', '[email protected]'),
     array(4, 'King', 'Mix', '[email protected]')
);

// loop through the array 
for ($i = 0; $i < count($my_data); $i++) {
    $d = $conn1->Execute($sql, $my_data[$i]);
    if (!$d) {
        print 'error' . $conn1->ErrorMsg() . '<br>';
    }
    echo 'Success!';
    echo "<br/>";
    }

The data is in array format.

What if I use a form.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $age = $_POST['age'];
}

<form enctype="multipart/form-data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <input type="text" id="name" name="name" />
    <input type="number" id="age" name="age" />
    <input type="submit" value="Submit"/>
</form>        

Now, the data comes from two variables $name and $age.

What I am unable to do is replace the above $my_data with the two variables which collects the data. How do I replace them

2
  • 1
    $d = $conn1->Execute($sql, array($name, $age)); Commented Dec 6, 2015 at 17:58
  • @u_mulder, please use answer to answer so that I accept it. Your solution works. Thank you. Commented Dec 6, 2015 at 18:02

1 Answer 1

1

As I see from

$d = $conn1->Execute($sql, $my_data[$i]);

arguments passed to Execute method are query string and array with some values.

So, in your case you can do the same:

$d = $conn1->Execute($sql, array($name, $age));

where $name, $age your variables and $sql is your query string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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