Page 1 of 1

[SOLVED] NEWBY: Error submitting content to db.

Posted: Thu Jan 06, 2005 8:48 am
by Frapster
feyd | Help us, help you. Please use

Code: Select all

and

Code: 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"> </td>
      <td> </td>
    </tr>
    <tr>
      <td width="100"> </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.


feyd | Help us, help you. Please use

Code: Select all

and

Code: 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]

oh yeah - my table structure

Posted: Thu Jan 06, 2005 8:50 am
by Frapster
I have a database on my localhost called 'robfloyd_com'. It has a table in it called 'test' and two fields labled 'first' and 'last'.

The URL I'm working with is http://www.robfloyd.com/links.php

Thanks,

Rob

feyd

Posted: Thu Jan 06, 2005 8:52 am
by Frapster
feyd: thanks and acknowledged!

Posted: Thu Jan 06, 2005 8:53 am
by feyd
since you don't have an action value in your form, are you sure it recieves the submission?

thanks - added an action

Posted: Thu Jan 06, 2005 8:58 am
by Frapster
feyd wrote:since you don't have an action value in your form, are you sure it recieves the submission?
I just added an action which calls 'links.php' and I continue to get:
Error, insert query failed

Posted: Thu Jan 06, 2005 9:08 am
by feyd
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.

it's working!

Posted: Thu Jan 06, 2005 9:08 am
by Frapster
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

Posted: Thu Jan 06, 2005 9:09 am
by feyd
congratulations.

good point

Posted: Thu Jan 06, 2005 9:11 am
by Frapster
it was actually both of them failing. lol

Thanks again!

rather than starting a new thread

Posted: Thu Jan 06, 2005 1:00 pm
by Frapster
I'm running into some interesting problems. I'm following the tutorial at:

http://www.php-mysql-tutorial.com/

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.

Code: Select all

$conn = mysql_connect(localhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
While this works.

Code: Select all

$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.

Posted: Thu Jan 06, 2005 1:03 pm
by markl999
$conn = mysql_connect(localhost, $dbuser, $dbpass)
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)

Posted: Thu Jan 06, 2005 2:25 pm
by Frapster
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!