PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
<?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);
?>
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.
okay.. you didn't specify the error output in the original post. Anyway, add a call to [php_man]mysql_error()[/php_man] for both error components of the queries. You may want to differenciate between the two queries to help track down which one is actually failing.
I went back in and looked at my table and realized I had a typo. Once I fixed the typo I tested the script and continued to get the same error. However, I browsed the data in my table and noticed it had added a record. So I took another look at the code and realized I didn't need the "FLUSH" section so I commented it out - tested it - and success!
Anyway - thanks for at least reading my post. I have just successfully submitted my first bit of content via a form I created. Now I'm off to see if I can pull it out. lol
But when I use their code it keeps crapping out on me. I get errors like:
Parse error: parse error in /home3/frapster/robfloyd-www/results.php on line 20
So what I've been doing is going to various resources and looking at how they execute something and trying it. For example the following fails on my server every time.
$dbcnx = @mysql_connect("localhost", $dbuser, $dbpass) or die('Error connecting to mysql');
Is the tutorial I'm using outdated? I love the format - it takes me through one piece at a time quite nicely and seems less confusing than others. But the code just doesn't seem to work. Am I dealing with an issue with their code or is it my server versions. I just tried to find the version numbers for both MySQL and PHP - but can't see them. I'll post them if I track them down.
The problem with that is that localhost isn't quoted so it treats it as a constant, 'localhost' would work fine though as that is treated as a string. If at the top of your script you set error reporting to a higher level, error_reporting(E_ALL); then you'de see:
Notice: Use of undefined constant localhost - assumed 'localhost' in ...
(that's when you forget to use quotes around localhost)
that's really odd. the tutorial I'm following doesn't appear to do that but I know I left the quotes off as a result of something I saw. oh well. good catch regardless - both sets of code work now. Thanks!