data does not come at phpmysqladmin. and also shows this warning

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\com\com.php on line 75
Thank you for submission

<?php
if ( isset($_POST['username'])) {
	$ret = add_to_database();
	if (!$ret) {
		print "Error: Database error";
		} else {
		print "Thank you for submission";
		}
} else {
	write_form();
}

//functions
function write_form() {
	$self =$_SERVER['PHP_SELF'];
	echo <<<EOT
	<form action="$self" method="POST">
	<table>
	
	
	<tr>
	  <td>User Name:</td>
	  <td><input type="text" name="username" /></td><br/>
	</tr>
	<tr>
	  <td>E-mail:</td>
	  <td><input type="text" name="useremail" /></td><br/>
	</tr>
	 <tr>
	 <td>Comment:</td> <textarea cols="50" rows="8" wrap="VIRTUAL" name="comment"></textarea> 
	</tr>	 
	
	
	<tr>
	  <td>&nbsp</td>
	  <td><input type="submit" /></td>
	</tr>
	</table>
   </form>
EOT;
}

function add_to_database() {
	
	$username = trim($_POST['username']);
	$email = trim($_POST['useremail']);	
	$comments=trim($_POST['comment']);
	mysql_connect("localhost","root","") or die("Couldn't connect to server");
	mysql_select_db("comments");
	check_user($username);
	check_email($email);
	
	$sql = "INSERT INTO `dig_users` ('user_id',  'username', 'useremail','comment' , ) VALUES ('',  '$username', '$email','$comment')";
	mysql_query($sql);
	mysql_close();
	return true;
}

//this will check whether there is user with same user name
function check_user($usr) {
	
	$sql = "SELECT * FROM `dig_users` WHERE username='$usr'";
	$result = mysql_query($sql);
	$rows =mysql_num_rows($result);
	if ($rows>0) {
		print "The user $usr already exists. Please select another username.";
		exit;
	}
}
//checks that email is unique
function check_email($em) {
	
	$sql = "SELECT * FROM `dig_users` WHERE useremail='$em'";
	$result = mysql_query($sql);
	$rows =mysql_num_rows($result);
	if ($rows>0) {
		print "The e-mail address $em already exists. Please type another e-mail address.";
		exit;
	}
}

Normally that error comes just because the parameter you pass to the mysql_num_rows() is not a valid mysql resource object. mysql_query() returns false when there is an error in executing the query. Double check the query string.

I am not sure if this is the problem as you haven't put your coe in code tags in your post so I don't know where the line causing the error actually is, but you have an error here (an extra comma after the 'comment' column name):

$sql = "INSERT INTO `dig_users` ('user_id', 'username', 'useremail','comment' , ) VALUES

Should be:

$sql = "INSERT INTO `dig_users` ('user_id', 'username', 'useremail','comment') VALUES

I would also recommend running your sql statement in the actual database when you get errors like this as it is easier to de-bug.

Change the relevant code to below and post error message here

$sql = "SELECT * FROM `dig_users` WHERE useremail='$em'";
$result = mysql_query($sql);
if(!$result)
die(mysql_error());

Thanks for your great help. I am totally novice at php mysql. After your suggestion this php code does not show any warning but it still contains this below problem.

Username blank, useremail blank and comment also blank at phpmysqladmin . why? At this problem data does not show anything at phpmysqladmin. What's the problem?

Please visit to the link below given and kindly solve it.

username, email & comment blank at phpmyadmin

Which query are you trying to run? Is the insert query working? Are there any records in your table?

Which query are you trying to run? Is the insert query working? Are there any records in your table?

I am trying to insert this query.

<?php
if ( isset($_POST)) {
$ret = add_to_database();
if (!$ret) {
print "Error: Database error";
} else {
print "Thank you for submission";
}
} else {
write_form();
}


//functions
function write_form() {
$self =$_SERVER;
echo <<<EOT
<form action="$self" method="POST">
<table>



<tr>
<td>User Name:</td>
<td><input type="text" name="username" /></td><br/>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="useremail" /></td><br/>
</tr>
<tr>
<td>Comment:</td> <textarea cols="50" rows="8" wrap="VIRTUAL" name="comment"></textarea>
</tr>



<tr>
<td>&nbsp</td>
<td><input type="submit" /></td>
</tr>
</table>
</form>
EOT;
}


function add_to_database() {


$username = trim($_POST);
$email = trim($_POST);
$comments=trim($_POST);
mysql_connect("localhost","root","") or die("Couldn't connect to server");
mysql_select_db("comments");
check_user($username);
check_email($email);


$sql = "INSERT INTO `dig_users` ('user_id',  'username', 'useremail','comment'  ) VALUES ('',  '$username', '$email','$comment')";
mysql_query($sql);
mysql_close();
return true;
}


//this will check whether there is user with same user name
function check_user($usr) {


$sql = "SELECT * FROM `dig_users` WHERE username='$usr'";
$result = mysql_query($sql);
$rows =mysql_num_rows($result);
if ($rows>0) {
print "The user $usr already exists. Please select another username.";
exit;
}
}
//checks that email is unique
function check_email($em) {



$sql = "SELECT * FROM `dig_users` WHERE useremail='$em'";


$result = mysql_query($sql);


if(!$result)


die(mysql_error());
$rows =mysql_num_rows($result);
#
if(is_resource($result))
{
if ($rows>0)
{
print "The e-mail address $em already exists. Please type another e-mail address.";
exit;
}



}
}
?>

Inserting this query i can not see any records in my table. have a look at this link:http://demacco.blogspot.com/

first use code tags. also in your script you dont do error checking may be assuming script is perfect. Please test the code and print any error

<?php
/*Set to error level to development modes*/
ini_set("display_errors", 1);
error_reporting(E_ALL);
if ( isset($_POST['username'])) {
$ret = add_to_database();
if (!$ret) {
print "Error: Database error";
} else {
print "Thank you for submission";
}
} else {
write_form();
}

//functions
function write_form() {
$self =$_SERVER['PHP_SELF'];
echo <<<EOT
<form action="$self" method="POST">
<table>


<tr>
<td>User Name:</td>
<td><input type="text" name="username" /></td><br/>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="useremail" /></td><br/>
</tr>
<tr>
<td>Comment:</td> <textarea cols="50" rows="8" wrap="VIRTUAL" name="comment"></textarea>
</tr>


<tr>
<td>&nbsp</td>
<td><input type="submit" /></td>
</tr>
</table>
</form>
EOT;
}

function add_to_database() {

$username = trim($_POST['username']);
$email = trim($_POST['useremail']);
$comments=trim($_POST['comment']);
mysql_connect("localhost","root","") or die("Couldn't connect to server");
mysql_select_db("comments");
check_user($username);
check_email($email);

$sql = "INSERT INTO dig_users (user_id, username, useremail,comment ) VALUES ('', '$username', '$email','$comment')";
echo $sql;
mysql_query($sql);
mysql_close();
return true;
}

//this will check whether there is user with same user name
function check_user($usr) {

$sql = "SELECT * FROM dig_users WHERE username='$usr'";
$result = mysql_query($sql);
if(!$result){
die("Function check_user failed: ".mysql_error());
}
$rows =mysql_num_rows($result);
if ($rows>0) {
print "The user $usr already exists. Please select another username.";
exit;
}
}
//checks that email is unique
function check_email($em) {


$sql = "SELECT * FROM dig_users WHERE useremail='$em'";

$result = mysql_query($sql);
if(!$result){
die("Function check_email failed: ".mysql_error());
}
 
$rows =mysql_num_rows($result);
#
if(is_resource($result))
{
if ($rows>0)
{
print "The e-mail address $em already exists. Please type another e-mail address.";
exit;
}


}
}
?>

first use code tags. also in your script you dont do error checking may be assuming script is perfect. Please test the code and print any error

<?php
/*Set to error level to development modes*/
ini_set("display_errors", 1);
error_reporting(E_ALL);
if ( isset($_POST['username'])) {
$ret = add_to_database();
if (!$ret) {
print "Error: Database error";
} else {
print "Thank you for submission";
}
} else {
write_form();
}

//functions
function write_form() {
$self =$_SERVER['PHP_SELF'];
echo <<<EOT
<form action="$self" method="POST">
<table>


<tr>
<td>User Name:</td>
<td><input type="text" name="username" /></td><br/>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="useremail" /></td><br/>
</tr>
<tr>
<td>Comment:</td> <textarea cols="50" rows="8" wrap="VIRTUAL" name="comment"></textarea>
</tr>


<tr>
<td>&nbsp</td>
<td><input type="submit" /></td>
</tr>
</table>
</form>
EOT;
}

function add_to_database() {

$username = trim($_POST['username']);
$email = trim($_POST['useremail']);
$comments=trim($_POST['comment']);
mysql_connect("localhost","root","") or die("Couldn't connect to server");
mysql_select_db("comments");
check_user($username);
check_email($email);

$sql = "INSERT INTO dig_users (user_id, username, useremail,comment ) VALUES ('', '$username', '$email','$comment')";
echo $sql;
mysql_query($sql);
mysql_close();
return true;
}

//this will check whether there is user with same user name
function check_user($usr) {

$sql = "SELECT * FROM dig_users WHERE username='$usr'";
$result = mysql_query($sql);
if(!$result){
die("Function check_user failed: ".mysql_error());
}
$rows =mysql_num_rows($result);
if ($rows>0) {
print "The user $usr already exists. Please select another username.";
exit;
}
}
//checks that email is unique
function check_email($em) {


$sql = "SELECT * FROM dig_users WHERE useremail='$em'";

$result = mysql_query($sql);
if(!$result){
die("Function check_email failed: ".mysql_error());
}
 
$rows =mysql_num_rows($result);
#
if(is_resource($result))
{
if ($rows>0)
{
print "The e-mail address $em already exists. Please type another e-mail address.";
exit;
}


}
}
?>

After checking this this notice shown: Notice: Undefined variable: comment in C:\xampp\htdocs\com\row1.php on line 57
INSERT INTO dig_users (user_id, username, useremail,comment ) VALUES ('', 'fatima05', 'msfatima05@gmail.com','')Thank you for submission

and comment records are not shown at table.

I am presuming the user_id is an auto_increment in your table and therefore you don't need to reference it in your INSERT query and as it is empty it is probably causing you a problem. Change your INSERT to:

$sql = "INSERT INTO dig_users (username, useremail, comment) VALUES ('$username', '$email', '$comment')";

I am presuming the user_id is an auto_increment in your table and therefore you don't need to reference it in your INSERT query and as it is empty it is probably causing you a problem. Change your INSERT to:

$sql = "INSERT INTO dig_users (username, useremail, comment) VALUES ('$username', '$email', '$comment')";

Notice: Undefined variable: comment in C:\xampp\htdocs\com\row1.php on line 57
INSERT INTO dig_users (username, useremail, comment) VALUES ('faiyad', 'faiyad7@gmail.com', '')Thank you for submission:'(

I am presuming the user_id is an auto_increment in your table and therefore you don't need to reference it in your INSERT query and as it is empty it is probably causing you a problem. Change your INSERT to:

$sql = "INSERT INTO dig_users (username, useremail, comment) VALUES ('$username', '$email', '$comment')";

Notice: Undefined variable: comment in C:\xampp\htdocs\com\row1.php on line 57
INSERT INTO dig_users (username, useremail, comment) VALUES ('faiyad', 'faiyad7@gmail.com', '')Thank you for submission

Please try running your insert query actually in the database and see what it says (i.e. does it work or does it throw up an error) - it may be that your comment column in the table is set to the wrong format.

Also, despite the Undefined Variable (which actually just means you haven't defined the $comment Variable in the script and is actually a very minor issue), is your data actually being inserted into your database?

Thank you very much all who help me to solve this problem. Special thanks to purplepixie and evstevemd and moderator . Daniweb is really friendly. I am going to mark this as solved.:) Doctorscare24

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.