1

what is the issue with this code , I'm using a form to insert some values into a database , i have a controller setup like that. when i submit the form , the value was not posted in the database, but if i remove all others fields and left only 2 fields in the form and post it ,it works so there's something that i miss,been trying to resolve for more than 6 hours .please some help :

//database insertion

 if (isset($_POST['VideoTITLE']))
 if (isset($_POST['ByArtist']))
 if (isset($_POST['GroupName']))
 if (isset($_POST['URL'])) 
 if (isset($_POST['VideoDate']))
 {

  try
 {
 $sql = 'INSERT INTO videoclip SET
        VideoTITLE = :VideoTITLE,
        ByArtist   = :ByArtist,
        GroupName  = :GroupName,
        URL        = :URL,
        VideoDate  = CURDATE()
        ';

    $s = $pdo -> prepare($sql);
    $s -> bindValue(':VideoTITLE',$_POST['VideoTITLE']);
    $s -> bindValue(':ByArtist',$_POST['ByArtist']);
    $s -> bindValue(':GroupName',$_POST['GroupName']);
    $s -> bindValue(':URL',$_POST['URL']);
    $s -> execute();
}
  catch(PDOException $e)
  {
    $error = 'error adding submitted data' . $e-> getMessage();
    include 'error.html.php';
    exit();
  }
    header('Location:.');
    exit();
}

here's my html form setup:

<form action="?" method="post" class="form-horizontal">
   <legend>Song Info</legend>


<fieldset>
<label>Song Title </label>
<input type="text" id="VideoTITLE" name="VideoTITLE" placeholder="song name…">

<label>Artist </label>
<input type="text" id="ByArtist" name="ByArtist" placeholder="artist name…">

<label>Musical Group</label>
<input type="text" id="GroupName" name="GroupName" placeholder="Type something…">

<label>Poster link</label>
<input type="text" id="URL" name="URL" placeholder="Type something…">

</fieldset><br>
<input  type="submit" class="btn  btn-success" value="Post video">

</form>
tadman
  • 208,517
  • 23
  • 234
  • 262
Obed Lorisson
  • 449
  • 3
  • 8
  • 22
  • 1
    What database, and what is the schema for the table? – Joseph at SwiftOtter Dec 20 '12 at 19:58
  • Any reason that you're doing 5 `if` statements instead of doing `if(isset($_POST['VideoTITLE']) && isset($_POST['ByArtist']))` etc.? – sbeliv01 Dec 20 '12 at 19:58
  • 1
    If you `print_r($_POST)` are all the values you are checking `isset()` on set? – None Dec 20 '12 at 19:59
  • 1
    Related: [How to squeeze error message out of PDO?](http://stackoverflow.com/q/3726505) – Pekka Dec 20 '12 at 19:59
  • 1
    I have tagged MySQL arbitrarily as it is the usual case with PHP. You need to retag, if you're using other database system. – Lion Dec 20 '12 at 20:00
  • i'm learnin from book for beginner , the book only have 1 if , because it's only one field they want to add value , in project i have 5 fields , so when i was try to resolve the issue , i was thinking may be i need to put 5 if instead of 1 ,if i need to replace it let me know – Obed Lorisson Dec 20 '12 at 20:00

3 Answers3

1

Its a couple of problems, maybe more:

  1. You have isset($_POST['VideoDate']) in your if condition which will always be false since VideoDate is not in your form. You should take this out since you seem to want to set it using CURDATE() in your insert script.
  2. your insert statement is incorrect. mysql inserts typically look like INSERT INTO TABLE_NAME (COL1, COL2) values('VALUE1', 'VALUE2'); so you should change your insert code to look like

    $sql = 'INSERT INTO videoclip (VideoTITLE, ByArtist, GroupName, URL, VideoDate) values (:VideoTITLE, :ByArtist, :GroupName, :URL, CURDATE())';

DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
  • 2
    Who the hell is downvoting the correct answers in this question? +1 to even out – Pekka Dec 20 '12 at 20:10
  • i fix it with your recommendations , i appreciate it: but after finishing posting but i receive this warning :Warning: Cannot modify header information - headers already sent by (output started at), but the value posted, but the redirect seems have an issue , what is the best way to correct it. – Obed Lorisson Dec 20 '12 at 20:22
  • check that there is no space above your ` – DiverseAndRemote.com Dec 20 '12 at 20:45
0

Your syntax is incorrect for INSERT. It should be something like:

$sql = 'INSERT INTO videoclip (VideoTITLE, ByArtist, GroupName, URL, VideoDate) 
    VALUES (:VideoTITLE, :ByArtist, :GroupName, :URL, CURDATE())';

In addition, $_POST['VideoDate'] is not valid as you do not have it in your form.

Kermit
  • 33,827
  • 13
  • 85
  • 121
0

You're doing the if statements wrong.

if (isset($_POST['VideoTITLE']) && isset($_POST['ByArtist']) && isset($_POST['GroupName'])
    && isset($_POST['URL']) && isset($_POST['VideoDate'])) {
....
}

This is basic programming stuff, so you might want to get a good introductory book to programming or PHP.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152