TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-19-2009, 07:26 PM   #1 (permalink)
The Visitor
Newcomer 
 
Join Date: Dec 2009
Posts: 1
Thanks: 0
micma909 is on a distinguished road
Default MySql Add to favorites - almost functional - help

Hello talkphp 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>";
micma909 is offline  
Reply With Quote
Old 12-19-2009, 08:18 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Can't completely debug this for you as it appears to me to be incomplete code (there are some values that don't have any assignments associated with them), but here's how I would rewrite it leaving your variable naming conventions in place.

The queries look okay, except for the fact that none of the data can be trusted. You forgot to escape the user based input coming from POST, and there are no checks to ensure the integrity or type of data being put back into the database. All this could be used to easily attack and manipulate your database server were this script to be on a live site.

There's a few other tips in the comments. Take a peek and see if it helps you get it back up and running;

php Code:
// This will help ensure that the values you are using are being set
echo 'DEBUG:<br />';
echo '$Id: ' . $Id . '<br />';
echo '$Current_Id: ' . $Current_Id . '<br />';
echo '<pre>' . var_dump( $_POST ) . '</pre>';
echo '<br />';

echo '<div class="video_post_bottom">';
echo '<div class="favorit">';
// You don't need to add the action if it's posting back to itself
echo '<form action="" method="post" class="favorites">';
echo '<input type="image" src="Images/Favorites.png" name="FAV" value="" />';
echo '<input type="hidden" name="FAV_Id" value="' . $Id . '">';
echo '</form></div>';

if ( isset( $_POST['FAV'] ) ) {
    // You should do some error checks here, ensure that your value is numeric, or a string, or a certain length...

    echo '<p>isset _POSTFAV</p>';
    $Video_Id = mysql_escape_string( $_POST['FAV_Id'] ); // Never use unescaped input values in an SQL query!

    // While the method of formatting your SQL statements is purely up to you, this method can ensure further data
    // integrity. Good SQL practice also involves never selecting more columns than you need, SELECT * should
    // be avoided whenever possible (and its always possible)
    $query7 = sprintf( "SELECT Video_Id FROM Favorites WHERE Video_Id = '%d' AND User_Id = '%s' LIMIT 1", $Video_Id, $Current_Id );
    echo '<p>SELECT * FROM FAVORITES</p>';

    $result_C = mysql_query( $query7 );

    // We don't need to loop through the results, or even fetch them since we know that if
    // no results were returned, the record wasn't found.
    if ( ! $result_C ) {
        echo '<p>INSERT INTO Favorites</p>';
        $query = sprintf( "INSERT INTO Favorites (Video_Id, User_Id) VALUES ('%d', '%s')", $Video_Id, $Current_Id );
        $result = mysql_query( $query );
        echo '<p>' . $Title . ' has been added to your favorites</p>'; // Not sure where $Title is coming from?
    }
    else { // No need to compare again here, if the row was found, it's in there
        echo '<p> THIS IS ALREADY IN YOUR FAVOURITES </p>';
    }

}

echo '<div class="comments"><a href="Comment/Comment.php?V_Id=' . $Id . '"><img src="Images/Comments.png"></a></div>';
echo '</div>';
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a MySQL class Andrew General 12 07-14-2009 03:49 PM
Securing your MySQL Queries with Sprintf Wildhoney General 26 03-18-2008 06:52 PM
MySQL Sell Up Alan @ CIT The Lounge 12 01-17-2008 05:46 PM
Error in connecting to MySQL via PHP EyeDentify MySQL & Databases 0 01-03-2008 01:06 PM
Notepage like application to open large MySQL files Wildhoney General 6 12-07-2007 02:18 PM


All times are GMT. The time now is 09:38 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design