Hi,i'm new in php. :)
Now i have to use this as per project requirement.
So i just started to learn it as fast as possible.
As first attempt, i wanna add some data into mysql database table which has been created by MySQL Command Line Client.
I installed php5 , apache2.2 and PHP Coder Pro . As i'm window-base application programmer using .net, first i tested php code which print some string on localhost.That's fine. But now when i tried to insert data into mysql, i got some problem which keeps me hitting my head a few hours.
i can see some form, but after i fill in required data, webpage comes blank and mysql table is not actual filled by data. :-/
Is there some other configuration do i need to change in mysql or php or apache?
Here's my code.
I do hope somebody can help me out.
Thanks in advance.

///Insert_Test.php code///
<html>
<head>
<title>Add New Key Info</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
if(isset($_POST['add']))
{

include 'library/configDB.php';
//include 'library/openDB.php';

global $username;
global $password;

$username = $_POST["username"];
$password = $_POST["password"];

// test2 is my database table name
$query="INSERT INTO test2(username,password) VALUES ('$username', PASSWORD('$password'))";
mysql_query($query) or die('Error, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');

include 'library/closeDB.php';
echo "New MySQL data added";
}

else
{

?>
<form method="post">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Username</td>
<td><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td width="100">Password</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add New Data"></td>
</tr>
</table>
</form>
<?php
}

//echo '<pre>';
//echo print_r($_POST['add']);
//echo '</pre>';
//echo "
?>

</body>
</html>
<style type="text/css"> .style1 { width: 615px; } .style2 { text-align: right; width: 85px; } </style>
///configDB.php code///
<?php
$dbhost='localhost';
$dbuser='root';
$dbpassword='1234';
$dbname='test'; // this is database name
$connec=mysql_connect($dbhost,$dbuser,$dbpassword) or die ('Error Connecting to MySQL:');
mysql_select_db($dbname);
?>

i typed in my browser(IE) " http://localhost/Insert_Test.php "

i also set following configuration in my php.ini file
error_reporting = E_ALL
and
display_errors = On
and
extension=mysql.dll
But i still got above problem. Help me please :'(

Recommended Answers

All 2 Replies

Try this....

$query="INSERT INTO test2(´username´, ´password´) VALUES ('$username', '$password')";

Hmmm... Your code is fine.. But see at line 37.. You did not specify where the submitted values to go.. So only you have such problem. Try this..

<form method="post" action="">  // for passing the values to this same page.

Surely it will work. If still any probs, pls let me know..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.