2

Possible Duplicate:
How to successfully rewrite old mysql-php code with deprecated mysql_* functions?

I have not been able to insert the values into the databse . However I do not get any error message as well .

<html>
<body>
<form action="database.php" method="post">
Name : <input type ="text" name = "name"/>
Number  :<input type ="text" name = "number"/>
<input type ="submit" value = "submit"/>
</form>
</body>
</html>

database.php

<?php
class Database
{
    var $host;
    var $user;
    var $pass;
    var $data;
    var $con;
    var $table;
    var $db;

    public function controls()
    {
        $this->host="localhost";
        $this->user="cgiadmin";
        $this->pass="cgi";
        $this->data="j2";
    }

    public function connection()
    {
        $this->con="mysql_connect($this->host,$this->user,$this->pass)";
    }
    public function tablename()
    {
        $this->table="Insert into employee(name,number) values('$_POST[name]','$_POST[number]')";
    }
    public function databaseconnection()
    {
        $this->db="mysql_select_db($this->data,$this->con)";
    }

}
$name=new Database;
$name->connection();
if(!($name->con))
{
    echo "'Error: ' . mysql_error()";
}

$name->databaseconnection();
$name->tablename();

echo "thanks for taking the survey";

?>
5
  • Why are you storing all of your function calls in strings? Commented Jun 14, 2012 at 6:14
  • 4
    Don't build SQL by mashing strings together Commented Jun 14, 2012 at 6:18
  • @k102 Where do I insert that query in my code ? Please help Commented Jun 14, 2012 at 6:33
  • @Blender I am new to this language . So finding it difficult to deal with it :( Commented Jun 14, 2012 at 6:34
  • @krish what changes should I do inorder to make it work Commented Jun 14, 2012 at 6:36

2 Answers 2

2

Try this...

Modify tablename() function

public function tablename($nam,$num)
    {
        $this->table=mysql_query("INSERT INTO employee(name,number) VALUES ('$nam','$num')");
    }

Get values and call tablename() function

$name=new Database;
$name->connection();
if(!($name->con))
{
    echo "'Error: ' . mysql_error()";
}

$name->databaseconnection();

$nam=$_POST[name];
$num=$_POST[number];
$name->tablename($nam,$num);

echo "thanks for taking the survey";
-3

You need some magic :) Read about quotes in php

<?php
class Database
{
    var $host;
    var $user;
    var $pass;
    var $data;
    var $con;
    var $table;
    var $db;

    public function controls()
    {
        $this->host="localhost";
        $this->user="cgiadmin";
        $this->pass="cgi";
        $this->data="j2";
    }

    public function connection()
    {
        $this->con = mysql_connect($this->host,$this->user,$this->pass);
    }
    public function tablename()
    {
        $this->table=mysql_query("INSERT INTO employee(name,number) VALUES ('".$_POST[name]."','".$_POST[number]."')");
    }
    public function databaseconnection()
    {
        $this->db=mysql_select_db($this->data,$this->con);
    }

}
$name=new Database();
$name->controls();
$name->connection();
if(!($name->con))
{
    echo 'Error: ' . mysql_error();
}

$name->databaseconnection();
$name->tablename();

echo "thanks for taking the survey";

?>
10
  • 5
    Holy SQL injection vulnerability, Batman! Commented Jun 14, 2012 at 6:21
  • @Ruben Nagoga I am getting an error message . Is it a problem with my database ? Error message is : Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: NO) in /home/jnagesh/public_html/SampleSurvey/MVC/database.php on line 20 Error: Access denied for user 'www-data'@'localhost' (using password: NO) Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/jnagesh/public_html/SampleSurvey/MVC/database.php on line 28 Commented Jun 14, 2012 at 6:29
  • Warning: mysql_query() [function.mysql-query]: Access denied for user 'www-data'@'localhost' (using password: NO) in /home/jnagesh/public_html/SampleSurvey/MVC/database.php on line 24 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/jnagesh/public_html/SampleSurvey/MVC/database.php on line 24 thanks for taking the survey Commented Jun 14, 2012 at 6:30
  • Is this a problem in program or an error in the database ? Commented Jun 14, 2012 at 6:30
  • @MichaelBorgwardt Could you please help me correct the error Commented Jun 14, 2012 at 6:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.