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 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败