开发者

mysql_query(): supplied argument is not a valid MySQL-Link resource

I have been trying to insert a sample piece of data from my form, but I always get a syntax error executing 开发者_如何学C$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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜