Forum Moderators: coopster

Message Too Old, No Replies

Problem with submit

MySql Add to favorites - almost functional - help

         

micma909

7:35 pm on Dec 19, 2009 (gmt 0)

10+ Year Member



Hello webmasterworld members,

I am very very new to php and mysql, and i have a problem.

Problem: I am buiding a simple embedd youtube-link site (school project) and i cant get my "add to favorites"-php to throw any values into my database. In my opinion the php below should work, i cant find any errors and i am completely blinded by now - why wont this work?

Additional things you need to know:

* this lies in a while loop that grabs the $Id.
* $Current_Id is from sessions id.

There was a point in time where this worked. please help me.

------------------------------------------------------------

print "<div class='video_post_bottom'>";//BOTTOM
print "<div class='favorit'><form action='Main4.php' method='post' class='favorites'><input type='image' src='Images/Favorites.png' name='FAV' value=''/><input type='hidden' name='FAV_Id' value='$Id'></form></div>"; //"Favorites" - BUTTON

//ADD TO FAVORITES
if (isset($_POST['FAV']))
{
print"<p>isset _POSTFAV</p>";
$Video_Id = $_POST['FAV_Id'];

$query7 ="SELECT * FROM Favorites WHERE '$Video_Id' = Favorites.Video_Id AND '$Current_Id' = User_Id";
$result_C = mysql_query($query7);
print"<p>SELECT * FROM FAVORITES</p>";

while ($row7 = mysql_fetch_array($result_C)) {
$favorite = $row7['Video_Id'];
print"<p>favorite = row7Video_Id</p>";
}

If(!$favorite)//not favorite yet?
{
$query = "INSERT INTO Favorites (User_Id, Video_Id) VALUES ('$Current_Id', '$Video_Id ')";
print"<p>INSERT INTO Favorites</p>";

$result_C = mysql_query($query);
print "<p> $Title has been added to your favorites</p>";

unset($_POST['FAV']);
}
else if($favorite && $Video_Id == $Id) //if voted has value
{
print "<p> THIS IS ALREADY IN YOUR FAVOURITES </p>";
}
}

print"<div class='comments'><a href='../Comment/Comment.php?V_Id=$Id'><img src='Images/Comments.png'></a></div>";//COMMENTS
print "</div>";

rocknbil

10:38 pm on Dec 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard micma909, first thing you need to learn to get into the habit of is error checking. What happens if you add these four lines, right where I show them?

if (! ($Current_Id>0) or (! ($Video_Id >0)) {
echo ("OOPS! current id or video id are invalid! c: $Current_Id v $Video_Id");
exit;
}

$query = "INSERT INTO Favorites (User_Id, Video_Id) VALUES ('$Current_Id', '$Video_Id ')";
print"<p>INSERT INTO Favorites</p>";

Even at that, it should still insert - but because you have quoted numeric values,

insert into table(num_field) values('');

You should still get an insert, it will just be a value of zero. Removing the quotes on numeric fields alerts you to the problem by generating a mysql error.

You can see where this is going. If you don't error check every expected value (which you should) you can do temporary things like this

echo '<!-- ' . $_POST['FAV_Id'] . ' -->';

Throughout you code, then view source. Verify all your values, you will figure it out.

Last thing, I don't even know what this is doing. :-)

print"<p>isset _POSTFAV</p>";

If you mean

print"<p>" . isset($_POST['FAV']) . "</p>";

It should just put a 1 or 0 (or true/false) between the p's. Unless _VARNAME does something I've forgotten (a likely possibility.)

micma909

11:11 pm on Dec 19, 2009 (gmt 0)

10+ Year Member



Thank you rocknbil,

yes the print"<p>isset _POSTFAV</p>"; thing was my clumbsy way of checking for errors...

It turns out that the form that was supposed to do the submit failed because i forgot to add <p></p> tags around it, go figure.

Thanks again.

rocknbil

9:51 pm on Dec 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Weeeeellllll . . . You don't add <p>tags around a form. :-) That's invalid HTML. If it "fixed" it, it's something else . . . . probably improperly nested tags.