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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line