简体   繁体   English

PHP - mySql 查询执行两次

[英]PHP - mySql Query executing twice

I am quite new to php and I am just trying my hands at a script, it is not aptly written however as it is vulnerable to SQL injection.我对 php 很陌生,我只是在尝试编写一个脚本,但它编写得并不恰当,因为它容易受到 SQL 注入的影响。 I intend to improve on that however that is only possible as I advance in PHP.我打算对此进行改进,但是这只有在我使用 PHP 时才有可能实现。 I am facing a problem currently when I try to POST variables from Java (Android) and use them to query my database.当我尝试从 Java (Android) POST 变量并使用它们查询我的数据库时,我目前面临一个问题。 However the script is executing twice, I find duplicate records in my database.但是脚本执行了两次,我在我的数据库中找到了重复的记录。 Following is the script:以下是脚本:

<?php

require 'DbConnect.php';


$Make = $_POST["Make"];
$Model = $_POST["Model"];
$Version= $_POST["Version"];
$FuelType= $_POST["FuelType"];
$Kilo = $_POST["Kilo"];
$Price= $_POST["Price"];
$Reg= $_POST["Reg"];
$Color= $_POST["Color"];
$Mdate= $_POST["Mdate"];
$Desc= $_POST["Desc"];
$Loc= $_POST["Loc"];
$Owners = $_POST["Owners"];
$Negot= $_POST["Negot"]; 
$Trans= $_POST["Trans"];
$AC= $_POST["AC"];
$car_lockk= $_POST["Lockk"];
$Sunroof= $_POST["Sunroof"];
$Window= $_POST["Window"];
$Seat= $_POST["Seats"];
$Stearing= $_POST["Stearing"];
$Music= $_POST["Player"];
$Wheels= $_POST["Wheel"];
$Sound= $_POST["Sound"];
$Drive= $_POST["Drive"]; 
$ID = $_POST["Seller_ID"];

$query2 = "INSERT INTO used_cars (make, model, version, color, \
    manufacturing_date, km_driven, fuel_type, expected_price, \
    negotiable, registration_place, no_of_owners, description, \
    current_location, transmission, ac, sunroof, window, seats, \
    stearing, player, wheels, sound_system, drive, car_lockk, seller_id) \
    VALUES ('$Make', '$Model', '$Version', '$Color', '$Mdate', '$Kilo', \
    '$FuelType', '$Price', '$Negot', '$Reg', '$Owners', '$Desc', '$Loc', \
    '$Trans', '$AC', '$Sunroof', '$Window', '$Seat', '$Stearing', \
    '$Music', '$Wheels', '$Sound', '$Drive', '$car_lockk', '$ID')";

if(mysql_query($query2)){
    echo 'success';
    //echo $Img
}else{
    echo 'Fail';
}

?> 

There is no reason for the code to be executed twice unless you are refreshing the page, or something in your connect script is causing it to happen.除非您正在刷新页面或连接脚本中的某些内容导致它发生,否则代码没有理由执行两次。

My recommendation is to slow down, your script is only a few lines yet with your original formatting it's barely readable.我的建议是放慢速度,您的脚本只有几行,但使用原始格式几乎无法阅读。 You have equals signs in different positions, useless white space and erratic spacing which I've attempted to edit out for the SO audience.你在不同的位置有等号,无用的空白和不稳定的间距,我试图为 SO 观众编辑掉。

Try to do things right the first time.第一次尝试做正确的事情。 Forego the mysql syntax, look up mysqli ( documentation & examples ) and implement your code using the object oriented interface -- it's much simpler.放弃mysql语法,查找mysqli文档和示例)并使用面向对象的接口实现您的代码——这要简单得多。

Your fixed code will look something like:您的固定代码如下所示:

<?php
    // Create DB connection object
    $mysqli = new mysqli("localhost","username","password","database");

    // Get our POST variables
    $make = $_POST["Make"];
    ... put them here ...
    $id = $_POST["Seller_ID"];

    // Create our base query and bind parameters
    $query = $mysqli->prepare("INSERT INTO used_cars (make, ..., id) VALUES (?, ..., ?)");
    $query->bind_param('s...i', $make, ..., $id);

    if($query->execute()) { // Will return true on success
        echo "Success";
    } else {
        echo "Fail";
    }
?>

The first argument to bind_param is a list of data types: s = string, i = int etc. You will need to list these correctly and in the right order. bind_param的第一个参数是数据类型列表:s = string、i = int 等。您需要以正确的顺序正确地列出这些。 Refer to the documentation if you need help.如果需要帮助,请参阅文档。 Binding parameters completely eliminates the possibility of an SQL injection attack and is the preferred way to use MySQL when passing user inputed values.绑定参数完全消除了 SQL 注入攻击的可能性,并且是在传递用户输入值时使用 MySQL 的首选方式。

On an unrelated note, typically in PHP we start variable names with a lowercase letter.在一个不相关的注释中,通常在 PHP 中,我们以小写字母开头变量名。 Uppercase letters are reserved for class names.为类名保留大写字母。

如果条件查询执行良好,那么页面将重定向到另一个页面。所以我们避免第二次插入数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM