〜PHPの学習2/14 MySQLのデータベースを選択する〜

<!DOCTYPE html>
<html lang="ja">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>phpの勉強 MySQLのデータベースを選択する</title>
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 mydb データベースを選択し、文字コードセット</h1>
</div>

<div id="content">
<p style="margin-top: 200px">
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました';
mysql_query('SET NAMES UTF8');
?>
</p>
</div>

<div id="foot">
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p>
</div>

</div>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>phpの勉強 MySQLのデータベースを実行</title>
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 mydb データベースを実行する</h1>
</div>

<div id="content">
<p style="margin-top: 200px">
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました';
mysql_query('SET NAMES UTF8');

mysql_query('UPDATE my_items SET sales=350, created="2011-02-15", modified="2013-02-25" where id=4') or die(mysql_error());
echo '<br>データ変更しました。'
?>
</p>
</div>

<div id="foot">
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p>
</div>

</div>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>phpの勉強 MySQLのデータベースを実行</title>
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 mydb データベースを実行する</h1>
</div>

<div id="content">
<p style="margin-top: 200px">
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました';
mysql_query('SET NAMES UTF8');

mysql_query('INSERT INTO my_items SET id=8, item_name="グレープフルーツ", price=250, keyword="すっぱい、レモン色、ジュース", sales=310, created="2013-01-14", modified="2013-02-14"') or die(mysql_error());
echo '<br>データを挿入しました。'
?>
</p>
</div>

<div id="foot">
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p>
</div>

</div>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>phpの勉強 MySQLのデータベースを実行</title>
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 mydb データベースを実行する</h1>
</div>

<div id="content">
<p style="margin-top: 200px">
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました';
mysql_query('SET NAMES UTF8');

mysql_query('DELETE FROM my_items where id=8') or die(mysql_error());
echo '<br>データ削除しました。'
?>
</p>
</div>

<div id="foot">
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p>
</div>

</div>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>phpの勉強 MySQLのデータベースを実行</title>
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 データベースのレコードセットからデータを取得する</h1>
</div>

<div id="content">
<p style="margin-top: 200px">
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました';
mysql_query('SET NAMES UTF8');

$recordSet=mysql_query('SELECT * FROM my_items');
$data=mysql_fetch_assoc($recordSet);
echo '<br>','idは、',$data['id'],'です。','商品名は、',$data['item_name'], '。 金額は、',$data['price'],'円です。','keywordは、',$data['keyword'],'です。';
?>
</p>
</div>

<div id="foot">
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p>
</div>

</div>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>phpの勉強 MySQLのデータベースを実行</title>
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 データベースのレコードセットからデータを取得する 2件分</h1>
</div>

<div id="content">
<p style="margin-top: 200px">
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました';
mysql_query('SET NAMES UTF8');

$recordSet=mysql_query('SELECT * FROM my_items') or die(mysql_error());
$data=mysql_fetch_assoc($recordSet);
echo '<br>'. $data['item_name'];
$data=mysql_fetch_assoc($recordSet);
echo '<br>'. $data['item_name'];
?>
</p>
</div>

<div id="foot">
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p>
</div>

</div>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>phpの勉強 MySQLのデータベースを実行</title>
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 データベースのレコードセットからデータを取得する 全件</h1>
</div>

<div id="content">
<p style="margin-top: 50px">
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました<br>';
mysql_query('SET NAMES UTF8');

$recordSet=mysql_query('SELECT * FROM my_items') or die(mysql_error());
while($data=mysql_fetch_array($recordSet)){
echo '<br>'. $data['item_name'];
echo '<br>';
}
?>
</p>
</div>

<div id="foot">
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p>
</div>

</div>
</body>
<!--$recordSet=mysql_query('SELECT * FROM my_items') or die(mysql_error());
while($data=mysql_fetch_array($recordSet)){
-->
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>phpの勉強 MySQLのデータベースを実行</title>
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 データベースのレコードセットからデータ件数を取得する count</h1>
</div>

<div id="content">
<p style="margin-top: 50px">
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました<br>';
mysql_query('SET NAMES UTF8');

$recordSet=mysql_query('SELECT count(id) AS record_count FROM my_items');
$data=mysql_fetch_assoc($recordSet);
echo '<br>件数は、'. $data['record_count']. '件です。';
echo '<br>';

?>
</p>
</div>

<div id="foot">
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p>
</div>

</div>
</body>
<!--$recordSet=mysql_query('SELECT * FROM my_items') or die(mysql_error());
while($data=mysql_fetch_array($recordSet)){
-->
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>phpの勉強 MySQLのデータベースを選択する</title>
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 mydb データベースを選択し、文字コードセット</h1>
</div>

<div id="content">
<p style="margin-top: 200px">
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました';
mysql_query('SET NAMES UTF8');
?>
</p>
</div>

<div id="foot">
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p>
</div>

</div>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>フォームからの情報を保存する</title>
<link rel="stylesheet" href="style.css" media="print, screen">
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 商品登録</h1>
</div>

<div id="content">
<p style="margin-top: 50px">

<p>登録する商品の情報を記入してください</p>
<form id="ftmInput" action="input_do.php" method="post" name="frmInput">
<dl>
<dt>
 <label for="maker_id">メーカーID</label>
</dt>
<dd>
 <input name="maker_id" type="text" id="maker_id" size="10" maxlength="10">
</dd>
<dt>
 <label for="item_name">商品名</label>
</dt>
<dd>
 <input name="item_name" type="text" id="item_name" size="35" maxlength="255">
</dd>
<dt>
 <label for="price">価格</label>
</dt>
<dd>
 <input name="price" type="text" id="price" size="10" maxlength="10">
</dd>
<dt>
 <label for="keyword">キーワード</label>
</dt>
<dd>
 <input name="keyword" type="text" id="keyword" size="50" maxlength="255">
</dd>
</dl>
<input type="submit" value="登録する">
</form>
</p>
</div>
</div>

</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>phpの勉強 MySQLのデータベースを実行</title>
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 商品登録</h1>
</div>

<div id="content">
<p style="margin-top: 50px">
<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました<br>';
mysql_query('SET NAMES UTF8');

$sql=sprintf('INSERT INTO food_items SET maker_id="%s", item_name="%s", price=%d, keyword="%s"',														         mysql_real_escape_string($_POST['maker_id']), 
         mysql_real_escape_string($_POST['item_name']),
         mysql_real_escape_string($_POST['price']), 
         mysql_real_escape_string($_POST['keyword'])	
    );

mysql_query($sql) or die(mysql_error());
?>
<p>商品を登録しました</p>
</p>
</div>

<div id="foot">
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p>
</div>

</div>
</body>
<!--
$request="'OR''='";
$safeSql=sprintf("SELECT * FROM test_table WHERE password='%s'",mysql_real_escape_string($request));
print($safeSql);

-->
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>phpの勉強 商品登録</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>
<div id="wrap">
<div id="head">
<h1>phpの勉強 商品登録</h1>
</div>

<div id="content">
<p style="margin-top: 50px">

<?php
mysql_connect('localhost','root','root') or die(mysql_error());
echo 'データベースに接続しました';
mysql_select_db('mydb') or die(mysql_error());
echo '<br>データベーススペース「mydb」を選択しました<br>';
mysql_query('SET NAMES UTF8');

$recordSet=mysql_query('SELECT * FROM my_items ORDER BY id DESC');
?>
<table width="100%">
   <tr>
      <th scope="col">ID</th> 
      <th scope="col">メーカー</th> 
      <th scope="col">商品名</th> 
      <th scope="col">価格</th> 
   </tr>
<?php
     while($table=mysql_fetch_assoc($recordSet)){
?>
   <tr>
      <td><?php print(htmlspecialchars($table['id'])); ?></td> 
      <td><?php print(htmlspecialchars($table['maker_id'])); ?></td> 
      <td><?php print(htmlspecialchars($table['item_name'])); ?></td> 
      <td><?php print(htmlspecialchars($table['price'])); ?></td> 
   </tr>
<?php
		 }
?>
</table>
</p>
</div>
</body>
</html>