user3301501
user3301501

Reputation: 39

PHP posting <a href> causes error on insert

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!

Upvotes: 0

Views: 100

Answers (1)

Simone Nigro
Simone Nigro

Reputation: 4887

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.";

Upvotes: 1

Related Questions