预期插入时插入数据库失败

时间:2016-12-14 20:24:41

标签: php mysql mysqli

我尝试在数据库中插入用户和cursus id,提交当前用户在第二部分中定义(请参阅注释),并且在文件顶部的require中创建与数据库的连接。 当我按下按钮时,它的回声成功,但在数据库中注意到了。控制台没有显示任何内容,当我回显$ a或& b时,它给我正确的ID。我做错了什么?

<?php
session_start();
require_once('includes/mysql_config.php');

//getting user data/id
$id = isset($_SESSION['id']) ? $_SESSION['id'] : header('location: login.php');
$result = mysqli_query($con, "SELECT * FROM users WHERE id =".$_SESSION['id']);
$user = mysqli_fetch_array($result);
require_once('header.php');

$id = $_GET['id'];
$result = mysqli_query($con, "SELECT * FROM cursus c LEFT OUTER JOIN cursussoort cr ON (c.cursussoort_ID = cr.id)  WHERE c.ID = $id ORDER BY c.begindatum ASC");
$amount = mysqli_affected_rows($con);

for ($i = 0; $i < $amount; $i++) {
  $cursus = mysqli_fetch_array($result);

$a = $cursus['id'];
$b = $user['id'];
//$a = mysqli_real_escape_string($a);
//$b = mysqli_real_escape_string($b);

  if(isset($_POST['submit'])){
      if($user['id']) {
          $sql = "INSERT INTO aanmeldingen (users_id, cursus_id) VALUES ('$b', '$a')";
          $result = mysqli_query($sql);
          echo "success";
      } else {
          echo "failed";
      }
  }

?>

<div class="main-nav">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <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="index.html">
        <h1><img class="img-responsive" src="images/logo.png" alt="logo"></h1>
      </a> -->
    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav navbar-right">
        <li class="scroll active"><a href="index.php#home">Home</a></li>
        <li class="scroll"><a href="index.php#services">Over ons</a></li>
        <li class="scroll"><a href="index.php#pricing">Cursussen</a></li>
        <li class="scroll"><a href="index.php#contact">Contact</a></li>
        <?php if(isset($_SESSION['id'])) {
          echo '<li><a href="myaccount.php"><span>Mijn account</span></a></li>';
          echo '<li><a href="logout.php"><span>Log Out</span></a></li>';
        } else {
          echo '<li><a href="login.php"><span>Log In</span></a></li>';
        } ?>
      </ul>
    </div>
  </div>
</div><!--/#main-nav-->
<br><br>
<!-- Post Content -->
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
          <div class="post-preview">
          <h1>  <?php echo $cursus['naam_cursus'] . "<br />";?></h1>
          van: <?php echo $cursus['begindatum'] . "<br />";?>
          tot: <?php echo $cursus['begindatum'] . "<br />";?>
            <?php echo $cursus['beschrijving'] . "<br />";?>
          </div>
        </div>
      </div>
      <form method="post">
        <input type="submit" name="submit">
      </form>
    </div>
  </div>
 <?php } ?>

<hr>

如果你们需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

使用您的代码:

$result = mysqli_query($sql);

mysqli_query()需要两个参数;连接和查询。尝试用此替换它并告诉我它是否解决了您的问题:

$result = mysqli_query($con, $sql);

马特