Jump to content

Need help with checking value in database..


DarkShadowWing

Recommended Posts

um, that link doesn't work. Do you get any errors on your page? try changing mysql_num_rows to mysql_affected_rows.

 

by the way, the code you currenly have doesn't check the right data

 

id  Firstname  Lastname  email 
1  test          user         blah@blah.blah
2  test          user         blah@blah.blah
3  test          user         blah@blah.blah
4       
5       
6       
7    

 

test and 'firstname' (which is what you test for) are not the same so it will allow you to update the table

Link to comment
Share on other sites

right. what i need is to check if the user has entered his or her name more than once, and has entered his or her email more than once, and submitted it more than once.

 

like if i enter test as my first name, and user for my last, it would check the database to see if "test" AND "user" BOTH EXIST, if they don't, then submit, else return an error to me :)

Link to comment
Share on other sites

make sure you check your posts before sticking them in the DB first...

 

run em through this function...

 

function safe($value){

   return mysqli_real_escape_string($con, $value);
} 

 

hes probably gonna wanna use mysql_real_escape_string seeing as he is using mysql functions. but yes this is a good idea

Link to comment
Share on other sites

right. what i need is to check if the user has entered his or her name more than once, and has entered his or her email more than once, and submitted it more than once.

 

like if i enter test as my first name, and user for my last, it would check the database to see if "test" AND "user" BOTH EXIST, if they don't, then submit, else return an error to me

Link to comment
Share on other sites

OK, so if i submit test as first name and user as lastname, if both exist in a row, then I can't submit, however, if 1 row has test as first name but poop as last name, I can still submit?

 

In that case change your or clauses (||) to and clauses (&&)

 

no. lol.

 

Ur right on the 1st part. u CANT submit if it finds "test" OR "user". but if it's "poop", and it finds "test" again, then it wont let it submit either.

Link to comment
Share on other sites

oh my bad I should have caught this earlier. You have to send the post variables to the function

 

recordExists('firstname', $_POST['firstname'], 'users0001','metaldetect01')

 

The 2nd parameter was the value of the column, and the first was the column itself right? Yeah you have to pass the post variables to the function. try it now

Link to comment
Share on other sites

Now it shows this in my db after 2 submits of "test", "user", and "email":

 

     id firstname lastname email
      1 test        user       blah@blah.blah 
      2 test        user       blah@blah.blah 
      3 test        user       blah@blah.blah 
      4       
      5       
      6       
      7       
      8       
      9       

 

The code:

 

if(recordExists('firstname', $_POST['firstname'], 'users0001','metaldetect01') || recordExists('lastname', $_POST['lastname'], 'users0001','metaldetect01') || recordExists('email', $_POST['email'], 'users0001','metaldetect01')){

echo "user: ".$firstname.", ".$lastname.", email: ".$email." already exists!";

}else{ 

$sql="INSERT INTO $tbl (firstname, lastname, email)
VALUES
('".safe($_POST['firstname'])."','".safe($_POST['lastname'])."','".safe($_POST['email'])."')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

}

 

It returned NOTHING this time for id 8, and id 9 (lastest 2 submits i made)

Link to comment
Share on other sites

Try something like the following

 

$sql_site = ("SELECT * FROM table WHERE `id` = '$_SESSION[id]' ");
$site_con = mysql_query($sql_site, $conn) or die(mysql_error());
$numrows = mysql_num_rows($site_con);

if($numrows > 0){
while($row = mysql_fetch_array($site_con))
{
echo "$row['row name']";
}

Link to comment
Share on other sites

Now its complaining about T_String ><

 

here:

 

<?php
$conn = "localhost";
$user = "myuser";
$pass = "mypass";
$dbname = "metaldetect01";
$tbl = "users0001";
$con = mysql_connect($conn,$user,$pass);
if (!$con)
  {
  die('Could not connect to database: "' . $dbname . '" because ' . mysql_error());
  }

mysql_select_db($dbname, $con);

$sql_site = ("SELECT * FROM $tbl WHERE `id` = $_SESSION[id] AND `firstname` = $_POST[firstname] AND `lastname` = $_POST[lastname] AND `email` = $_POST[email] ");
$site_con = mysql_query($sql_site, $conn) or die(mysql_error());
$numrows = mysql_num_rows($site_con);

//if(recordExists('firstname', 'firstname', 'users0001','metaldetect01') || recordExists('lastname', 'lastname', 'users0001','metaldetect01') || recordExists('email', 'email', 'users0001','metaldetect01')){

if($numrows > 0){
while($row = mysql_fetch_array($site_con))
{
echo "$row['row name']";

echo "user: ".$firstname.", ".$lastname.", email: ".$email." already exists!";

}else{ 

$sql="INSERT INTO $tbl (firstname, lastname, email)
VALUES
('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['email']."')";

echo "You were successfully added to the database, $firstname, $lastname!";
echo "Returning you to the previous page...";

echo "<script>setTimeout('document.location=\"./ThankYou.html\";', 10000);</script>";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

}

function safe($value){

   return mysqli_real_escape_string($con, $value);
} 

function recordExists($id,$idval,$table,$db) {//check for id=idval in table and return TRUE or FALSE
   $result = mysql_query("SELECT * FROM ".$table." WHERE ".$id."='".$idval."'") or die(mysql_error());
if(mysql_num_rows($result) > 0) {//if we found more than 0
      return true;
   }//end if row
   return false;
}

mysql_close($con)
?>

 

the line it complains about is:

 

if($numrows > 0){
while($row = mysql_fetch_array($site_con))
{
echo "$row['row name']";

echo "user: ".$firstname.", ".$lastname.", email: ".$email." already exists!";

}

 

the whole code part for that above is:

 

if($numrows > 0){
while($row = mysql_fetch_array($site_con))
{
echo "$row['row name']";

echo "user: ".$firstname.", ".$lastname.", email: ".$email." already exists!";

}else{ 

$sql="INSERT INTO $tbl (firstname, lastname, email)
VALUES
('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['email']."')";

echo "You were successfully added to the database, $firstname, $lastname!";
echo "Returning you to the previous page...";

echo "<script>setTimeout('document.location=\"./ThankYou.html\";', 10000);</script>";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

}

Link to comment
Share on other sites

Ok. its fixed but its STILL not detecting multiple user name records in the mysql database..

 

<?php
$conn = "localhost";
$user = "myuser";
$pass = "mypass";
$dbname = "metaldetect01";
$tbl = "users0001";
$con = mysql_connect($conn,$user,$pass);
if (!$con)
  {
  die('Could not connect to database: "' . $dbname . '" because ' . mysql_error());
  }

mysql_select_db($dbname, $con);

$sql_site = ("SELECT * FROM $tbl WHERE `id` = '$_SESSION[id]' AND `firstname` = '$_GET[firstname]' AND `lastname` = '$_GET[lastname]' AND `email` = '$_GET[email]' ");
$site_con = mysql_query($sql_site, $con) or die(mysql_error());
$numrows = mysql_num_rows($site_con);

if($numrows > 0){
while($row = mysql_fetch_array($site_con))
{

echo "user: ".$row['firstname'].", ".$row['lastname'].", email: ".$row['email']." already exists!";

}

}else{ 

$sql="INSERT INTO $tbl (firstname, lastname, email)
VALUES
('".$_GET['firstname']."','".$_GET['lastname']."','".$_GET['email']."')";

echo "You were successfully added to the database, ".$_GET['firstname'].", ".$_GET['lastname']."!";
echo "Returning you to the previous page...";

echo "<script>setTimeout('document.location=\"./ThankYou.html\";', 10000);</script>";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

}

function safe($value){

   return mysqli_real_escape_string($con, $value);
} 

function recordExists($id,$idval,$table,$db) {//check for id=idval in table and return TRUE or FALSE
   $result = mysql_query("SELECT * FROM ".$table." WHERE ".$id."='".$idval."'") or die(mysql_error());
if(mysql_num_rows($result) > 0) {//if we found more than 0
      return true;
   }//end if row
   return false;
}

mysql_close($con)
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.