美文网首页
MySQL and PHP 2

MySQL and PHP 2

作者: bobo970129 | 来源:发表于2017-12-19 23:58 被阅读0次
<?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

      本文链接:https://www.haomeiwen.com/subject/ttjwwxtx.html