0

new to PHP so i'm still learning how to do certain stuff. I am trying to store a Youtube link into my database. the code i use is valid and stores my form into the database without problems.

My Question: I don't want to store the entire Youtube link, but just the ID of the link.

For example: i enter this link into my forminput: http://www.youtube.com/watch?v=sIkhewd69SE The only thing i want to store is: sIkhewd69SE

This because i want to embed the entries later in other pages.

Here's the HTML of the form:

<form action="uploadcomplete.php" method="post">
                            <table cellspacing="20">
                            <tr>
                                <td>Paste a Youtube link:</td>
                                <td><input size="50%" id="link" class="form-text" name="link"type="text"/></td>
                            </tr>
                            <tr>
                                <td>Enter the title:</td>
                                <td><input size="30%" id="title" class="form-text" name="title" type="text"/></td>
                            </tr>
                            <tr>
                                <td>Enter the artist:</td>
                                <td><input size="30%" id="artist" class="form-text" name="artist" type="text"/></td>
                            </tr>

                            <tr>
                                <td></td>
                                <td><input id="submit" class="submit" type="submit" name="submit" value="Upload!" /></td>

                            </tr></table>  
                        </form>

Here's PHP-function called on by the form (UPLOAD COMPLETE.PHP) :

$errors = array()

if (isset($_POST['link']) && isset($_POST['title']) && isset($_POST['artist'])) {
  if (strlen($_POST['link'])<3) $errors[] = 'Uw naam moet minstens 3 letters bevatten';
  if (strlen($_POST['title'])<6) $errors[] = 'De naam van uw bedrijf moet minstens 3 letters bevatten';
  if (strlen($_POST['artist'])<6) $errors[] = 'Uw nummer moet minstens 9 nummers bevatten';

  if(count($errors)<1) {
    $conn = new mysqli('localhost', 'root', '', 'silentbeats');

    if (mysqli_connect_errno()) {
      exit('Connectie gefaald: '. mysqli_connect_error());
    }

    $adds['link'] = $conn->real_escape_string($_POST['link']);
    $adds['title'] = $conn->real_escape_string($_POST['title']);
    $adds['artist'] = $conn->real_escape_string($_POST['artist']);

    $sql = "INSERT INTO `tbltracks` (`link`, `title`, `artist`) 
    VALUES ('". $adds['link']. "', '". $adds['title']. "', '". $adds['artist']. "')"; 

    if ($conn->query($sql) === TRUE) {
      echo 'Uw gegevens werden opgeslagen, bedankt!';
      echo "<br /><a href='LP_general.php'>Terug naar de hoofdpagina</a>";
    }
    else {
      echo 'Error: '. $conn->error;
    }

    $conn->close();
  }
  else {
    echo "<script>alert('".implode("\\n", $errors)."');</script>";
    echo "<script>window.location='upload.php'</script>"; 
  }
}
else {
  echo 'Geen data ontvangen van formulier';
}
?>

How do i store only the ID tag of this? Any help would be appreciated, i'm really stuck on this one.

CodeHigh
  • 3
  • 1
  • 4
  • If the link is consistently in that format you could use explode() on the "watch?v=". – A.O. Aug 26 '13 at 23:10
  • i saw that topic too lawrence, i'm just wonering how i can implement this into my code. i'm still a noobie in PHP – CodeHigh Aug 26 '13 at 23:17

2 Answers2

0

This should do what you want, even if there are more parameters in your url:

$ytLink = 'http://www.youtube.com/watch?v=sIkhewd69SE';
$ytId = preg_replace('/^.+v=(\w+)&*.*$/', '$1', $ytLink);
echo $ytId;
StackSlave
  • 10,613
  • 2
  • 18
  • 35
0
list($query, $arg) = (explode('=', parse_url($_POST['link']))['query']));

The value you're looking for will be in $arg

Shylo Hana
  • 1,792
  • 13
  • 10