By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,967 Members | 1,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,967 IT Pros & Developers. It's quick & easy.

Beginners pb with PHP

P: 4
Hello and thanks in advance for any help.

I'm just starting out on PHP and read the very useful exercise at http://bytes.com/serversidescripting...ses/index.html

I turned on debugging as suggested and on running I got the following error messages:
Expand|Select|Wrap|Line Numbers
  1. Notice: Undefined variable: id in C:\Program Files\EasyPHP 2.0b1\www\test\add2.php on line 27
  2.  
  3. Notice: Undefined variable: name in C:\Program Files\EasyPHP 2.0b1\www\test\add2.php on line 27
  4.  
  5. Notice: Undefined variable: email in C:\Program Files\EasyPHP 2.0b1\www\test\add2.php on line 27
  6.  
  7. Notice: Undefined variable: opinion in C:\Program Files\EasyPHP 2.0b1\www\test\add2.php on line 27
  8.  
which relate to the line 27:
Expand|Select|Wrap|Line Numbers
  1. $sqlquery = "INSERT INTO $table VALUES('$id','$name','$email','$opinion')";
  2.  
So this suggests that I need to declare variables or that the html does not pass the data to the php script?

My final result does not as expected write to the table "information".

Thanks for your help in debugging as this seems to be a good exercise in learning the basics of writing to mysql data tables.

Regards

Mark
Sep 9 '08 #1
Share this Question
Share on Google+
5 Replies


code green
Expert 100+
P: 1,726
So this suggests that I need to declare variables or that the html does not pass the data to the php script?
Your second guess is almost certainly the problem,
Just relying on error reporting does not always reveal the picture.
It is advisable to echo the values of your variables at various stages.
[PHP]echo "<br>id $id name $name email $email opinion $opinion";[/PHP]
Sep 9 '08 #2

Atli
Expert 5K+
P: 5,058
Hi. Welcome to Bytes!

The problem is that that tutorial is using an old version of PHP. (PHP 4, I would guess).

In PHP5, value passed to PHP through HTML <input> elements must be retrieved from the $_POST array. (or $_GET if you used the GET protocol.)

So, you would either have to create the variables you use in your query, like so:
Expand|Select|Wrap|Line Numbers
  1. $value = $_POST['value'];
  2.  
Or simply replace the values inside the query with the $_POST array, like so:
Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT stuff FROM table WHERE value = '{$_POST['value']}'";
  2.  
I would recommend the former. Once you start using this is real applications you are going to want to validate the values before using them, so having them in variables is going to come in handy.
Sep 9 '08 #3

P: 4
Got it thanks. This does the trick
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // turns on or off debugging
  4. error_reporting(E_ALL);
  5. ini_set('display_errors', true);
  6.  
  7. // supposed to cause your script to halt when a MySQL query fails, and report the error message:
  8. //mysql_query($query, $link_id) or die('<hr />MySQL Error: ' .mysql_error(). '<hr />');
  9.  
  10. $DBhost = "127.0.0.1";
  11. $DBuser = "root";
  12. $DBpass = "";
  13. $DBName = "service_site";
  14. $table = "information";
  15. $name = $_POST["name"];
  16. $email = $_POST["email"];
  17. $opinion = $_POST ["opinion"];
  18.  
  19. mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
  20.  
  21. mysql_select_db("$DBName") or die("Unable to select database $DBName");
  22.  
  23. $sqlquery = "INSERT INTO $table VALUES('id','$name','$email','$opinion')";
  24.  
  25. $results = mysql_query($sqlquery);
  26.  
  27. mysql_close();
  28.  
  29. print "<HTML><TITLE> PHP and MySQL </TITLE><BODYBGCOLOR=\"#FFFFFF\"><center><table border=\"0\"width=\"500\"><tr><td>";
  30. print "<p><font face=\"verdana\" size=\"+0\"> <center>You Just Entered This Information Into the Database<p><blockquote>";
  31. print "Name : $name<p> E-Mail : $email<p>Opinion : $opinion</blockquote></td></tr></table></center></BODY></HTML>";
  32.  
  33. ?>
  34.  
Thanks and Regards

Mark
Sep 9 '08 #4

Dormilich
Expert Mod 5K+
P: 8,639
[PHP]$DBhost = "127.0.0.1";
$DBuser = "root";
$DBpass = "";
$DBName = "service_site";[/PHP]
I recommend not using a root account for db access, this could crash your db when using an incorrect administrative query.
Sep 9 '08 #5

Atli
Expert 5K+
P: 5,058
Mark,

Please use [code] tags when posting code examples.

[code].. code goes here... [/code]

Thanks.
Sep 9 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.