|
-
March 19th, 2010, 03:58 PM
#1
Can't insert form data into mysql database
There are two files, index.htm(form) and submit.php(code to insert form data into a database's table).
The database is created manually using HeidiSQL with appropriate table and columns.
But the problem is that after filling out the fields and clicking the submit button an empty screen appears and no data was inserted into the table of a database.
How fix?
index.htm code
Code:
<html>
<head>
<title>Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<form name="form1" action="submit.php" action="post">
<table>
<tr>
<th class="center" colspan="2">Form</th>
</tr>
<tr>
<td class="right">Field1</td><td><input type="text" name="field1" /></td>
</tr>
<tr>
<td class="right">Field2</td><td><input type="text" name="field2" /></td>
</tr>
<tr>
<td class="center" colspan="2"><input type="submit" value="Add" /></td>
</tr>
</table>
</form>
</body>
</html>
submit.php code
Code:
<?php
//Connect to mysql
$connect = mysql_connect("localhost","root","");
//If unable to connect, give me an explanation
if (!$connect)
{
die("Couldn't connect: " . mysql_error());
}
//If all is jolly, select the database using the current connection
mysql_select_db("db", $connect);
//Insert data into the table using input from the form page's fields
$sql = "INSERT INTO items (field1,field2) VALUES ('$_POST[field1]','$_POST[field2]')";
//If all fails, give me an explanation
if (!mysql_query($sql, $connect)
{
die('Error: ' . mysql_error());
}
//Otherwise congratulate me
echo "1 record added";
//Close connection
mysql_close($connect);
?>
-
March 19th, 2010, 04:27 PM
#2
-
March 20th, 2010, 11:39 AM
#3
Are you sure the database name and path are correct? The database is literally at the root of your C drive? And it's named db?
There is nothing to fear, but life itself.
-
March 20th, 2010, 01:50 PM
#4
 Originally Posted by JPnyc
Are you sure the database name and path are correct? The database is literally at the root of your C drive? And it's named db?
Database name yes, but I don't see where is the path indicator.
I'm using wamp server installed at C:\wamp\
-
March 22nd, 2010, 04:22 PM
#5
$connect = mysql_connect("localhost","root",""); is telling the program to look for the database at the very root of your file directory, which you're telling me is not where it's located. That should be changed to reflect the actual path C:\wamp\
There is nothing to fear, but life itself.
-
March 23rd, 2010, 09:20 AM
#6
 Originally Posted by JPnyc
$connect = mysql_connect("localhost","root",""); is telling the program to look for the database at the very root of your file directory, which you're telling me is not where it's located. That should be changed to reflect the actual path C:\wamp\
Okay. How would I do that?
Tried to do it like that...
Code:
$connect = mysql_connect("C:\wamp\","root","");
... and didn't work.
-
March 23rd, 2010, 10:33 AM
#7
$connect = mysql_connect("localhost","C:\wamp\","");
There is nothing to fear, but life itself.
-
March 23rd, 2010, 12:28 PM
#8
Code:
$connect = mysql_connect("localhost","C:\wamp\","");
Still doesn't add anything to the database and shows a blank page after submitting the form.
-
March 24th, 2010, 04:23 AM
#9
 Originally Posted by neshtak
Still...shows a blank page after submitting the form.
There are no html commands/structure being sent back to the browser from submit.php.
Format your VALUES like this: '{$_POST[field1]}','{$_POST[field2]}'
-
March 24th, 2010, 09:58 AM
#10
 Originally Posted by prouton
There are no html commands/structure being sent back to the browser from submit.php.
Format your VALUES like this: '{$_POST[field1]}','{$_POST[field2]}'
Tried it, but no luck.
Here's are the latest codes:
index.htm
Code:
<html>
<head>
<title>Welcome</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<form name="form1" method="post" action="submit.php">
Name<input type="text" name="name" /><br />
Age<input type="text" name="age" /><br />
<input type="submit" value="Add" />
</form>
</body>
</html>
submit.php
Code:
<?php
print_r($_POST);
$connect = mysql_connect("localhost","C:\wamp\","");
if (!$connect)
{
die("Couldnt connect" . mysql_error());
}
mysql_select_db("db", $connect);
$sql = "INSERT INTO table (name, password) VALUES ('{$_POST[name]}','{$_POST[age]}')";
echo $sql;
if (!mysql_query($sql, $connect))
{
die("Error: " . mysql_error());
}
echo "1 record added";
mysql_close($connect);
?>
-
March 24th, 2010, 01:25 PM
#11
First, you still haven't put any html commands into submit.php. What are you expecting the browser to do when it receives simple text? Add the html, head and body tags appropriately.
Second, you're passing the wrong values to the connect function. The three values are location ('localhost', since it's running on the same server as the php program), username (which in your original example was 'root'), and password. What user/password did you define when you created the database?
So if your database is named 'db' and the fields in table 'table' are named 'name' and 'password', then you should be good to go.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|