473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

not writing data to db

I'm trying to add a guestbook to my website and got these scripts from the net (http://www.phpeasystep.com/workshopview.php?id=15) but only 1d and date&time are being recorded in the sql db; name, email and comments aren't.

any idea anyone please?

thx

SQL table creation script:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `guestbook` (
  2. `id` int(4) NOT NULL auto_increment,
  3. `name` varchar(65) NOT NULL default '',
  4. `email` varchar(65) NOT NULL default '',
  5. `comment` longtext NOT NULL,
  6. `datetime` varchar(65) NOT NULL default '',
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
guestbook.php:

Expand|Select|Wrap|Line Numbers
  1. <table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
  2. <tr>
  3. <td><strong>Sign Guestbook </strong></td>
  4. </tr>
  5. </table>
  6. <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  7. <tr>
  8. <form id="form1" name="form1" method="post" action="addguestbook.php">
  9. <td>
  10. <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
  11. <tr>
  12. <td width="117">Name</td>
  13. <td width="14">:</td>
  14. <td width="357"><input name="name" type="text" id="name" size="40" /></td>
  15. </tr>
  16. <tr>
  17. <td>Email</td>
  18. <td>:</td>
  19. <td><input name="email" type="text" id="email" size="40" /></td>
  20. </tr>
  21. <tr>
  22. <td valign="top">Comment</td>
  23. <td valign="top">:</td>
  24. <td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
  25. </tr>
  26. <tr>
  27. <td>&nbsp;</td>
  28. <td>&nbsp;</td>
  29. <td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
  30. </tr>
  31. </table>
  32. </td>
  33. </form>
  34. </tr>
  35. </table>
  36. <table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
  37. <tr>
  38. <td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
  39. </tr>
  40. </table>
addguestbook.php:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host="localhost"; // Host name
  3. $username="xxx"; // Mysql username
  4. $password="xxx"; // Mysql password
  5. $db_name="maxalien_davidloran"; // Database name
  6. $tbl_name="guestbook"; // Table name
  7.  
  8. // Connect to server and select database.
  9. mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
  10. mysql_select_db("$db_name")or die("cannot select DB");
  11.  
  12. $datetime=date("y-m-d h:i:s"); //date time
  13.  
  14. $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
  15. $result=mysql_query($sql);
  16.  
  17. //check if query successful
  18. if($result){
  19. echo "Successful";
  20. echo "<BR>";
  21. echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
  22. }
  23.  
  24. else {
  25. echo "ERROR";
  26. }
  27.  
  28. mysql_close();
  29. ?>
viewguestbook.php:

Expand|Select|Wrap|Line Numbers
  1. <table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
  2. <tr>
  3. <td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
  4. </tr>
  5. </table>
  6. <br>
  7.  
  8. <?php
  9.  
  10. $host="localhost"; // Host name
  11. $username="xxx"; // Mysql username
  12. $password="xxx"; // Mysql password
  13. $db_name="maxalien_davidloran"; // Database name
  14. $tbl_name="guestbook"; // Table name
  15.  
  16. // Connect to server and select database.
  17. mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
  18. mysql_select_db("$db_name")or die("cannot select DB");
  19.  
  20. $sql="SELECT * FROM $tbl_name";
  21. $result=mysql_query($sql);
  22.  
  23. while($rows=mysql_fetch_array($result)){
  24. ?>
  25. <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  26. <tr>
  27. <td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
  28. <tr>
  29. <td>ID</td>
  30. <td>:</td>
  31. <td><? echo $rows['id']; ?></td>
  32. </tr>
  33. <tr>
  34. <td width="117">Name</td>
  35. <td width="14">:</td>
  36. <td width="357"><? echo $rows['name']; ?></td>
  37. </tr>
  38. <tr>
  39. <td>Email</td>
  40. <td>:</td>
  41. <td><? echo $rows['email']; ?></td>
  42. </tr>
  43. <tr>
  44. <td valign="top">Comment</td>
  45. <td valign="top">:</td>
  46. <td><? echo $rows['comment']; ?></td>
  47. </tr>
  48. <tr>
  49. <td valign="top">Date/Time </td>
  50. <td valign="top">:</td>
  51. <td><? echo $rows['datetime']; ?></td>
  52. </tr>
  53. </table></td>
  54. </tr>
  55. </table>
  56. <BR>
  57. <?
  58. }
  59. mysql_close(); //close database
  60. ?>
Jan 9 '10 #1
2 5481
Markus
6,050 Expert 4TB
Hey, Macdalor.

Firstly, I recommend you turn on PHP debugging messages. I suspect if you had these enabled from the get-go, you would've been able to spot the errors yourself.

Now, on to the problems. The script you're using appears to rely on something called register globals - you'll notice the big red warning, indicating any modern PHP should not rely on this 'feature'. Register globals allows one to register all the super-global arrays as defined variables. That is, instead of having to do $_POST['var_name'] to access a variable in the POST array (submitted via HTML forms, typically), you can instead do $var_name. Obviously this poses many issues, one being multiple definitions of the same variable (var_name in $_SERVER and POST). Thankfully, however, you PHP configuration does not (appear) to have register_globals on. The fix is simple, instead of using $name, $email, etc., you should access them via the POST array.

Consider the following:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo $name;
  3. // would become:
  4. echo $_POST['name'];
  5.  
  6. echo $email;
  7. // would become:
  8. echo $_POST['email'];
  9.  
Have a go at correcting these issues.

Furthermore, you should take a look at this to protect yourself from SQL injection, which at the moment you are wide-open to.

Mark.
Jan 9 '10 #2
wooaaa GREAT ANSWER! thank you very much Markus! That is a very detailled and clear reply for newbies like me! well done!

now I should get to work and consider ll this ;-)

thx again!
Jan 9 '10 #3

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

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
12
by: Chris Springer | last post by:
I'd like to get some feedback on the issue of storing data out to disk and where to store it. I've never been in a production environment in programming so you'll have to bear with me... My...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: patrickdepinguin | last post by:
Hi, I need to write large quantities of data to a file in C. The data comes from statistics that are continuously gathered from a simulator, and in order to not slow the whole thing down I would...
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
2
by: =?Utf-8?B?S3VtYXI=?= | last post by:
I am using granados telnet client for connecting to the telnet and get the data from it. Every thing appears to be going smooth. But for some reason when I try to write the byte data to a string or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.