[SOLVED] NEWBY: Error submitting content to db.
Posted: Thu Jan 06, 2005 8:48 am
feyd | Help us, help you. Please use
I'm fairly pleased with my progress but no matter what I do I end up with the code itself erroring out. For whatever reason it's not submitting to my database.
feyd | Help us, help you. Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I am teaching myself PHP and am making some progress following some tutorials - but for whatever reason I cannot get this form to submit properly. I am using a hosting service which has PHP 4 installed, MySQL and localhost is definitely the 'host' I should be using as far as I know.
I tried different versions of code to actually 'open' my database and it wasn't until I put $dbcnx in front of it that it finally worked. There's a ton I don't know so if I'm not providing enough info let me know - but the code I'm currently using is below. I've changed the username and pword.Code: Select all
<?php
//open database
$dbhost = 'localhost';
$dbuser = 'secret';
$dbpass = 'secret';
$dbcnx = @mysql_connect("localhost", $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'robfloyd_com';
mysql_select_db($dbname);
?>
<html>
<head>
<title>Add New MySQL User</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if(isset($_POSTї'add']))
{
$first = $_POSTї'first'];
$last = $_POSTї'last'];
$query = "INSERT INTO test (first, last) VALUES ('$first', '$last')";
mysql_query($query) or die('Error, insert query failed');
$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');
echo "A new name has been added.";
}
else
{
?>
<form method="post">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">First Name </td>
<td><input name="first" type="text" id="first"></td>
</tr>
<tr>
<td width="100">Last Name </td>
<td><input name="last" type="text" id="last"></td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td><input name="add" type="submit" id="add" value="Add Name"></td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
<?php
//close database
mysql_close($dbcnx);
?>feyd | Help us, help you. Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]