0

Again guys here is same problem. Please check this code. Everything is OK. But sql data does not show up.

<?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 "Welcome   <b> $_POST[username] Check your email and download the books</b>";

}
} 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>&nbsp</td>
<td><input type="submit" /></td>
</tr>
</table>
</form>
EOT;
}

function add_to_database() {

$username = trim($_POST['username']);
$email = trim($_POST['useremail']);

mysql_connect("xxxx","xxxxx","xxxxx") or die("Couldn't connect to server");
mysql_select_db("xxxxxx");
check_user($username);
check_email($email);

$sql = "INSERT INTO dig_users(username, useremail,) VALUES ('$username', '$email',)";;
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;
}


}
}
?>

Edited by mike_2000_17: Fixed formatting

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by phorce
0
<?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 "Welcome   <b> $_POST[username] Check your email and download the books</b>";
	
}
} 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>&nbsp</td>
<td><input type="submit" /></td>
</tr>
</table>
</form>
EOT;
}

function add_to_database() {

$username = trim($_POST['username']);
$email = trim($_POST['useremail']);

mysql_connect("xxxx","xxxxx","xxxxx") or die("Couldn't connect to server");
mysql_select_db("xxxxxx");
check_user($username);
check_email($email);

$sql = "INSERT INTO dig_users(username, useremail) VALUES ('$username', '$email')";
$result = mysql_query($sql);
if(mysql_affected_rows() == 1)
{  
    echo 'Inserted';
}else{
  echo 'Could not insert';
}
  
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;
}


}
}
?>

Try that =)

Edited by Narue: Removed sensitive info

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.