Hello i am designing a project which is intended to store the values entered in a browser into database. Finding some difficulties in displaying the database contents on browser.

Below i am posting my entire code. The data i enter in browser are getting inserted into the database. I checked it manually. But while displaying the contents on browser there are some errors that will be displayed. i am posting the error shown as well. please help me find out what is the problem

My code has two parts 1 which is php code deals with the database and 2 XML which deals with the front end


Php code:

<?php
$name=$_POST["name"];
$addressline1=$_POST["addressline1"];
$addressline2=$_POST["addressline2"];
$email=$_POST["email"];
$result=1;
$mysql=mysql_connect("localhost","root","")or die("connect error");
mysql_select_db("pruthvi");
$result=mysql_query("insert into form values('$name','$addressline1','$addressline2','$email')");
print "name=$name addressline1=$addressline1 addressline2=$addressline2 email=$email result=$result";
?>

<html>
<head>
<title>query DB</title>
</head>
<body>
<center><h3>
<? if($result==1) print "record inserted successfully"; ?>
</h3></center>
<h4>current DB contents</h4>
<? $result=mysql_query("select * from form"); ?>
<table border="1" align="center" width="500">
<tr>
<th>name</th>
<th>addressline1</th>
<th>addressline2</th>
<th>email</th>
</tr>
<? while($array=mysql_fetch_row($result))
{
	print "<tr>";
	print "<td>" . $array[0] . "</td>";
	print "<td>" . $array[1] . "</td>";
	print "<td>" . $array[2] . "</td>";
	print "<td>" . $array[3] . "</td>";
	print "</tr>";
}
mysql_free_result($result);
mysql_close($mysql);
?>
</table>
</body>
</html>

XML code:

<?xml version="1.0" encoding="utf-8" ?>
<?DOCTYPE HTML PUBLIC "-//w3c//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form action="http://localhost/13a.php" method="post">
<table width="500">
<tr>
<td>name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>addressline1</td>
<td><input type="text" name="addressline1"></td>
</tr>
<tr>
<td>addressline2</td>
<td><input type="text" name="addressline2"></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email"></td>
</tr>

</table>
<input type="submit" value="submit">
</form>
</body>
</html>

Error shown

name=pruthvi addressline1=kadur addressline2=bangalore email=ppp result=

current DB contents

"; print ""; print ""; print ""; print ""; print ""; } mysql_free_result($result); mysql_close($mysql); ?>

name addressline1 addressline2 email
" . $array[0] . " " . $array[1] . " " . $array[2] . " " . $array[3] . "


Please help how to debug this error and make the contents display in my browser.

Thanks,
Pruthvi

ok, first thing is your using $result twice, basically you have colliding variables. second thing is that your insert statement is incorrect. Whenever you use an insert statement you need to define the table fields, then use the "VALUES" to insert into the table fields.

$name=$_POST["name"];
$addressline1=$_POST["addressline1"];
$addressline2=$_POST["addressline2"];
$email=$_POST["email"];

$con = //Connection;
mysql_select_db("db", $con);
$sql = "INSERT into table(name,addressline1,addressline2,email) VALUES('$name','$addressline1','$addressline2','$email')";
if(mysql_query($sql,$con)) {
    die('Error: ' . mysql_error());
} else {
    print "name=$name addressline1=$addressline1 addressline2=$addressline2 email=$email ";
}

On the database display, you cannot use 0 or 1 or 2 or 3. define them from your table fields. Also, you have to use fetch_array not fetch_row. A really great website is www.w3schools.com

God i was so silly... :D Thanks a lot for correcting me sir. That corrected my code :) Many Thanks, Tak care:)

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.