dongqian1893
dongqian1893
2018-08-16 22:39

Php Mysql:Button没有插入数据库

已采纳

I am new here so apologies in advance if my explanations are not straightforward enough.

I am trying to insert information into the database via a button function but I am having no luck. I receive no errors so I'm guessing I have code in the wrong place but I cannot work it out. Thank you in advance for any help given.

<?php
session_start();
include("includes/db.php");
include("functions/functions.php");
?>

<?php


$tutorial_id = @$_GET['tut_id'];

$get_tutorials = "select * from tutorials where tutorial_url='$tutorial_id'";

$run_tutorials = mysqli_query($con,$get_tutorials);

$check_tutorials = mysqli_num_rows($run_tutorials);

if($check_tutorials == 0){

echo "<script> window.open('index.php','_self') </script>";

}
else{



$row_tutorials = mysqli_fetch_array($run_tutorials);

$tut_id = $row_tutorials['tutorial_id'];

$tut_title = $row_tutorials['tutorial_title'];

$tut_img1 = $row_tutorials['tutorial_img1'];

$tut_img2 = $row_tutorials['tutorial_img2'];

$tut_img3 = $row_tutorials['tutorial_img3'];

$tut_video = $row_tutorials['tutorial_video'];

$tut_keywords = $row_tutorials['tutorial_keywords'];

$status = $row_tutorials['status'];

$tut_url = $row_tutorials['tutorial_url'];
?>

<!DOCTYPE html>
<html>

<head>

<title> <?php echo $tut_title; ?> </title>

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" >

<meta name="keywords" content="<?php echo $tut_keywords; ?>" >

<meta name="author" content="YouCanCode" >

<link href="http://fonts.googleapis.com/css?family=Roboto:400,500,700,300,100" rel="stylesheet" >

<link href="styles/bootstrap.min.css" rel="stylesheet">

<link href="styles/style.css" rel="stylesheet">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">

</head>

<body>

<div id="top"><!-- top Starts -->

<div class="container"><!-- container Starts -->

<div class="col-md-6 offer"><!-- col-md-6 offer Starts -->

<a href="#" class="btn btn-success btn-sm" >
<?php
if(!isset($_SESSION['user_email'])){
echo "Welcome: Guest";
}else{
echo "Welcome: " . $_SESSION['user_email'] . "";
}
?>
</a>

</div><!-- col-md-6 offer Ends -->

<div class="col-md-6"><!-- col-md-6 Starts -->
<ul class="menu"><!-- menu Starts -->


<li>
<?php
if(!isset($_SESSION['user_email'])){
echo "<a href='login.php' >MY PROFILE</a>";
}
else{
echo "<a href='users/my_account.php?my_favourites'>MY PROFILE</a>";
}
?>
</li>


<li>
<?php

if(!isset($_SESSION['user_email'])){
echo "<a href='login.php'>LOGIN</a>";
}else {
echo "<a href='logout.php'>LOGOUT</a>";
}
?>
</li>

</ul><!-- menu Ends -->

</div><!-- col-md-6 Ends -->

</div><!-- container Ends -->
</div><!-- top Ends -->

<div class="navbar navbar-default" id="navbar"><!-- navbar navbar-default Starts -->
<div class="container" ><!-- container Starts -->

<div class="navbar-header"><!-- navbar-header Starts -->

<a class="navbar-brand home" href="index.php" ><!--- navbar navbar-brand home Starts -->
YouCanCode
</a><!--- navbar navbar-brand home Ends -->

<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navigation"  >

<span class="sr-only" >Toggle Navigation </span>

<i class="fa fa-align-justify"></i>

</button>

<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#search" >

<span class="sr-only" >Toggle Search</span>

<i class="fas fa-search" ></i>

</button>


</div><!-- navbar-header Ends -->

<div class="navbar-collapse collapse" id="navigation" ><!-- navbar-collapse collapse Starts -->

<div class="padding-nav" ><!-- padding-nav Starts -->

<ul class="nav navbar-nav navbar-left"><!-- nav navbar-nav navbar-left Starts -->

<li>
<a href="index.php"> Home </a>
</li>

<li class="active" >
<a href="tutorials.php">Tutorials</a>
</li>

<li>
<?php

if(!isset($_SESSION['user_email'])){
echo "<a href='login.php' >My Profile</a>";
}
else{
echo "<a href='users/my_account.php?my_favourites'>My Profile</a>";
}
?>
</li>

<li>
<a href="about.php"> About Us </a>
</li>

<li>
<a href="contact.php"> Contact Us </a>
</li>

</ul><!-- nav navbar-nav navbar-left Ends -->

</div><!-- padding-nav Ends -->

<div class="navbar-collapse collapse right"><!-- navbar-collapse collapse right Starts -->
<button class="btn navbar-btn btn-primary" type="button" data-toggle="collapse" data-target="#search">
<span class="sr-only">Toggle Search</span>
<i class="fa fa-search"></i>
</button>
</div><!-- navbar-collapse collapse right Ends -->

<div class="collapse clearfix" id="search"><!-- collapse clearfix Starts -->

<form class="navbar-form" method="get" action="results.php"><!-- navbar-form Starts -->

<div class="input-group"><!-- input-group Starts -->
<input class="form-control" type="text" placeholder="Search" name="user_query" required>
<span class="input-group-btn"><!-- input-group-btn Starts -->
<button type="submit" value="Search" name="search" class="btn btn-primary">
<i class="fa fa-search"></i>
</button>
</span><!-- input-group-btn Ends -->
</div><!-- input-group Ends -->

</form><!-- navbar-form Ends -->

</div><!-- collapse clearfix Ends -->

</div><!-- navbar-collapse collapse Ends -->

</div><!-- container Ends -->
</div><!-- navbar navbar-default Ends -->


<div id="content" ><!-- content Starts -->
<div class="container" ><!-- container Starts -->

<div class="col-md-12" ><!--- col-md-12 Starts -->

<ul class="breadcrumb" ><!-- breadcrumb Starts -->

<li>
<a href="index.php">Home</a>
</li>

<li><a href="tutorials.php">Tutorials</a></li>

<li> <?php echo $tut_title; ?> </li>

</ul><!-- breadcrumb Ends -->
</div><!--- col-md-12 Ends -->



<div class="col-md-12"><!-- col-md-12 Starts -->

<div class="row" id="tuorialtMain"><!-- row Starts -->

<div class="col-sm-6"><!-- col-sm-6 Starts -->

<div id="mainImage"><!-- mainImage Starts -->

<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">

<?php echo $tut_video; ?>
</div>
</div>
</div>

</div><!-- mainImage Ends -->
</div><!-- col-sm-6 Ends -->

<div class="col-sm-6" ><!-- col-sm-6 Starts -->

<div class="box" ><!-- box Starts -->

<h1 class="text-center" > <?php echo $tut_title; ?> </h1>


<p class="text-center buttons" ><!-- text-center buttons Starts -->

<button class="btn btn-primary" type="submit" name="add_favourite">
<i class="fas fa-heart"></i> Add To Favourites
</button>

<?php

if(isset($_POST['add_favourite'])){

if(!isset($_SESSION['user_email'])){

echo "<script>alert('You Must Be Logged In!')</script>";

echo "<script>window.open('login.php','_self')</script>";

}
else{

$user_session = $_SESSION['user_email'];

$get_user = "select * from users where user_email='$user_session'";

$run_user = mysqli_query($con,$get_user);

$row_user = mysqli_fetch_array($run_user);

$user_id = $row_user['user_id'];

$select_favourite = "select * from favourite where user_id='$user_id' AND tutorial_id='$tut_id'";

$run_favourite = mysqli_query($con,$select_favourite);

$check_favourite = mysqli_num_rows($run_favourite);

if($check_favourite == 1){

echo "<script>alert('This Tutorial Is Already In Your Favourites!')</script>";

echo "<script>window.open('$tut_url','_self')</script>";

}
else{

$insert_favourite = "insert into favourite (user_id,tutorial_id) values ('$user_id','$tut_id')";

$run_favourite = mysqli_query($con,$insert_favourite);

if($run_favourite){

echo "<script> alert('Tutorial Has Been Added To Favourites!') </script>";

echo "<script>window.open('$tut_url','_self')</script>";

}

}

}

}

?>

</p><!-- text-center buttons Ends -->

</form><!-- form-horizontal Ends -->

</div><!-- box Ends -->


<div class="row" id="thumbs" ><!-- row Starts -->

<div class="col-xs-4" ><!-- col-xs-4 Starts -->
<a href="#" class="thumb" >
<img src="admin_area/tutorial_images/<?php echo $tut_img1; ?>" class="img-responsive" >
</a>
</div><!-- col-xs-4 Ends -->

<div class="col-xs-4" ><!-- col-xs-4 Starts -->
<a href="#" class="thumb" >
<img src="admin_area/tutorial_images/<?php echo $tut_img2; ?>" class="img-responsive" >
</a>
</div><!-- col-xs-4 Ends -->

<div class="col-xs-4" ><!-- col-xs-4 Starts -->
<a href="#" class="thumb" >
<img src="admin_area/tutorial_images/<?php echo $tut_img3; ?>" class="img-responsive" >
</a>
</div><!-- col-xs-4 Ends -->


</div><!-- row Ends -->
</div><!-- col-sm-6 Ends -->


</div><!-- row Ends -->

<br><br>

<div class="col-md-3 col-sm-6"><!-- col-md-3 col-sm-6 Starts -->

<div class="box same-height headline"><!-- box same-height headline Starts -->

<h3 class="text-center">Check These Tutorials Out</h3>

</div><!-- box same-height headline Ends -->

</div><!-- col-md-3 col-sm-6 Ends -->

<?php

$get_tutorials = "select * from tutorials order by rand() LIMIT 0,3";

$run_tutorials = mysqli_query($con,$get_tutorials);

while($row_tutorials = mysqli_fetch_array($run_tutorials)) {

$tut_id = $row_tutorials['tutorial_id'];

$tut_title = $row_tutorials['tutorial_title'];

$tut_img1 = $row_tutorials['tutorial_img1'];

$language_id = $row_tutorials['language_id'];

$get_language= "select * from code_languages where language_id='$language_id'";

$run_language = mysqli_query($db,$get_language);

$row_language = mysqli_fetch_array($run_language);

$language_name = $row_language['language_title'];

$tut_url = $row_tutorials['tutorial_url'];


echo "

<div class='col-md-3 col-sm-6 center-responsive' >

<div class='tutorial' >

<a href='$tut_url' >

<img src='admin_area/tutorial_images/$tut_img1' class='img-responsive' >

</a>

<div class='text' >

<center>
<p class='btn btn-primary'> $language_name </p>
</center>

<hr>

<h3><a href='$tut_url' >$tut_title</a></h3>

<p class='buttons' >

<a href='$tut_url' class='btn btn-default' >View Tutorial</a>

</p>

</div>

</div>

</div>

";


}


?>


<?php } ?>

</div><!-- row same-height-row Ends -->
</div><!-- col-md-12 Ends -->

</div><!-- container Ends -->
</div><!-- content Ends -->

<?php
include("includes/footer.php");
?>

</body>
</html>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • drdzpknk76046 drdzpknk76046 3年前

    As Funk Forty Niner said you need a form to tell the button to send the POST array somewhere. You can wrap your button in this:

    <form action="" method="POST">
    Button code here
    </form> 
    

    You will also need to add a name like name="submit" to your button.

    You can then add this to your PHP code:

    if(isset($_POST['submit'])){
    Your PHP code here
    }
    

    Which will tell the PHP code to execute if the button is pressed.

    点赞 评论 复制链接分享

相关推荐