954,198 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

writitng data from php to mysql

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>

Name:
<input name="name" type="text" size="15" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">

URL:
<input name="url" type="text" size="15" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">

Message:
<textarea name="message" cols="15" wrap="VIRTUAL" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;"></textarea>

<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

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

Can you send me the error message its very helpfull for easy to identify your mistake

vssp
Junior Poster
199 posts since Jul 2006
Reputation Points: 5
Solved Threads: 5
 

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");
 
<strong>if($_POST['process'] == 1){</strong>    
 
$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>

Name:
<input name="name" type="text" size="15" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">

URL:
<input name="url" type="text" size="15" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">

Message:
<textarea name="message" cols="15" wrap="VIRTUAL" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;"></textarea>

<strong><input type="hidden" name="process" value ="1" /></strong>
<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

crazynp
Junior Poster in Training
58 posts since Jan 2007
Reputation Points: 10
Solved Threads: 2
 

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

Attachments dataentry.zip (2.11KB)
jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,820
Solved Threads: 600
 

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()

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

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

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

Can you try putting some value in the field 'id' in the INSERT query, just to check if that works or not?

crazynp
Junior Poster in Training
58 posts since Jan 2007
Reputation Points: 10
Solved Threads: 2
 

crazynp :

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

thanks :cheesy:

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

did you like my php diles by the way?

it was my first attempt

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,820
Solved Threads: 600
 

^^ 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 :)

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

People can easily come with sql-injections if you use that code.

You have to use for example mysql_real_escape_string or something else. Read about safety at php.net

Andy-Pandy
Newbie Poster
14 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

first try (well apart from phpinfo; - thats basically phps hello world)

i already knew SQL SELECT statements and basic HTML so i got a book on PHP and after seeing generally how things were done, i decided to make thay

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,820
Solved Threads: 600
 

if you are still having the problem, in which line the error is being indicated?

niceboy
Newbie Poster
19 posts since Jan 2006
Reputation Points: 9
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You