<?php
$con = mysql_connect("localhost","","");
if(!$con){
die("Cannot link to the database" . mysql_error);
}
mysql_select_db("test");
$selection = $_POST['selection'];
if(isset($_POST['confirmDelete']) == true){
$idForDelete = $_POST['delete_id'];
$sql = "DELETE FROM exercise WHERE ID = '$idForDelete'";
if(!mysql_query($sql,$con)){
die ('Error ' . mysql_error());
}else{
echo "one record deleted.";
}
}
if($selection == "insert"){
// Insert Begin
if($_POST['firstname'] == "" || $_POST['lastname'] == '' || $_POST['age'] == ''){
echo "<script>alert('Empty!');history.go(-1);</script>";
}else{
//upload Begin
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if (!is_dir("upload/"))
{
mkdir("upload/", 0777);
}
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
/*$sql="INSERT INTO exercise(Link) VALUES ('upload/".$_FILES["file"]["name"]."')";
if(!mysql_query($sql, $con)){
die('Error: ' . mysql_error());
}else{
echo "The file is uploaded already!";
}*/
}
}
}
elseif($_FILES["file"]["size"] > 200000)
{
echo "File size is larger than 20000!";
}
else
{
echo "Invalid file";
}
//Upload End
$sql = "INSERT INTO exercise(FirstName,LastName,Age,Link) VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]','upload/".$_FILES["file"]["name"]."')";
if(!mysql_query($sql,$con)){
die("Error: " . mysql_error());
}else{
echo "one record added.";
echo "The file is uploaded already!";
}
}
// Insert End
}else if($selection == "update"){
$id = $_POST['id1'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$sql = "UPDATE exercise SET FirstName = '$firstname',LastName = '$lastname',Age = '$age' WHERE ID = '$id' ";
if(!mysql_query($sql,$con)){
die('Error ' . mysql_error());
}else{
echo "one record updated";
}
}
$result = mysql_query("SELECT * FROM exercise ORDER BY Age DESC");
echo "<table border = '1'>
<tr>
<th>ID</th>
<th>FirstName</th>
<th>LaseName</th>
<th>Age</th>
<th>Edit</th>
<th>Delete</th>
<th>Img</th>
</tr>";
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['ID']?></td>
<td><?php echo $row['FirstName']?></td>
<td><?php echo $row['LastName']?></td>
<td><?php echo $row['Age']?></td>
<td><a href = "3.php?id=<?php echo $row['ID'] ?>">Edit</a></td>
<td><form action = "2.php" method = "post"><input type = "submit" value = "delete"><input type = "hidden" name = "delete_id" value = <?php echo $row['ID']?>/><input type = "hidden" name = "confirmDelete" value = "yes"/></form></td>
<td><img src = "<?php echo $row['Link']?>"/></td>
</tr>
<?php
}
echo "</table>";
?>
MySQL and PHP 2
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Mac下利用HomeBrew安装PHP开发环境,下面这篇文章写的很详细: INSTALL NGINX, PHP A...
- mysql_fetch_row() 作用:以索引数组形式获取和显示数据 详解:每执行一次,都从资源也就是结果集里依...
- 一般情况下不建议对生产环境进行升级,升级开始后会停止LNMP相关服务。本文仅适用于LNMP1.2、1.3及1.4等...