php删除sql查询无法正常工作

时间:2017-03-29 16:07:16

标签: php mysql mysqli

<?php
    include('session.php');
?>

<?php
    $conn = new mysqli("127.0.0.1","root","","foo");
    if ($conn->connect_errno) {
        echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " .         $conn->connect_error;
    }
    $sew = $_SESSION['login_user'];
    $a = $_GET["en"];
    $l = 1;
    $d = -1;

    if($a == 1)
    {
        $sqlw = " INSERT into dlkeuser VALUES('$a','$sew')" ;

        if ($conn->query($sqlw) === FALSE) 
        {
            echo "you have already disliked the song";
        }
        else
        {
        //query1
            $sql = " DELETE FROM lkeuser WHERE userid = '$sew' AND songid = '$a' ";

        //query2
            $sql = "UPDATE liking
            SET count = count - 1 ";

            if ($conn->query($sql) === TRUE) {
                echo "you disliked the song";
            } 
            else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }

在这个php代码片段中,query1不起作用,而查询2没问题。

我试图在dlkeuser(dislike)表中针对用户i / p($_GET["en"])插入(songid,userid)并从lkeuser(like)表中删除记录(songid,userid)存在。 (songid,userid)对是这里的复合主键。 count是一首歌的喜欢/不喜欢。

3 个答案:

答案 0 :(得分:0)

在重用$ sql变量之前,需要执行query1。

n

答案 1 :(得分:0)

您没有在任何地方执行query1。以下代码不会执行您的查询

$sql  = " DELETE FROM lkeuser WHERE userid = '$sew' AND songid = '$a' " ;

您需要另一行,如下所示(就像您对query2所做的那样)

if ($conn->query($sql) === TRUE) {

    echo "you liked the song";
} 
else {
      echo "Error: " . $sql . "<br>" . $conn->error;
}

执行查询并检查错误。

答案 2 :(得分:0)

让我们试试这个,

它会起作用

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>New Mentoring - 50- Home </title>

<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat:200,300,400,500,600" rel="stylesheet">
<link href="css/styles.css" rel="stylesheet">

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    </head>
    
<body>
<nav class="navbar navbar-default">
  <div class="container-fluid"> 
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
      
        <a class="navbar-brand" href="https://www.newmentoring.com"><img src="img/logo.png" class="img-responsive"></a></div>
      			<!-- Collect the nav links, forms, and other content for toggling -->
   			 <div class="collapse navbar-collapse" id="defaultNavbar1">
      			<ul class="nav navbar-nav pull-right">
        			<li class="active"><a href="index.html">Home<span class="sr-only">(current)</span></a></li>
        			<li><a href="about.html"> About</a></li>
        			<li><a href="saving-youth.html">Saving the Youth</a></li>
       				<li><a href="mentoring.html"> Mentoring Tips</a></li>
		 			<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" onClick="location.href='community.html'">Community <span class="caret"></span></a>
		 			<!-- DropDown Menu Here -->
					<ul class="dropdown-menu" role="menu">
						<li><a href="stories.html"> Stories of Wisdom</a></li>
						<li><a href="guestbook.html"> Guest Book</a></li>
						<li><a href="donate.html">Donate</a></li>
					</ul><!--community dropdown menu-->
					</li><!-- end of comunnity dropdown li & ul -->
         			<li><a href="contact.html"> Contact</a></li>
      			</ul> <!-- end of navbar left -->
    		</div> <!-- /.navbar-collapse --> 
  </div> <!-- /.container-fluid --> 
</nav> <!-- END OF FULL NAVIGATION BAR -->

<div class="jumbotron">
	<div class="container" id="jumbotron-inner">
  		<h1>New Mentoring.com</h1>
 			 <p class="lead">Where our youth gain wisdom and leadership.</p>
  				<p><a class="btn btn-primary btn-lg" href="#newSenior" role="button">Learn more</a></p>
	</div><!-- end of container & jumbtron inner -->
</div><!--jumbotron-->

<!-- BLOCKQUOTE-->
<blockquote>
	<p>&quot;Let your age make the difference in your community.&quot;</p>
	
		<span class="pull-right quote">- Delece Williams</span>
</blockquote>

<div class="container-fluid">
  <div class="row">
	  <div class="col-sm-6 senior">
		<h2>What is a New Mentoring?</h2>
	<p>The Middle Aged Generation that's considered to be between 30 to 80 whose living healthier, while inspiring youth to engage in healthier life-style choices and those helping to bridge the gap between the traditional values of seniors and the technical appetites of today's youth with wisdom keys. </p>                     
	</div>
  
 	<div class="col-sm-6 newMentor">

 	</div><!-- img div-->
	</div><!--row-->
  <div class="row">
   <div class="col-sm-6 get-involved">
	   <h2>How to Get Involved?</h2>

<p>We would love for you to get involved and its simple.  Just be willing to share your time and /or stories, experience, wisdom and expertise to a young person to help them make better and healthier lifestyle choices for the success of a better path throughout life.  You can do that by joining our online conversations or hosting a <a href="community.html">community</a> forum near you.</p> 
   
	</div>
	
	  <div class="col-sm-6 get-inolved-img">
	  	
	  </div><!-- background img-->
</div><!--row-->
	<!--col-md-12->
  </div>
  <hr>
  <div class="row">
    <div class="text-justify col-sm-6"> 
    <h3>Stories of Wisdom</h3>
   Share words of wisdom on how you break barriers to get young people to listen. Connect with our online family and help us to inspire others<a href="storiesofwisdom.html"> here.</a> </div>
   
   
 <div class="text-justify col-sm-6"> 
    <h3>Guest Book</h3>
   Thank you for visiting I Am The New 50 Campaign. Stop by and sign our guest book and be a part of our community. Tell us how we’ve inspired you to help youth <a href="guestbook.html"> here.</a> </div>
</div><!-- end of row div-->
  
 
    <div class="col-md-6 dontations">
      <h2>Dontations for Kidz Korna</h2>
     		<p>Women &amps; Men T-Shirts are Available for Campaign Fundraising Initiatives</p>
      		<div class="row">
      		
 			 <div class="col-md-3">
    				<a href="#" class="thumbnail">
      					<img src="..." alt="...">
    				</a>
				</div>
  				
  				 <div class="col-md-3">
    				<a href="#" class="thumbnail">
      					<img src="..." alt="...">
    				</a>
				</div>
 				 
  				 <div class="col-md-3">
    				<a href="#" class="thumbnail">
      					<img src="..." alt="...">
    				</a>
				</div>
  				
  					 <div class="col-md-3">
    				<a href="#" class="thumbnail">
      					<img src="..." alt="...">
    				</a>
				</div>
   				
				<p>Price:$20.00 <br>
					<br> 
   			<a class="btn btn-lg" href="https://kidzkorna.com/" role="button">Click here to order now</a> </p>
    				</div>
	  </div>
	</div>
</div> 

 <footer>
  <div class="row">
    <div class="text-center col-md-6 col-md-offset-3">
    
    <p class="copy">Copyright &copy; 2017 &middot; All Rights Reserved &middot; <a href="http://newmentoring.com/" >New Mentoring</a> &middot; Chicago, IL.</p>
    </div>
  </div>
	</footer>

	
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
<script src="js/jquery-1.11.3.min.js"></script>

<!-- Include all compiled plugins (below), or include individual files as needed --> 
<script src="js/bootstrap.js"></script>
</body>
</html>