使用php将包含图片的表单提交到mysql服务器

使用php将包含图片的表单提交到mysql服务器,php,forms,Php,Forms,我正在制作一个表单,允许用户创建一个带有二维码的页面,将其链接到该页面。我只有一个问题,我不能得到图片上传到数据库和显示后,表格已经提交。它只显示照片的文件名,而不是照片 这是我的表格代码 <?php ob_start(); session_start(); <html> <head> </head> <title>create </title> <center> <font size="+5">

我正在制作一个表单,允许用户创建一个带有二维码的页面,将其链接到该页面。我只有一个问题,我不能得到图片上传到数据库和显示后,表格已经提交。它只显示照片的文件名,而不是照片

这是我的表格代码

<?php
ob_start();
session_start();


<html>
<head>


</head>

<title>create </title>
<center> <font size="+5"> Create </center>
<body background="">
<p



<table align="center" cellpadding="0" border="0" cellspacing="1">
<form name="createTeaOffer" action="submit.php" method="get">

<tr> <td> <font size="+3"> Headline:</td> <td> <input type="text" name="hea"> </td> </tr>
<br />
<tr> <td> <font size="+3"> Photo:  </td> <td> <input type="file" name="pho"> </td> </tr>
<br /> 
<tr> <td><font size="+3">Description: </td> <td><input name="des" type="textarea"> </td>   </tr>
<br />
<tr> <td><font size="+3">Contact Method: </td> <td> <input name="con" type="radio" value="email" name="email" >Email <input type="radio" value="phone"   method="get" name="phone"> Phone </td> </tr>
<br />
<tr> <td><font size="+3">Email or Phone: </td> <td> <input name="ema" type="text"> </td> </tr>
<br />
<tr> <td><font size="+3">Location: </td> <td> <input name="loc" type="text"> </td> </tr>
<br />
<tr> <td><font size="+3">Expiration (yyyy-mm-dd): </td> <td> <input name="exp" type="date"> </td> </tr>
<br />
<tr> <td><font size="+3">Distance: </td> <td> <input name="dis" type="text"> </td></tr>
<br />
</font></table>
<input type="submit"> 
</form>
</body>
</html>

这是一个一般性的回答,我不能真正解释实际的代码,但您不能将图像上载到“数据库”,因为数据库(通常)基于文本,除非您使用图像编码。如果您的意思是上载到站点的文件系统,请参阅本教程并将图像URL添加到数据库中。然后,您可以使用
在用户配置文件上回显图像

正如dagon所说,你的html有一些问题,例如你把标题放在正文部分,而它应该放在标题部分。我还建议使用$\u POST请求,而不是$\u GET请求,因为这样更安全。要更好地掌握html的基础知识,有很多可用的资源。字体标签的使用会让一个普通的web开发人员痛哭流涕,因为它甚至不应该再被使用了。学习CSS,杜德(把
td{font-size:15px}
放在标题部分)


最后,调用表单字段名“loc”和“con”是命名字段的可怕方式。如果你是唯一一个编写代码的人,那么为什么你要把名字弄得如此混乱?

这是一个一般性的答案,我无法解释实际的代码,但你不能将图像上传到“数据库”,因为数据库(通常)是基于文本的,除非你使用图像编码。如果您的意思是上载到站点的文件系统,请参阅本教程并将图像URL添加到数据库中。然后,您可以使用
在用户配置文件上回显图像

正如dagon所说,你的html有一些问题,例如你把标题放在正文部分,而它应该放在标题部分。我还建议使用$\u POST请求,而不是$\u GET请求,因为这样更安全。要更好地掌握html的基础知识,有很多可用的资源。字体标签的使用会让一个普通的web开发人员痛哭流涕,因为它甚至不应该再被使用了。学习CSS,杜德(把
td{font-size:15px}
放在标题部分)


最后,调用表单字段名“loc”和“con”是命名字段的可怕方式。如果你是唯一一个编写代码的人,那么你为什么要把名字弄得如此混乱呢?

读一下,你的表单有很多地方不对劲:另外,用CSS代替表格布局页面也是一个糟糕的主意。这里有一个链接,解释了为什么我会学到更多关于通用html/css和php/mysql的知识。我认为一个很好的资源是TutsPlus。它需要钱,但很便宜,而且有大量的信息。读了这篇文章,你的表单有很多问题:另外,用css代替表格布局页面是一个糟糕的主意。这里有一个链接,解释了为什么我会学到更多关于通用html/css和php/mysql的知识。我认为一个很好的资源是TutsPlus。它需要钱,但很便宜,而且那里有丰富的信息
<?php
ob_start();
session_start();
$template = <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><!--TITLE--></title>




</head>

<body>
This page belongs to user $myusername<p><p>
QR Code: <img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fwww.url....%2F"<!--PAGENAME-->"&choe=UTF-8" title="Link to Google.com"><p> 
Headline: <!--COMMENT--><p>
Photo: <!--PHOTO--><p>
Description: <!--DESC--><p>
Contact By: <!--CONBY--><p>
Contact information: <!--EMP--><p>
Location: <!--LOC--><p>
page expires on: <!--EXP--><p>
Distance: <!--DIS--><p>
<p>
Webpage URL: <!--PAGENAME-->
<p>
Please click <a href="/">Here</a> to go back to the homepage.

</body>
</html>

EOD;

//handle the posted form
if(isset($_POST['hea1'])&&isset($_POST['pho1'])){
//replace the areas of the template with the posted values
$page = str_replace('<!--TITLE-->',htmlentities($_POST['hea1']),$template);
$page = str_replace('<!--COMMENT-->',htmlentities($_POST['hea1']),$page);
$page = str_replace('<!--PHOTO-->',htmlentities($_POST['pho1']),$page); 
$page = str_replace('<!--PAGENAME-->',htmlentities($_POST['wp1']),$page);


    $page = str_replace('<!--DESC-->',htmlentities($_POST['des1']),$page);
        $page = str_replace('<!--CONBY-->',htmlentities($_POST['con1']),$page);
            $page =   
str_replace('<!--EMP-->',htmlentities($_POST['ema1']),$page);
   $page =  str_replace('<!--LOC-->',htmlentities($_POST['loc1']),$page);               
$page = str_replace('<!--EXP-->',htmlentities($_POST['exp1']),$page);   
$page = str_replace('<!--DIS-->',htmlentities($_POST['dis1']),$page);   


//create a name for the new page
$pagename = md5($_POST['hea1']).'.html';

//db connect & select
$db=mysql_connect('localhost','streich','U4iwcjn3Hi&d');
mysql_select_db('streich_users');

//check if page already exists
//    $result = mysql_query('SELECT page_url from pages WHERE url="'.mysql_real_escape_string($pagename).'"');
//    if(mysql_num_rows($result)>=1){
//        $notice = '<p>Page already created <b>./pages/'.$pagename.'</b></p>';
//    }else{
    //inset new page into db
    mysql_query('INSERT into pages  (`page_name`,`disc`,`page_url`,`username`,
    `start_date`,`end_date`,`location`,`email`,`phone`,`photo`)VALUES(
    "'.mysql_real_escape_string(htmlentities($_POST['hea1'])).'",
    "'.mysql_real_escape_string(htmlentities($_POST['dis1'])).'",
    "'.$pagename.'","'.$myusername.'","'.mysql_real_escape_string(htmlentities($_POST['crd1'])).'",
    "'.mysql_real_escape_string(htmlentities($_POST['exp1'])).'",
    "'.mysql_real_escape_string(htmlentities($_POST['loc1'])).'",
    "'.mysql_real_escape_string(htmlentities($_POST['ema1'])).'",
    "'.mysql_real_escape_string(htmlentities($_POST['pho1'])).'")');
    //put the created content to file
    file_put_contents('./tearoffer/'.$pagename,$page);
    //make a notice to show the user
    $notice = '<p>New Page created <b>./page/'.$pagename.'</b></p>';
    $fulllink = 'url/'.$pagename.'';
    //header("location:/tearoffer/$pagename"); 
    echo $fulllink;
    ?>
    <img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fwww.url......%2Fpages%2F<? echo $pagename ?>&choe=UTF-8" title="Link"><p> 
    <?
  //  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en-gb">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Make page example</title>
<style type="text/css">
.prevpage {
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
color: #093;
background-color: #333;
}
body {

}
</style>
</head>

<body>
<?php
//if the notice is set then display it
if(isset($notice)){echo $notice;} ?>
<ul><li class="prevpage"><b>Headline:</b> <?php echo $_GET['hea']; ?></li>
<li class="prevpage"></li>
<li class="prevpage"><b>Photo:</b> <?php echo $_GET['pho']; ?></li>
<li class="prevpage"></li>
<li class="prevpage"><b>Description:</b> <?php echo $_GET['des']; ?></li>
<li class="prevpage"></li>
<li class="prevpage"><b>Contact by:</b> <?php echo $_GET['con']; ?></li>
<li class="prevpage"></li>
<li class="prevpage"><b>E-Mail or Phone:</b> <?php echo $_GET['ema']; ?></li>
<li class="prevpage"></li>
<li class="prevpage"><b>Location:</b> <?php echo $_GET['loc']; ?></li>
<li class="prevpage"></li>
<li class="prevpage"><b>Expiration (yyyy-mm-dd):</b> <?php echo $_GET['exp']; ?></li>
<li class="prevpage"></li>
<li class="prevpage"><b>Distance:</b> <?php echo $_GET['dis']; ?></li>
</ul>/
<form method="POST" action="">
<input name="hea1" type="hidden" value="<?php echo $_GET['hea']; ?>"> 
<input name="pho1" type="hidden" value="<?php echo $_GET['pho']; ?>">  
<input name="des1" type="hidden" value="<?php echo $_GET['des']; ?>"> 
<input name="crd1" type="hidden" value="<?php echo date("Y-m-d"); ?>">   
<input name="con1" type="hidden" value=<?php echo $_GET['con']; ?>> 
<input name="ema1" type="hidden" value=<?php echo $_GET['ema']; ?>> 
<input name="loc1" type="hidden" value=<?php echo $_GET['loc']; ?>> 
<input name="exp1" type="hidden" value=<?php echo $_GET['exp']; ?>>
<input name="dis1" type="hidden" value=<?php echo $_GET['dis']; ?>>
<input name="tst1" type="hidden" value="Thank you for using pages.">
<?php
    echo '<input type="hidden" name="wp1" value="' . htmlspecialchars($pagename) . '" />'."\n";
?>



<input type="submit" value="submit preview">
</form>



</body></html>