| | |
writitng data from php to mysql
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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:
here is the php file that should write it to table:
can anyone please point out my mistake.
thank you
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)
<?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:
PHP Syntax (Toggle Plain Text)
<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
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Solved Threads: 2
does this work?
•
•
•
•
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)
<?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
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
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.
If i am helpful, please give me reputation points.
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()
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
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
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
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Solved Threads: 2
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.
![]() |
Similar Threads
- PHP and MySQL Web Development (PHP)
- Apache (Linux Servers and Apache)
- php/mysql configuration? queries do not return anything. (PHP)
- php/mysql coder for paid job (Web Development Job Offers)
- PHP-MySQL retrieval or getting data (PHP)
- php/mySQL full time position (Web Development Job Offers)
- Seeking PHP/MySQL Developers for MMORPG (Compensation) (Web Development Job Offers)
- Using PHP, MySQL and Apache Server (PHP)
Other Threads in the PHP Forum
- Previous Thread: coding for search
- Next Thread: Help
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code codingproblem cron curl database date directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select send server sessions sms snippet soap source space speed sql static structure syntax system table tutorial up-to-date update upload url validation validator variable video web wordpress xml youtube






