我想更新数据库中的一栏,点击投票按钮

时间:2019-07-07 07:16:25

标签: php mysqli

我开始为基本投票系统编写代码。但是我收到了此错误消息。

  

警告:mysqli_query()期望参数1为mysqli,给定布尔值   在第6行的E:\ My-WORLD \ xampp \ htdocs \ php-tut \ voting \ vote.des.php中

<?php
  $con = mysqli_connect("localhost","root","","voting") || die("not 
 connected");
echo "connected";

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

   global $con;

   $vote_ele = "update votes set ele = ele + 1";
   $run = mysqli_query($con,$vote_ele);

   if($run){
      echo "voted to electronics";
   }
   else{
      echo mysqli_error($run);
   }

  }
 ?>

2 个答案:

答案 0 :(得分:0)

<div class="d-flex flex-column align-items-center h-100 customerForm"> <!-- logo --> <div class="justify-content-center d-flex"> <div [ngx-scroll-to]="'#destination'" class="tbtn">About us</div> <div [ngx-scroll-to]="'#howitwork'" class="tbtn">How It Works</div> </div> <div class="d-flex align-items-start pt-3 pr-3 pl-3"> <img src="assets/logo2.png" class="img-fluid p-3" alt="Responsive image" /> </div> <!-- request form --> <div class="w-100 pl-3 pr-3 d-flex justify-content-center"> <form [formGroup]="customerForm" (ngSubmit)="onSubmit()" class="pr-3 pl-3"> <div class="example-container"> <mat-form-field class="w-100"> <input matInput placeholder="Name" formControlName="name" /> </mat-form-field> <mat-form-field class="w-100"> <input matInput min="0" max="12" formControlName="phone" placeholder="Phone (only 11 digits)" value="03" (keypress)="numberOnly($event)" /> </mat-form-field> <mat-form-field class="w-100"> <input matInput placeholder="Email" formControlName="email" /> </mat-form-field> <mat-form-field class="w-100"> <mat-select placeholder="Location" formControlName="locations"> <mat-option *ngFor="let loc of locations" [value]="loc.location"> {{ loc.location }} </mat-option> </mat-select> </mat-form-field> <!-- <mat-form-field class="w-100"> <mat-select placeholder="Service" formControlName="service"> <mat-option *ngFor="let offer of service" [value]="offer.service">{{ offer.service }}</mat-option> </mat-select> </mat-form-field> --> </div> <mat-checkbox class="terms" formControlName="terms"> <div (click)="openBottomSheet()"> Accept our Terms & Conditions </div> </mat-checkbox> <div class="d-flex justify-content-center mt-3"> <button class="btn pl-4 pr-4" [disabled]="!customerForm.valid"> You Need Help! </button> </div> </form> </div> <!-- request form end --> </div> html, body { min-height:100%; height: 100%; font-family: 'Montserrat', sans-serif; } .wrapper{ // background: url('../src/assets/webbg.png') bottom center no-repeat; background-size: contain; } .bg{ background-position: bottom center; background-repeat: no-repeat; } .tbtn{ background: #283593; padding:20px 13px 5px; margin: 0.5em; border-radius: 0 0 0.4em 0.4em; color: #ffffff; font-size: 0.7em; cursor: pointer; text-align: center; } .btn{ color: #fff; border: 0!important; border-radius: 40px; transition: all .3s ease 0s; background: #082146!important; font-weight: 500; font-size: 0.9em; } .terms{ font-size: 0.8em; } .termsText p:last-child{ display: none!important; } .txtDelp p:last-child{ display: none!important; } .main{ // height: calc(100vh); height: 100%; } .g-block{ background: rgba(0, 0, 0, 0.1); height: 100%; width: 100%; } .about-text{ background: url(../src/assets/bgsign.png) center center no-repeat; background-size: contain; } .col-text{ flex-direction: column; color:#0c1f44; h3,h4{ font-size: 1.1em; font-weight: 700; text-transform: uppercase; color: inherit; } p{ font-size: 0.9em; } } .h-social .list-inline-item{ color: #082146; font-weight: 700; } .footer{ align-items: center; margin-top: 2rem; font-size: 0.8rem; } 均不得与任何mysqli函数关联写入。

How to properly connect with mysqli

||

鉴于此脚本不是唯一一个使用mysql连接的脚本,请将此代码存储在名为or的单独文件中,然后将其包含在其他文件中。因此,您当前的代码将变为

$host = '127.0.0.1';
$db   = 'voting';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, $charset);
} catch (\mysqli_sql_exception $e) {
     throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}

请参阅-简洁,整洁且可重复使用。

答案 1 :(得分:-2)

使用$con = mysqli_connect("localhost","root","","voting") || die("not connected");时,您正在将bool分配给$con变量。因此不再是mysqli

此外,当您使用global $con时,您正在覆盖$con的值,因此不再是您在上一行中分配的mysqli

尝试一下:

        $con = mysqli_connect("localhost", "root", "", "voting");

        if (mysqli_connect_errno())
            die("Failed to connect to MySQL: " . mysqli_connect_error());

        echo "connected";

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

            // This will override $con value!!
            // global $con;

            $vote_ele = "update votes set ele = ele + 1";
            $run = mysqli_query($con, $vote_ele);

            if ($run) {
                echo "voted to electronics";
            }
            else {
                echo mysqli_error($run);
            }

        }