mysql_query():提供的参数不是有效的 MySQL-Link 资源

发布于 2024-11-09 11:34:24 字数 916 浏览 0 评论 0原文

我一直在尝试从表单中插入样本数据,但执行 $result 时总是出现语法错误。

来自index.php的相关部分:

<form method="post" action="form.php">
<ul >
    <li>
        <label for="accession_number">Accession Number</label>
        <input id="accession_number" name="accession_number" type="text" maxlength="6" value=""/> 
    </li>
</ul>
</form>

和来自form.php的部分:

<?php
$connection = mysql_connect($server, $username, $password) or die('Could not connect'.mysql_error());
mysql_select_db($database, $connection) or die("Cannot select db.");

$accession_number = $_POST['accession_number'];

$query = "INSERT INTO top (accession_number) ".
"VALUES ($accession_number)";
var_dump($query);
mysql_error();

$result = mysql_query($$query, connection) or die('Error querying database.');

mysql_close($connection);
?>

我不知道我做错了什么。

I have been trying to insert a sample piece of data from my form, but I always get a syntax error executing $result.

The relevant part from index.php:

<form method="post" action="form.php">
<ul >
    <li>
        <label for="accession_number">Accession Number</label>
        <input id="accession_number" name="accession_number" type="text" maxlength="6" value=""/> 
    </li>
</ul>
</form>

and parts from form.php:

<?php
$connection = mysql_connect($server, $username, $password) or die('Could not connect'.mysql_error());
mysql_select_db($database, $connection) or die("Cannot select db.");

$accession_number = $_POST['accession_number'];

$query = "INSERT INTO top (accession_number) ".
"VALUES ($accession_number)";
var_dump($query);
mysql_error();

$result = mysql_query($query, connection) or die('Error querying database.');

mysql_close($connection);
?>

I don't know what I'm doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱 2024-11-16 11:34:24

您的 mysql_query() 参数顺序错误。

$result = mysql_query($connection, $query)

应该是

$result = mysql_query($query, $connection)

http://php.net/manual/en/function.mysql-查询.php

资源mysql_query(字符串$query [,资源$link_identifier])

更新

当我说使用 mysql_error() 时,我的意思是仅当存在明显错误时。尝试这样的事情,

if (isset($_POST['accession_number'])) {
    $accession_number = $_POST['accession_number'];
    $query = sprintf('INSERT INTO `top` (accession_number) VALUES (%d)',
                     $accession_number);
    $result = mysql_query($query);
    if (false === $result) {
        throw new Exception('Error in query you have, hmmm: ' . mysql_error());
    }
    // and so on

我强烈建议完全放弃 MySQL 库并转向 PDO。写上面的代码让我感觉很肮脏。

Your parameters for mysql_query() are in the wrong order.

This

$result = mysql_query($connection, $query)

should be

$result = mysql_query($query, $connection)

http://php.net/manual/en/function.mysql-query.php

resource mysql_query ( string $query [, resource $link_identifier ] )

Update

When I said use mysql_error(), I meant only if there was an apparent error. Try something like this

if (isset($_POST['accession_number'])) {
    $accession_number = $_POST['accession_number'];
    $query = sprintf('INSERT INTO `top` (accession_number) VALUES (%d)',
                     $accession_number);
    $result = mysql_query($query);
    if (false === $result) {
        throw new Exception('Error in query you have, hmmm: ' . mysql_error());
    }
    // and so on

I highly recommend ditching the MySQL library entirely and moving to PDO. Writing the above code makes me feel dirty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文