PHP发布<a href=""> causes error on insert</a>

时间:2014-02-13 18:55:15

标签: php mysql

我正在编写一个php文件,它从表单中获取值并将它们发布到mysql数据库。其中一个表格字段是指向点击时播放的视频的按钮链接。如果我进入数据库并手动添加链接,它会很有用。但是我的PHP插入会导致错误。请看一下这段代码:

$fileName = "video_".$id.".html";
$link = "<a href=\"javascript: void(0)\" onclick=\"MM_openBrWindow('videos/".$fileName."','','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=420,height=390')\"><button class=\"count\">Watch Video</button></a>";
$con=mysqli_connect("localhost","videomanager","password","my_database");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$sql="INSERT INTO video_list (date, title, description, link) VALUES('$_POST[date]','".mysqli_real_escape_string($_POST['sermon'])."','".mysqli_real_escape_string($_POST['description'])."','$link' )";
if (!mysqli_query($sql,$con))
  {
  die('Error: ' . mysqli_error());
  }
  echo "This video has been successfully added to the video database.";
mysqli_close($con);

如果我通过执行以下操作来查看$ link:echo $ link;死();它生成一个带按钮的页面,按钮中的代码看起来很好。是我试图插入它的方式?谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

你有很多错误

<?php
$fileName = "video_".$id.".html";
$link     = "<a href=\"javascript: void(0)\" onclick=\"MM_openBrWindow('videos/".$fileName."','','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=420,height=390')\"><button class=\"count\">Watch Video</button></a>";
$con      = mysqli_connect("localhost","videomanager","password","my_database");

// Check connection
if ( mysqli_connect_errno() )
    die('Failed to connect to MySQL: ' . mysqli_connect_error() );

// Check param is set
if( !isset($_POST['date'], $_POST['sermon'], $_POST['description']) )
    die('Param Error');

// SQL Request
$sql = sprintf("INSERT INTO video_list (date, title, description, link) VALUES('%s','%s','%s','%s')",
        mysqli_real_escape_string($con, $_POST['date']), 
        mysqli_real_escape_string($con, $_POST['sermon']),
        mysqli_real_escape_string($con, $_POST['description']), 
        mysqli_real_escape_string($con, $link) 
);

// SQL execute
$result = mysqli_query($con, $sql) or die('Error: ' . mysqli_error($con));

// Free result
mysqli_free_result($result);

// Close connection
mysqli_close($con);

echo "This video has been successfully added to the Kim Watt videos.";