1. SQL ORDER BYの勉強

 1. 昇順 SELECT * FROM my_items ORDER BY id ASC;で my_itemsテーブルのidが昇順で並べられる
 2. 降順 SELECT * FROM my_items ORDER BY id DESC;で my_itemsテーブルのidが降順で並べられる
 3. WHERE句 SELECT * FROM my_items WHERE sales<=70 ORDER BY sales;で my_itemsテーブルのsales70以下で検索
4. 絶対情報salesフィールド SELECT * FROM my_items ORDER BY sales ASC; 売り上げ数昇順表示
5. 絶対情報salesフィールド SELECT * FROM my_items ORDER BY sales DESC; 売り上げ数降順表示
6. 絶対情報salesフィールド SELECT * FROM my_items SET sales=100 id=1; 売り上げ数降順表示

    
    
  

〜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>

thanks.php

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>確認画面</title>
<link href="style.css" rel="stylesheet" media="screen, print">
</head>
<body>
<?php
 $name = $_POST['name'];
 $email = $_POST['email'];
 $message = $_POST['message'];
	
 $name = htmlspecialchars($name);
 $email = htmlspecialchars($email);
 $message = htmlspecialchars($message);
	
	echo $name.'様<br>'."\n";
	echo 'お問い合わせ、ありがとうございました。<br>'."\n";
	echo 'お問い合わせ内容『'.$message.'』を<br>'."\n";
	echo $email.'にメールで送りましたのでご確認ください。'."\n";
	
 $mail_sub = 'お問い合わせを受け付けました。';
 $mail_body = $name."様、ご協力ありがとうございました。";
 $mail_body = html_entity_decode($mail_body,ENT_QUOTES,"UTF-8");
 $mail_head = 'From:xxx@gmail.com';
 
 mb_language('Japanese');
 mb_internal_encoding("UTF-8");
 mb_send_mail($email,$mail_sub,$mail_body,$mail_head);
?>
</body>
</html>

check.php

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>確認画面</title>
<link href="style.css" rel="stylesheet" media="screen, print">
</head>
<body>
<?php
 $name = $_POST['name'];
 $email = $_POST['email'];
 $message = $_POST['message'];
	
 $name = htmlspecialchars($name);
 $email = htmlspecialchars($email);
 $message = htmlspecialchars($message);

  echo '<ul>'."\n";
  echo '<li>';
    if($name=='') {
        echo 'お名前が、入力されていません。';
    } else {
        echo 'ようこそ、'.$name.'様';
    }
  echo '</li>'."\n";
  echo '<li>';
    if($email=='') {
        echo 'メールアドレスが、入力されていません。';
    } else {
        echo 'メールアドレス:'.$email;
    }
  echo '</li>'."\n";
  echo '<li>';		
    if($message=='') {
        echo 'お問い合わせの内容が、入力されていません。';
    } else {
        echo 'お問い合わせの内容:'.$message;
    }
  echo '</li>'."\n";
  echo '</ul>'."\n";

if($name=='' || $email=='' || $message=='') {
  echo '<form>'."\n";
  echo '<input type="button" onClick="history.back()" value="戻る">'."\n";;
  echo '</form>'."\n";	
} else {
  echo '<form action="thanks.php" method="post">'."\n";
  echo '<input type="hidden" name="name" value="'.$name.'">';
  echo '<input type="hidden" name="email" value="'.$email.'">';
  echo '<input type="hidden" name="message" value="'.$message.'">';
  echo '<input type="button" onClick="history.back()" value="戻る">'."\n";
  echo '<input type="submit" value="送信">'."\n";
  echo '</form>'."\n";
}
?>
</body>
</html>