2015-11-21 162 views
1

如果即时点击设置配置文件图片的手段,我想在配置文件图片上设置该图片,我已经设置了该图片的值我必须存储1个剩余值,我必须存储0,在我的代码只会插入0代码如何设置配置文件图片

$sql=mysql_query("SELECT id FROM useralbum WHERE isprofilepic IN ('1') AND ssmid='$ssmid'"); 
$count = mysql_num_rows($sql); 
if($count == '1'){ 
while($row=mysql_fetch_assoc($sql)){ 
    $profileid = $row['id']; 
} 
$profile=mysql_query("UPDATE useralbum SET isprofilepic='0' WHERE id='$profileid'"); 
if($profile){ 
    echo "SUCCESS"; 
} 
else{ 
    echo "ERROR".mysql_error(); 
} 
} 

else{  
$newprofile=mysql_query("UPDATE useralbum SET isprofilepic='1' WHERE isprofilepic='".$_SESSION['profile']."'"); 
    if($newprofile){ 
    echo "SUCCESS"; 
} 
else{ 
    echo "ERROR".mysql_error(); 
} 
} 

回答

0

这是问题在您的情况。如果有以前的配置文件图片 。然后它会进入这个区块。它永远不会进入其他部分,所以从未为新的配置文件图片设置1。

if($count == '1'){ 
while($row=mysql_fetch_assoc($sql)){ 
    $profileid = $row['id']; 
} 
$profile=mysql_query("UPDATE useralbum SET isprofilepic='0' WHERE id='$profileid'"); 
if($profile){ 
    echo "SUCCESS"; 
} 
else{ 
    echo "ERROR".mysql_error(); 
} 
} 

在此,您不更改以前的记录后更改新的配置文件图片记录。如果之前没有设置配置文件图片,则可以使用。它会进入其他部分。但如果配置文件图片已经设置并且您改变了它,该怎么办?所以改变你的条件。

$sql=mysql_query("SELECT id FROM useralbum WHERE isprofilepic="1" AND ssmid='$ssmid'"); 
$count = mysql_num_rows($sql); 

//If there is already profile pic set then if block executes and set it to 0. 
if($count == '1'){ 
while($row=mysql_fetch_assoc($sql)){ 
    $profileid = $row['id']; 
} 
$profile=mysql_query("UPDATE useralbum SET isprofilepic='0' WHERE id='$profileid'"); 
if($profile){ 
    echo "SUCCESS"; 
} 
else{ 
    echo "ERROR".mysql_error(); 
} 
} 

    //After setting previous pic 0, update new pic to 1. (This will works on both cases if already profile pic is set and there is first time setting profile pic) 
$newprofile=mysql_query("UPDATE useralbum SET isprofilepic='1' WHERE ssmid='$ssmid'"); 
    if($newprofile){ 
    echo "SUCCESS"; 
} 
else{ 
    echo "ERROR".mysql_error(); 
} 

会给你准确的记录。试试这个。

+0

我无法理解爵士乐,在那里我必须改变,我犯了错误,你能否更新你的查询 –

+0

什么都没有发生爵士乐0只插入,1不插入 –

+0

$ _SESSION ['profile' ]'。有没有ssmid?如果是,那么你的查询在设置'1'时是错误的。 $ newprofile = mysql_query(“UPDATE useralbum SET isprofilepic ='1'WHERE ssmid ='$ ssmid'”); if($ newprofile){ – Dhara

相关问题