简体   繁体   English

我的mySQL查询出了什么问题?

[英]What is wrong my mySQL query?

I am trying to get my query to work and I can't seem to find what is wrong with it. 我试图使我的查询正常工作,但似乎找不到任何问题。 It is for a from btw. 它来自btw。

//Connection
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect.");
$selected = mysql_select_db("fblaWebsite",$dbhandle) or die("Could not select the database");

//execute the SQL query 
        $insertQuery = "INSERT INTO Bookings (band, occasion, placeName, address, city, state, email, firstName, lastName, comments, ticketsForSale, phoneNumber, ticketPrice, date, time)
        VALUES ('$band', '$occasion', '$placeName', '$address', '$city', '$state', '$email', '$firstName', '$lastName', '$comments', '$ticketsForSale', '$phoneNumber', '$ticketPrice', '$date', '$time')";
        if(mysql_query($insertQuery)){
            echo "Form Successfully Submited!";
        }
        else{
            echo "Error Submiting Form!";
        }

I can't seem to find the error. 我似乎找不到错误。 I just keep getting "Error Submiting Form!" 我只是不断收到“错误提交表单!”

As others have recommended, PDO would be very helpful for this. 正如其他人所建议的那样,PDO将对此非常有帮助。

However, since your question isn't using PDO, and I don't see the values for your variables, I would recommend that you call mysql_error() in your else condition, and output it to the screen (in development only!) in order to see what the last error from MySQL was. 但是,由于您的问题不是使用PDO,并且我看不到变量的值,因此建议您在else条件下调用mysql_error() ,并将其输出到屏幕中(仅在开发中!),为了查看来自MySQL的最后一个错误是什么。

That will give you a starting point for solving your particular problem. 这将为您解决特定问题提供一个起点。

I also second the recommendation about making sure you sanitize your input values, and ensure that no harmful values are being substituted into your sql statement. 我还建议您确保对输入值进行清理,并确保没有有害值被替换为sql语句。 If you aren't using it, may I recommend mysql_escape_string . 如果您不使用它,我可以推荐mysql_escape_string

Keep in mind that vanilla mysql drivers are slated for deprecation in 5.5. 请记住,计划在5.5中弃用香草mysql驱动程序。 Seriously consider upgrading to MySQLi or PDO to avoid your code breaking in PHP 5.5 environments! 认真考虑升级到MySQLi或PDO,以避免在PHP 5.5环境中破坏代码!

Here is PHP's FAQ on the deprecation and changing to one of the new mechanisms: Why is the MySQL extension (ext/mysql) that I've been using for over 10 years discouraged from use? 这是关于弃用并更改为新机制之一的PHP常见问题解答: 为什么不鼓励使用我已有10多年的MySQL扩展(ext / mysql)? Is it deprecated? 是否已弃用? What do I use instead? 我该怎么用呢? How can I migrate? 我该如何迁移?

The below code should get you started with connecting to PDO, and executing a simple insert query as with your example. 下面的代码将使您开始连接到PDO,并像示例一样执行简单的插入查询。

$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

try {
    //connect as appropriate as above
     $stmt = $db->prepare("INSERT INTO Bookings (band, occasion, placeName, address, city, state, email,     firstName, lastName, comments, ticketsForSale, phoneNumber, ticketPrice, date, time)     Values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?");
     $stmt->execute(array($band, $occasion, $placeName, $address, $city, $state, $email, $firstName, $lastName,     $comments, $ticketsForSale, $phoneNumber, $ticketPrice, $date, $time));
     $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $ex) {
    echo "An Error occured!"; //user friendly message
    //The line below will echo the error message from the query if there is one.
    //echo $ex->getMessage();
}

You don't need pdo to get an answer. 您不需要pdo即可获得答案。 Just use something like this. 只是使用这样的东西。 Use it for testing and it'll give you an idea where the error is. 使用它进行测试,它会告诉您错误在哪里。

$result = mysql_query($query) or die("Bad query 1: " .mysql_error());

Now... watch how many down votes I get for saying you don't need PDO. 现在...看看我说你不需要PDO会得到多少反对票。

You can do prepared statements in procedural code too. 您也可以在过程代码中执行prepared statements It is a good idea to learn that method or you'll be forever going back protecting against sql injection a dozen different ways. 学习该方法是一个好主意,否则您将永远回避十二种不同方法来防止sql注入。

edit: 编辑:

I forgot to say that you should move over to mysqli statements and away from the older depreciated mysql statements. 我忘了说,您应该移至mysqli语句,而不要使用旧的折旧mysql语句。 Chances are the example you found was 10 years old. 您发现10岁的例子很有可能。 Learning php and sql is a voyage of discovery, it helps if you get off on the wrong foot. 学习php和sql是发现的旅程,如果您走错了脚,它会有所帮助。 I'd take a look at this for some examples. 我来看一些示例。 http://us1.php.net/manual/en/mysqli.query.php http://us1.php.net/manual/en/mysqli.query.php

EDIT: 编辑:

As Dennis pointed out, you should probably learn Mysqli Prepared Statements instead. 正如Dennis指出的那样,您可能应该学习Mysqli Prepared Statements It would be a LOT smarter at this juncture and you can still use Procedural code. 在此时此刻会更聪明,您仍然可以使用过程代码。 Lots of examples here on Stackoverflow about how to do those. 在Stackoverflow上有很多有关如何执行这些操作的示例。 mysqli_stmt_fetch returns number mysqli_stmt_fetch返回数字

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

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