•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 370,613 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 2,116 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: 2162 | Replies: 12
![]() |
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
$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
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Rep Power: 2
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 $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
•
•
Join Date: Apr 2005
Location: Old Hampshire, Old England (LOL)
Posts: 11,938
Reputation:
Rep Power: 30
Solved Threads: 263
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 2:55 pm.
TRY MY SUGGESTIONS AT YOUR OWN RISK!
james.bennet1@ntlworld.com
james.bennet1@ntlworld.com
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:
Rep Power: 2
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 12:45 pm.
•
•
Join Date: Apr 2005
Location: Old Hampshire, Old England (LOL)
Posts: 11,938
Reputation:
Rep Power: 30
Solved Threads: 263
did you like my php diles by the way?
it was my first attempt
it was my first attempt
TRY MY SUGGESTIONS AT YOUR OWN RISK!
james.bennet1@ntlworld.com
james.bennet1@ntlworld.com
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
- 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 and MySQL Web Development (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: PHP problem - register page
- Next Thread: Help



Linear Mode