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

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

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");
 
[B]if($_POST['process'] == 1){[/B]    
 
$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>
[B]<input type="hidden" name="process" value ="1" />[/B]
<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

Try some error checking on the mysql query:

eg:

$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;
}

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

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

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

crazynp :

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

thanks :cheesy:

did you like my php diles by the way?

it was my first attempt

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

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.