Click here to Skip to main content
15,886,058 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i am new to PHP. i am trying to create a page that will input data into
mysql database. i created a database called 'form1' and a table called 'demo'
with 2 columns ID(auto Increase) and input1(varchar).
i created a html file called demo-form with this codes

!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="gencyolcu" />

<title>Untitled 2</title>
</head>

<body>

<form action="demo.php" method="post" />

<p>Input 1: <input type="text" name="input1" /> </p>
<input type="submit" value="Submit" />
</form>

</body>
</html>

and a PHP file called 'demo' with code

php

/**
* @author gencyolcu
* @copyright 2015
*/

define('DB_NAME', 'form1');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

if (!$link) {

die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {

die('Can\'t use' . DB_NAME . ':' . mysql_error());
}

echo 'Connected successfully';

$value = $_POST['input1'];

$sql = "INSERT INTO demo (input1) VALUES ('$value')";

if (!mysql_query($sql)) {
die('Error: '. mysql_error());
}

mysql_close();

but after running the demo-form in my browser and i enter text into the
textbox and click submit.
no action takes please. dont really no what the problem is. please help me out
Posted
Comments
[no name] 24-Jun-15 0:33am    
http://php.net/manual/en/function.mysql-connect.php
mikeoabban 24-Jun-15 14:05pm    
i have tried using PDO and MySQLi to connect but the data is not able to store into the database

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
echo 'Connected to Database<br/>';

$value = $_POST['input1'];

$sql = "INSERT INTO demo (input1) VALUES ('$value')";



$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

?>

and this

host_info . "\n";

$mysqli->close();

?>

please help me out

1 solution

//Hi mikeoabban please try the following code, see some reference trough the link
http://www.phpeasystep.com/mysql/5.html[^]

PHP
/**
* @author gencyolcu
* @copyright 2015
*/

$hostname='localhost:3306';
$username='root';
$password='';

try {
$dbh = new PDO("mysql:host=$hostname;dbname=forms1",$username,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
echo 'Connected to Database<br />';

$value = $_POST['input1'];

$sql = "INSERT INTO demo (input1) VALUES ('$value')";

//Created by anand

//Execute command
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
   echo "Successful"; 
}else{
    echo $sql; //for debuging
}

//End


$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

?>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900