Help with mysql error

Hi people

I am at frustration overflow…

Have an html form for data entry which posts to insert_ac.php, when I post the form I get this:

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:

[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'];
	 $assetnum=$_POST['assetnum'];
	 $registration=$_POST['registration'];

				 // Insert data into mysql 
	 $sql="INSERT INTO assets (make, model, assetnum, registration)VALUES('$make', '$model', '$assetnum', $registration')";
	 	if (!mysql_query($link,$sql))
                    {

die('Error: ’ . mysql_error($link));
}

echo "1 record added";

mysql_close();
?>[/php]

Any suggestions will be most appreciated.

Thanks

essenz

You are using mysql API to connect to the databases therefore you need to pass in the arguments in a way mysql accepts. Change.

[php]if (!mysql_query($link,$sql))[/php]

to

[php]if (!mysql_query($sql, $link))[/php]

Your problem will be solved.

Last thing I would suggest to use mysqli to connected to the databases instead of plain old mysql

Thanks tanzeelniazi
Got rid of that error and now I get this:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘003’)’ at line 1

The ‘003’)’ is data inserted at the html from.

Same code with your changes applied:

[php]<?php

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

	 $link = mysqli_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'];
	 $assetnum=$_POST['assetnum'];
	 $registration=$_POST['registration'];


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


	 echo "1 record added";


	 echo "<a href='assetdetails.php'>Back to main page</a>";

	 mysql_close();

?>[/php]

Thanks again.

Essenz

That’s good. You haven’t used spaces in )VALUES( and opening ’ is missing from $registration variable.

Change.
[php]$sql=“INSERT INTO assets (make, model, assetnum, registration)VALUES(’$make’, ‘$model’, ‘$assetnum’, $registration’)”;[/php]

to
[php]$sql=“INSERT INTO assets (make, model, assetnum, registration) VALUES (’$make’, ‘$model’, ‘$assetnum’, ‘$registration’)”;[/php]

WOW thanks…
how stupid am I? it is so easy to miss…

Yeah, these mistakes are very common. We all are victim of it but an error can be our be our best teacher, if we learn something from it. 8)

Sponsor our Newsletter | Privacy Policy | Terms of Service