Вопрос

I would like to store youtube video id-s and names in my database, but on the admin page, I just want to send the video link in a form.

I tried this, but it doesn't work. I'm just learning how to use a function. Would you please help me?

if (isset($_POST['sendit'])){
$y_video  = $_POST['video'];
function video_add($y_video){

        $video=explode("=", $y_video);
        $code = explode("&",$video[1]);
        $video_feed = file_get_contents("http://gdata.youtube.com/feeds/api/videos?v=2&q=".$video[1]."&max-results=1&fields=entry(title)&prettyprint=true");
        $video_obj = simplexml_load_string($video_feed);
        $video_str = $video_obj->entry->title;
        $sql="INSERT INTO youtube ('y_vid','y_nev')
        VALUES('".$code[0]."','".mysql_real_escape_string($video_str)."')";
        mysql_query($sql);
            header("Location:video-list.php");
}
}
Это было полезно?

Решение

You have created the function but you are not calling it. You should edit your code to look something like this:

<?
  function video_add($y_video){

        $video=explode("=", $y_video);
        $code = explode("&",$video[1]);
        $video_feed = file_get_contents("http://gdata.youtube.com/feeds/api/videos?v=2&q=".$video[1]."&max-results=1&fields=entry(title)&prettyprint=true");
        $video_obj = simplexml_load_string($video_feed);
        $video_str = $video_obj->entry->title;
        $sql="INSERT INTO youtube ('y_vid','y_nev')
        VALUES('".$code[0]."','".mysql_real_escape_string($video_str)."')";
        mysql_query($sql);
            header("Location:video-list.php");
  }

  if (isset($_POST['sendit'])) video_add($_POST['video']);
?>

So basically, you first create the function (you can put it in some other file which you would later include if you need it on multiple places) and then you call it with the data you received with post.

Also, you are lacking connection data (or maybe you omitted those on purpose).

Also do not use mysql_* since they are deprecated. Use mysqli_* or PDO. See more at http://php.net/mysql_query

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top