Jump to content

updating an ARRAY using PDO


happypete

Recommended Posts

Hi,

 

I can 'echo' the array, but can't get it to update, not sure about the code:

 

This prints the array:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 2);

     // Include the necessary files
    include_once 'inc/db.inc.php';

    // Open a database connection
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);

  	// Extract details from database

$sql2 = "SELECT id, dates, night, week, month, min, rank
  			FROM rates
		ORDER BY rank ASC";
		$stmt2 = $db->prepare($sql2);
		$stmt2->execute();
		$e2 = $stmt2->fetch(); 
  
?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <link rel="stylesheet" href="css/default.css" type="text/css" />
    <title>Test Site</title>
</head>

<body>
    <h1>Test Site</h1>
    <?php include('menu.php'); ?>

    <form method="post" action="arraytestupdate.php" enctype="multipart/form-data">
        <fieldset>
            <label>Rates<div class="clear"></div>
            <?php
            foreach($stmt2 as $e2) {
            ?>
            <input type="hidden" name="id" value="id=<?php echo $e2['id']; ?>" />
            <input class="mediuminput" type="text" name="dates" value="<?php echo $e2['dates']; ?>" />
            <input class="smallinput" type="text" name="night" value="<?php echo $e2['night']; ?>"   />
            <input class="smallinput" type="text" name="week" value="<?php echo $e2['week']; ?>"  />
            <input class="smallinput" type="text" name="month"value="<?php echo $e2['month']; ?>"  />
            <input class="smallinput" type="text" name="min" value="<?php echo $e2['min']; ?>"  />
            <input class="smallinput" type="text" name="rank" value="<?php echo $e2['rank']; ?>"  />
            </label>
            <?php
            }
            ?>
  			<input class="green" type="submit" name="submit" value="Update Entry" />
    	</fieldset>
    </form>

</body>
</html>

 

Not sure about the correct code to update..i need a 'foreach' in there somewhere??

 

<?php

// Include the necessary files
    include_once 'inc/db.inc.php';

    // Open a database connection
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);

	// Check if coming from a POST command and upadte the RATES
   		if($_SERVER['REQUEST_METHOD']=='POST'
  		&& $_POST['submit']=='Update Entry')
   		  {

        $sql = "UPDATE rates
                SET dates=?, night=?, week=?, month=?, min=?, rank=?
                WHERE id=?";
        $stmt = $db->prepare($sql);
        $stmt->execute(
           array(
			$_POST['dates'],
			$_POST['night'],
			$_POST['week'],
			$_POST['month'],
			$_POST['min'],
			$_POST['rank'],
			$_POST['id'],
            )
        );

        $stmt->closeCursor();


	// once updated return to admin page
	header('Location: arraytest.php');
	exit;
}
else
{
	// If nothing happend send back to admin page
	header('Location: arraytest.php');
	exit;
}


?>

 

thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/190607-updating-an-array-using-pdo/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.