如何从html表单插入图像到mysql

时间:2016-05-16 12:25:31

标签: php html mysql

我只是创建一个表单来获取名称,DOB和图像。如何获取图像并将其插入到mysql.i中我也试图在表单下方的同一页面中显示表格。

这是我的代码:

的index.php:

<?php 
if(isset($_POST['success'])) {
        echo "Your Registration is successful";
    }   

?>
<!DOCTYPE HTML> 
<html> 
<head> 
<title>SignUp</title> 
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<script type="text/javascript" src="validate.js"></script>
<script         src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">         </script>
<script src="http://iamrohit.in/lab/js/location.js"></script>
<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
  <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js">   </script>
  <script>
     $(function() {
        $( "#datepicker-13" ).datepicker();
        $( "#datepicker-13" ).datepicker("show");
     });
 </script>
</head> 
<body id="$_POST['success']" style="background-color: lightblue"> 
<div id="Sign-Up"> <fieldset style="width:30%">
<legend>Sign Up</legend> 
<table border="1"> <tr> 
<form method="POST" action="function.php"> 
<td>Name</td><td> <input type="text" name="name" id="name"></td> </tr> 
<tr> <td>DOB</td><td><input type="text" name="datepicker"   id="datepicker-13" size="30"></td></tr> 
<tr height="30">
<td>Upload Image :</td>
<td><input type=hidden name="MAX_FILE_SIZE" value="1000000" />
<input type="file" name="image">
</td>
</tr>
<tr> <td><input id="button" type="submit" name="submit" value="Submit"></td> </tr>
</form>
</table> 
</fieldset> 
</div> 
<div class="result">

</div>
</body>
</html> 

function.php:

<?php

 include('config.php');


function NewUser()
    {




    $name = $_POST['name'];
    $dob = date('Y-m-d',strtotime($_POST['datepicker']));
    $image = $_POST['image'];
    $query = "INSERT INTO personal (name,dob,image) VALUES ('$name','$dob','$image')";
    $data = mysql_query ($query)or die(mysql_error());
    if($data)
    {

            header("Location: index.php?success=1");
    }

}

function SignUp($Data)
{
if(!empty($_POST['name']))   //checking the 'user' name which is from    Sign-Up.html, is it empty or have some text
{
   $query = mysql_query("SELECT * FROM personal WHERE name =    '$_POST[name]'") or die(mysql_error());

    if(!$row = mysql_fetch_array($query) or die(mysql_error()))
    {
    newuser();
}
 else
 {
    echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
 }
}
}
if(isset($_POST['submit']))
{
    SignUp($_POST);

  }

?>

list.php的:

<?php

/ *     list.php的     显示来自&#39; personal&#39;的所有数据。表 * /

// connect to the database
include('function.php');

// get results from database
$result = mysql_query("SELECT * FROM personal") 
    or die(mysql_error());  

// display data in table
echo "<p><b>View List</b>  <a href='view-paginated.php?page=1'></a></p>";

echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>S.No</th> <th>Name</th> <th>DOB</th> <th>Image</th> </tr>";

//loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {

    // echo out the contents of each row into a table
    echo "<tr>";
    echo '<td>' . $row['s.no'] . '</td>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['dob'] . '</td>';
    echo '<td>' . $row['image'] . '</td>';
    echo "</tr>";

} 
echo "</table>";

&GT;

0 个答案:

没有答案