0

I am currently trying to send data from an HTML form (Proform.html) to a MYSQL database. I cannot figure out how to solve this problem that keeps occurring as I am new to php the error message I keep receiving is.

"Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\php\Proform.php on line 22"

It could be possable as I have used several different references to create this page of code that i have mixxed two different realeases of php. Though help would be appriciated Anyways code is as follows.

    <?php

$dbname='*****';
$dbhost='localhost';
$dbpass='******';
$dbuser='******';






$dbhandle = mysql_connect($dbhost, $dbuser, $dbpass)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";


//select a database to work with
$selected = mysql_select_db("ecig",$dbhandle)
  or die("Could not select examples");

$q = $dbhandle->query("INSERT INTO Persons (First_Name, Last_Name)

VALUES ('$_POST[First_Name]', yes)");



if (array_key_exists ('check_submit', $_POST )) 

echo "Your Name is : {$_POST['First_Name']}<br />";
echo "Your Second Name is : {$_POST['Second_Name']}<br />";
echo "Your Email Address is : {$_POST['Email_Address']}<br />";
echo "Your Password Is : {$_POST['Password']}<br />";

?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – John Conde Mar 26 '13 at 03:58
  • FYI, you are also wide open to [SQL injections](http://stackoverflow.com/q/60174) – John Conde Mar 26 '13 at 03:58
  • the connection string is made with mysql which does not support database calls as an object. if you want to use it, use mysql_fetch_array but mysql calls are deprecated. Instead, I suggest switching to mysqli with $con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname); – Youn Elan Mar 26 '13 at 04:01

2 Answers2

1

There is a problem in your query

Why are you calling the query using the database connection string.

You have to call it like this:

$con=mysql_connect($dbhost,$dbuser,$dbpass,$dbname);
$res=mysql_query("INSERT INTO Persons (First_Name, Last_Name) VALUES('$_POST[First_Name]', yes)");
mysql_close();
n00begon
  • 3,503
  • 3
  • 29
  • 42
chintan
  • 471
  • 3
  • 16
0

mysql_connect does not return any object .... so it does not have any function or property that you can access using -> operator . It returns a connection identifier

replace

$dbhandle->query("INSERT INTO Persons (First_Name, Last_Name)
VALUES ('$_POST[First_Name]', yes)");`

with

mysql_query("INSERT INTO Persons (First_Name, Last_Name) 
VALUES ('$_POST[First_Name]', yes)")

I advice you to start learning mysqli or PDO since mysql functions are retarded and no longer maintained

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67