Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.66 KB | None | 0 0
  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
  3. define('MYSQL_HOST', '127.0.0.1');
  4. define('MYSQL_USERNAME', 'user1');
  5. define('MYSQL_PASSWORD', '123456');
  6. define('MYSQL_DBNAME', 'form');
  7. define('ENDL', "\r\n");
  8.  
  9. $DB = new mysqli(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DBNAME);
  10.  
  11. if ($DB->connect_error) {
  12.     echo 'Connect error ' . $DB->connect_errno . ': ' . $DB->connect_error;
  13.     die();
  14. }
  15.  
  16. $DB->set_charset('utf8');
  17.  
  18. if (isset($_POST['update'])) {
  19.     $sql = 'UPDATE `articles` SET `title` = "%s" WHERE `id` = %d';
  20.     foreach ($_POST['title'] as $key => $value) {
  21.         $id = filter_var($key, FILTER_VALIDATE_INT);
  22.         $title = $DB->real_escape_string($value);
  23.        
  24.         if ($id === FALSE) {
  25.             continue;
  26.         }
  27.        
  28.         $DB->query(sprintf($sql, $title, $id));
  29.     }
  30. }
  31. ?>
  32. <html>
  33.     <head>
  34.         <title>Form Application</title>
  35.     </head>
  36.     <body>
  37.         <form method="post" action=".">
  38.             <table border="1">
  39.                 <tr>
  40.                     <th>ID</th>
  41.                     <th>Title</th>
  42.                 </tr>
  43.                 <?php
  44.                 $sql = 'SELECT * FROM `articles`';
  45.                 $templete = '<tr><td>%1$d</td><td><input type="text" name="title[%1$d]" value="%2$s" /></td></tr>' . ENDL;
  46.                 if ($query = $DB->query($sql)) {
  47.                     while ($result = $query->fetch_assoc()) {
  48.                         printf($templete, $result['id'], $result['title']);
  49.                     }
  50.                 }
  51.                 ?>
  52.             </table>
  53.             <input type="submit" name="update" value="update" />
  54.         </form>
  55.     </body>
  56. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement