User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 402,374 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,096 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2296 | Replies: 12
Reply
Join Date: Nov 2004
Posts: 229
Reputation: sam1 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

writitng data from php to mysql

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

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

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

<html>
<head><title></title></head>
<body>
<?
include("connect.php");

if ($_POST['submit']) {

$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="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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 176
Reputation: vssp is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 4
vssp vssp is offline Offline
Junior Poster

Re: writitng data from php to mysql

  #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  
Join Date: Jan 2007
Posts: 58
Reputation: crazynp is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
crazynp crazynp is offline Offline
Junior Poster in Training

Re: writitng data from php to mysql

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

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

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  
Join Date: Apr 2005
Location: Old Hampshire, Old England (LOL)
Posts: 11,937
Reputation: jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough 
Rep Power: 30
Solved Threads: 268
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Microsoft Fanboy

Re: writitng data from php to mysql

  #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 2:55 pm.
Attached Files
File Type: zip dataentry.zip (2.1 KB, 20 views)
TRY MY SUGGESTIONS AT YOUR OWN RISK!
james.bennet1@ntlworld.com
Reply With Quote  
Join Date: Sep 2005
Posts: 674
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 5
Solved Threads: 39
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: writitng data from php to mysql

  #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  
Join Date: Nov 2004
Posts: 229
Reputation: sam1 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: writitng data from php to mysql

  #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  
Join Date: Jan 2007
Posts: 58
Reputation: crazynp is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
crazynp crazynp is offline Offline
Junior Poster in Training

Re: writitng data from php to mysql

  #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 12:45 pm.
Reply With Quote  
Join Date: Nov 2004
Posts: 229
Reputation: sam1 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: writitng data from php to mysql

  #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  
Join Date: Apr 2005
Location: Old Hampshire, Old England (LOL)
Posts: 11,937
Reputation: jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough jbennet is a jewel in the rough 
Rep Power: 30
Solved Threads: 268
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Microsoft Fanboy

Re: writitng data from php to mysql

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

it was my first attempt
TRY MY SUGGESTIONS AT YOUR OWN RISK!
james.bennet1@ntlworld.com
Reply With Quote  
Join Date: Nov 2004
Posts: 229
Reputation: sam1 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: writitng data from php to mysql

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 12:35 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC