943,903 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3821
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 14th, 2007
0

writitng data from php to mysql

Expand 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:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Mar 15th, 2007
0

Re: writitng data from php to mysql

Can you send me the error message its very helpfull for easy to identify your mistake
Reputation Points: 5
Solved Threads: 5
Junior Poster
vssp is offline Offline
197 posts
since Jul 2006
Mar 15th, 2007
0

Re: writitng data from php to mysql

does this work?

Click to Expand / Collapse  Quote originally posted by sam1 ...
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:

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
crazynp is offline Offline
58 posts
since Jan 2007
Mar 15th, 2007
0

Re: writitng data from php to mysql

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
Attached Files
File Type: zip dataentry.zip (2.1 KB, 45 views)
Last edited by jbennet; Mar 15th, 2007 at 3:55 pm.
Moderator
Featured Poster
Reputation Points: 1784
Solved Threads: 574
Moderator
jbennet is offline Offline
16,520 posts
since Apr 2005
Mar 16th, 2007
0

Re: writitng data from php to mysql

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()
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Mar 18th, 2007
0

Re: writitng data from php to mysql

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
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Mar 18th, 2007
0

Re: writitng data from php to mysql

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
crazynp is offline Offline
58 posts
since Jan 2007
Mar 18th, 2007
0

Re: writitng data from php to mysql

crazynp :

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

thanks :cheesy:
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Mar 20th, 2007
0

Re: writitng data from php to mysql

did you like my php diles by the way?

it was my first attempt
Moderator
Featured Poster
Reputation Points: 1784
Solved Threads: 574
Moderator
jbennet is offline Offline
16,520 posts
since Apr 2005
Mar 20th, 2007
0

Re: writitng data from php to mysql

^^ 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
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: coding for search
Next Thread in PHP Forum Timeline: Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC