writitng data from php to mysql

Reply

Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

writitng data from php to mysql

 
0
  #1
Mar 14th, 2007
hi,

it is giving me headache to write data from a php file to mysql( i can read from the table but cant write) here is my connect.php which connects to mysql:

  1. <?php
  2. $mysql_host = "localhost";
  3. $mysql_user = "sam";
  4. $mysql_pass = "123456";
  5. $mysql_data = "website";
  6. $mysql_table = "shoutbox";
  7. mysql_connect($mysql_host,$mysql_user,$mysql_pass) or die("Couldnt connect, try again. " . mysql_error());
  8. mysql_select_db($mysql_data) or die("Cannot select database! Please Try again." . mysql_error());
  9. ?>

here is the php file that should write it to table:

  1. <html>
  2. <head><title></title></head>
  3. <body>
  4. <?
  5. include("connect.php");
  6.  
  7. if ($_POST['submit']) {
  8.  
  9. $name = $_POST['name'];
  10. $url = $_POST['url'];
  11. $message = $_POST['message'];
  12. $date = date('jS \o\f F, Y \a\t g\:iA');
  13. $ip = $_SERVER['REMOTE_ADDR'];
  14.  
  15. if (!$name || !$message) {
  16. die ('You left a field blank. Please check again.');
  17. }
  18. else {
  19. mysql_query("INSERT INTO shoutbox (id,ip,name,url,message,date) VALUES('','$ip','$name','$url','$message','$date')") or die('Error inserting into DB.');
  20. }
  21. }
  22. ?>
  23.  
  24. <form name="shout" method="post" action="process.php">
  25. <div align="center"><iframe height="200" width="120" src="show.php" name="shoutbox"></iframe>
  26. <br>
  27. Name:<br>
  28. <input name="name" type="text" size="15" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">
  29. <br>
  30. URL:<br>
  31. <input name="url" type="text" size="15" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">
  32. <br>
  33. Message:<br>
  34. <textarea name="message" cols="15" wrap="VIRTUAL" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;"></textarea>
  35. <br>
  36. <input type="submit" name="Submit" value="Submit" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">
  37. </div></form>
  38. </body>
  39. </html>

can anyone please point out my mistake.

thank you
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 197
Reputation: vssp has a little shameless behaviour in the past 
Solved Threads: 5
vssp vssp is offline Offline
Junior Poster

Re: writitng data from php to mysql

 
0
  #2
Mar 15th, 2007
Can you send me the error message its very helpfull for easy to identify your mistake
Thanks
VSSP
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 58
Reputation: crazynp is an unknown quantity at this point 
Solved Threads: 2
crazynp crazynp is offline Offline
Junior Poster in Training

Re: writitng data from php to mysql

 
0
  #3
Mar 15th, 2007
does this work?

Originally Posted by sam1 View Post
hi,

it is giving me headache to write data from a php file to mysql( i can read from the table but cant write) here is my connect.php which connects to mysql:

  1. <?php
  2. $mysql_host = "localhost";
  3. $mysql_user = "sam";
  4. $mysql_pass = "123456";
  5. $mysql_data = "website";
  6. $mysql_table = "shoutbox";
  7. mysql_connect($mysql_host,$mysql_user,$mysql_pass) or die("Couldnt connect, try again. " . mysql_error());
  8. mysql_select_db($mysql_data) or die("Cannot select database! Please Try again." . mysql_error());
  9. ?>

here is the php file that should write it to table:

<html>
<head><title></title></head>
<body>
<?
include("connect.php");
 
if($_POST['process'] == 1){    
 
$name = $_POST['name'];
$url = $_POST['url'];
$message = $_POST['message'];
$date = date('jS \o\f F, Y \a\t g\:iA');
$ip = $_SERVER['REMOTE_ADDR'];
 
if (!$name || !$message) {
die ('You left a field blank. Please check again.');
}
else {
mysql_query("INSERT INTO shoutbox (id,ip,name,url,message,date) VALUES('','$ip','$name','$url','$message','$date')") or die('Error inserting into DB.');
}
}
?>
 
<form name="shout" method="post" action="process.php">
<div align="center"><iframe height="200" width="120" src="show.php" name="shoutbox"></iframe>
<br>
Name:<br>
<input name="name" type="text" size="15" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">
<br>
URL:<br>
<input name="url" type="text" size="15" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">
<br>
Message:<br>
<textarea name="message" cols="15" wrap="VIRTUAL" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;"></textarea>
<br>
<input type="hidden" name="process" value ="1" />
<input type="submit" name="Submit" value="Submit" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">
</div></form>
</body>
</html>

can anyone please point out my mistake.

thank you
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,144
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 530
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: writitng data from php to mysql

 
0
  #4
Mar 15th, 2007
i have included a zip file with php pages with the following:

data entry html form (its a simple adress book)

php script to process the form and put it into the database

php page showing a report of all the contacts in the database

(edit dbinfo.inc.php and specify your own database path, username and password to be correct for your database)

Make the mysql database first!

To create the table i have include a php page called tbl_create_script. run this after editing the dbinfo.inc.php file and it will make it for you

The code is small, simple and well layed out, perfect for studying
Last edited by jbennet; Mar 15th, 2007 at 3:55 pm.
Attached Files
File Type: zip dataentry.zip (2.1 KB, 21 views)
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: writitng data from php to mysql

 
0
  #5
Mar 16th, 2007
Try some error checking on the mysql query:

eg:

[php]
$result = mysql_query("INSERT INTO shoutbox (id,ip,name,url,message,date) VALUES('','$ip','$name','$url','$message','$date')") or die('Error inserting into DB.');

if ($result) {

// ok
echo "Mysql Insert ok";

} else {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}

[/php]

the mysql_query() will return false if the query fails, and the last error for your mysql session/connection will be returned by mysql_error()
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: writitng data from php to mysql

 
0
  #6
Mar 18th, 2007
thanks for the replies and advices everyone.

crazynp : i tried the values but didnt work.

digital-ether: i tried the error checking but no error is output in meanwhile no data is entered either.

jbennet: that was a good set of files for beginners. I did change the dbinfo.inc.php file, it did create the table when running the php file, but again i cannot insert any data to the table using the dataentry.html file...

I dont know what else to do, do you guys think it is something to do with the mysql version or it is a php error or mine ....as it retrieves data from table and creates table but not write any data...

thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 58
Reputation: crazynp is an unknown quantity at this point 
Solved Threads: 2
crazynp crazynp is offline Offline
Junior Poster in Training

Re: writitng data from php to mysql

 
0
  #7
Mar 18th, 2007
Can you try putting some value in the field 'id' in the INSERT query, just to check if that works or not?
Last edited by crazynp; Mar 18th, 2007 at 1:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: writitng data from php to mysql

 
0
  #8
Mar 18th, 2007
crazynp :

thanks alot mate, i put null instead of ' ' and it wrote id as value 1...

thanks :cheesy:
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,144
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 530
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: writitng data from php to mysql

 
0
  #9
Mar 20th, 2007
did you like my php diles by the way?

it was my first attempt
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: writitng data from php to mysql

 
0
  #10
Mar 20th, 2007
^^ really helpful and correct files mate. do u mean you are beginner and just did them or you mean in first try .

In both situation great help for beginners like me thank you
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC