-4

I am pulling my hair out here. I have an html form for data entry and posting to insert_ac.php and I get this error:

Warning: mysql_query() expects parameter 1 to be string, resource given in /home/content/52/11733052/html/admin/insert_ac.php on line 24

Here is the code from insert_ac.php:

<?php

     $username = "nlladmin";
     $password = "password";
     $hostname = "localhost"; 

     $link = mysql_connect($hostname, $username, $password) 
             or die("Unable to connect to MySQL");

     $selected = mysql_select_db("nlladmin",$link) 
             or die("Could not select Admin Database");

    // Get values from form 
     $make=$_POST['make'];
     $model=$_POST['model'];

    // Insert data into mysql 
      $sql="INSERT INTO assets (make, model)VALUES('$make', '$model')";
       if (!mysql_query($link,$sql))
     {
          die('Error: ' . mysql_error($link));
     }

    echo "1 record added";

    mysql_close();
    ?>

Any suggestion will be helpful.

Johan
  • 74,508
  • 24
  • 191
  • 319
Brent
  • 1
  • 1
  • In this case: RTFM. Then RTFEM (read the fine error message) again. Please. Really. – deceze Sep 12 '13 at 08:54
  • A: you should not be using the mysql_ functions, they are depreciated (for a very good reason). B: you have a SQL injection hole, see: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?lq=1, If you must insist on using the mysl_ lib, learn the use `mysql_real_escape_string`. – Johan Sep 12 '13 at 09:08

3 Answers3

1

Besides that you should not be using mysql_ functions,

you swapped the parameters in

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

That's what the error msg says.

if (!mysql_query($sql, $link))
....

works.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
0

The first parameter is the query, the second the resource.

mysql_query($sql, $link);
davey
  • 1,801
  • 2
  • 17
  • 22
-1

the function mysql_query() just takes the query as a parameter , like the following :

mysql_query($sql);
Leo Bali
  • 309
  • 1
  • 8