dongzao9044 2014-02-13 18:55
浏览 42
已采纳

PHP发布<a href>会导致插入错误

I am writing a php file that takes values from a form and posts them to a mysql database. One of the table fields is a button link to a video that will play when clicked. It works great if I go into the database and manually add the link. However my PHP insert causes an error. Please have a look at this code:

$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);

If I look at $link by doing something like: echo $link; die(); it produces a page with the button and the code in the button looks good. Is it how I am trying to insert it? Thanks for your help!

  • 写回答

1条回答 默认 最新

  • donljt2606 2014-02-13 19:04
    关注

    you have a lot of errors

    <?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.";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注