PDO mysql multi update?

Status
Not open for further replies.

Georgez

Member
Joined
Dec 26, 2010
Messages
7
Programming Experience
Beginner
I can update name but how do I update lastname from the input statement below?
I have multiple row in mysql table
need help with the code
Please HELP!!! Thanks alot in advance

VB.NET:
<?php
$host = "localhost";       // Host name
$username = "username";            // Mysql username
$password = "";            // Mysql password
$db_name = "test";         // Database name
$tbl_name = "test_mysql"; // Table name

// Connect to server and select databse.
$db = new PDO('mysql:host=' . $host . ';dbname=' . $db_name, $username, $password);

// If there is an update, process it.
if (isset($_POST['submit'], $_POST['name']) && is_array($_POST['name'])) {
    $stmt = $db->prepare("UPDATE `$tbl_name` SET `name`=:name WHERE id=:id");
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->bindParam(':name', $name, PDO::PARAM_STR);
    foreach ($_POST['name'] as $id => $name) {
        $stmt->execute();
    }
    echo '<h1>Updated the records.</h1>';
}

// Print the form.
echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '" method="post">';
foreach ($db->query("SELECT `id`, `name`, `lastname` FROM `$tbl_name` ORDER BY `name`") as $row) {
    echo '<input type="text" name="name[' . (int)$row['id'] . ']" value="'
        . htmlspecialchars($row['name']) . '" /><input type="text" lastname="lastname[' . (int)$row['id'] . ']" value="'
        . htmlspecialchars($row['lastname']) . '" /><br />';;
   
}
echo '<input type="submit" name="submit" value="Update" /></form>';
?>
 
May I ask where exactly VB.NET fits into this? This site is dedicated to VB.NET and using it in conjunction with other technologies. If your question is not about VB.NET or integrating some other technology into a VB.NET app then I'm afraid that it's not appropriate for this site.
 
Status
Not open for further replies.
Back
Top