mercredi 6 mai 2015

PHP SQL Add and Delete

I want to make a site that allows a user to add to a collection, then favorite with the click on a checkbox, and I want to add another checkbox to delete a gemstone if needed. I'm having problems with my checkbox that adds to the favorites table, how to delete, and adding to a collection. I'm looking for corrections, resources that could help me and psuedo code, but if I haven't given enough info let me know!

So this is my object-oriented favorites checkbox. Right now, the checkboxes are there but they're not functioning. There's some jquery in there that is supposed to stop the user from faving more than 10. I look at this and it makes sense to me so kind of hard to see where I'm going wrong.

    $username = $_SESSION['USERNAME']; 
    if(isset($_POST['submit'])){//to run PHP script on submit
        if(!empty($_POST['favorites'])){ 
        // Loop to store and display values of individual checked checkbox.
            foreach($_POST['favorites'] as $selected){
                if($_POST['checkbox']==0)
                $_POST['checkbox'] = '';
                $query= "UPDATE u_collection SET fav = 0 WHERE username ='".$username."'"; 
                mysqli_query($db,$query);
                echo 'test';
            }
        } 
    } 
    if(isset($_POST['Collection'])){
    $query= "UPDATE u_collection SET gem = '".$i->name."' WHERE username ='".$username."'"; 
    mysqli_query($db,$query);
    }

foreach($gems as $i){
    echo '<tr> 
    <th>'.$i->name.'</th> 
    <th><form action="userhome.php" method="post">
    <input type="checkbox"'; 
    if(!empty($i->fav)){
        echo ' checked = "checked" ';         
    }
    echo 'name="favorites[]" value='.$i->name.'></th></tr></form>'; 
}

<script src="http://ift.tt/13jmVM7"></script>
<script>
    $('form input[type=checkbox]').on('click', function(){
        if ($('form input[type=checkbox]:checked').length > 10)
        {
           $(this).removeAttr('checked');
           alert('You are only allowed to select 10 checkboxes');
        }
    });
</script>

Now the collection is really bothering me because I know it's simplifier than the previous problem, but my brain's drawing a blank. This is the code that is seen on the side, and there's a button that is to add to the user's collection. I'm trying to figure how I would do that with this object oriented variable, I'm assuming? I also need to add a delete button in the collection.

foreach($gemInfo as $i){
echo('
  <tr>  
  <th>
<img src='.$i->image.'><br>
<form action="userhome.php" method="post">
<input type="hidden" value=".'$i->name'.">
<input type="submit" name="Collection" value="+ Collection" />
</form>
</th>
  </tr>
  <tr>
    <th>'.$i->name.'</th>
  </tr>
  //rest of table'
  );
}

This is my first attempt at adding to the user's collection. It looks way wrong. I know I need to check the user's gems to see if it's already there, and it needs to be added to that user's part of the database, via session as used, but it doesn't make sense.

if(isset($_GET['name'])){
    $usql = "SELECT gem FROM gems WHERE gem = '".name."';";
    $ures =mysqli_query($db, $usql) or die(mysqli_error($db) ."<br>".$usql);
    $urow = mysqli_fetch_assoc($ures);
    $d = $urow['gem'];
    if(isset($_GET['name']) && $_GET['name'] == 'add'){
        //check to see if already added
        $fchksql = "SELECT gem FROM u_collection WHERE username ='".$_SESSION['USERNAME']."'";
        $fchkres =mysqli_query($db, $fchksql) or die(mysqli_error($db));
        $fr = mysqli_fetch_assoc($fchkres);
        $numrows = mysqli_num_rows($fchkres);
        if($numrows < 1){
        $afsql = "INSERT INTO u_collection(gem) values('".$_SESSION['USERNAME']."','".$_GET['name']."',now());";
        mysqli_query($db, $afsql) or die(mysqli_error($db) ."<br>".$afsql);
        header("Location: ".$config_basedir."userprofile.php?u=".name);
        }//end if

Also, when adding, it becomes an array. How would you delete from an array or is that simpler than I thought? Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire