独吾洋
|
分享:
▼
x0
|
[SQL][求助] 表格的日期和照片插入mysql table的问题
假设我做个form 想要储存日期和照片到mysql资料库里 index.tpl 复制程式
<html>
<body>
<h2>Add your details</h2>
<form method="post" action=".">
<table border=1>
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
</tr>
<tr>
<td>Location</td>
<td><input type="text" name="location"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Date of Birth</td>
<td>
<select name="month">
<option value="-1" selected>Month</option>
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">Jun</option>
<option value="07">Jul</option>
<option value="08">Aug</option>
<option value="09">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
<select name="day">
<option value="-1" selected>Day</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="year">
<option value="-1" selected>Year</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1968">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
</select>
</td>
<tr>
<td>Gender</td>
<td>
<input type="radio" name="gender" value="m">Male
<input type="radio" name="gender" value="f">Female
</td>
</tr>
<tr>
<td>Photo</td>
<td><input type="file" name="upload_photo"></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" value="Sign up"></td>
</tr>
</table>
</form>
<hr>
<h2>Member List</h2>
{if count($members) == 0}
<p>No members found.</p>
{else}
{foreach from=$members item=member}
<ul>
<li>{$member.name}, {$member.location}, {$member.gender}</li>
</ul>
{/foreach}
{/if}
</body>
</html>
index.php 复制程式
<?php
require "/usr/local/Smarty/libs/Smarty.class.php";
require "includes/defs.php";
$name = $_POST['name'];
$location = $_POST['location'];
$email = $_POST['email'];
$birthday = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
$gender = $_POST['gender'];
$tmpName = $_FILES['upload_photo']['tmp_name'];
$fp = fopen($tmpName, 'r');
$photo = fread($fp, filesize($tmpName));
$photo = addslashes($photo);
fclose($fp);
if (!empty($name)){
add_member($name, $location, $email, $birthday, $gender, $photo);
}
$smarty = new Smarty();
$smarty->assign('members', $members);
$smarty->display('index.tpl');
?>
defs.php 复制程式
<?php
function showerror() {
die("Error ". mysql_errno() . " : " . mysql_error());
}
function add_member($name, $location, $email, $birthday, $gender, $photo){
$connection = @ mysql_connect("mysql", "s2603948", "FHpRgXTG")
or die("Could not connect");
mysql_select_db("s2603948db", $connection) or showerror();
$insert = "insert into temp values" .
"(null, '$name', '$location', '$email', '$birthday', '$gender', '$photo')";
$result = @ mysql_query ($insert, $connection) or showerror();
mysql_close($connection) or showerror();
}
function is_member() {
$connection = @ mysql_connect("mysql", "s2603948", "FHpRgXTG")
or die("Could not connect");
mysql_select_db("s2603948db", $connection) or showerror();
$query = "select * from temp order by id";
$result = @ mysql_query($query, $connection) or showerror();
$members = array();
while ($row = mysql_fetch_array($result)) {
$members[] = $row;
}
mysql_close($connection) or showerror();
return $members;
}
?>
mysql table 复制程式
mysql> create table temp (id int(4) auto_increment,
-> name varchar(40),
-> email varchar(40),
-> birthday date default -00-00',
-> gender char(1),
-> photo blob,
-> primary key (id));
Query OK, 0 rows affected (0.05 sec)
[ 此文章被独吾洋在2008-04-21 07:07重新编辑 ]
|
|
x0
[楼 主]
From:APNIC | Posted:2008-04-17 14:32 |
|
|
|