Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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";

?>
share|improve this question
2  
and where is mysql_query? – k102 Jun 14 '12 at 6:14
Why are you storing all of your function calls in strings? – Blender Jun 14 '12 at 6:14
your query is not executed – kannan Jun 14 '12 at 6:16
4  
Don't build SQL by mashing strings together – Quentin Jun 14 '12 at 6:18
@k102 Where do I insert that query in my code ? Please help – Jathin Jun 14 '12 at 6:33
show 2 more comments

marked as duplicate by hakre, ircmaxell, Gordon, tereško, Madara Uchiha Jun 14 '12 at 12:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote -3 down vote accepted

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";

?>
share|improve this answer
4  
Holy SQL injection vulnerability, Batman! – Michael Borgwardt Jun 14 '12 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 – Jathin Jun 14 '12 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 – Jathin Jun 14 '12 at 6:30
Is this a problem in program or an error in the database ? – Jathin Jun 14 '12 at 6:30
@MichaelBorgwardt Could you please help me correct the error – Jathin Jun 14 '12 at 6:54
show 7 more comments

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";
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.