PHP MySQL - Check for existing record and redirect according

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP MySQL - Check for existing record and redirect according

Post by onion2k »

hairytea wrote:I am confused as to why you asked me to remove value="Your E-Mail" from the form?
Because he's just guessing random things rather than reading the script and thinking about the problem.

The problem is that when you check to see if the email address is already in the database your code isn't working. Write something to test if the address is there, and then integrate that code into your script.

Code: Select all

$slq = "SELECT * FROM emails WHERE email_addresses='".trim($_POST["email"])."' ";
$ser = mysqli_num_rows($mysql, $slq);
Try echo'ing $ser and see what it's value is. I suspect it's going to be 0. I never use mysqli because I use database abstraction, but mysqli_num_rows() isn't in the PHP manual, so I think that's where your issue lies.
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: PHP MySQL - Check for existing record and redirect according

Post by hairytea »

onion2k wrote:
hairytea wrote:I am confused as to why you asked me to remove value="Your E-Mail" from the form?
Because he's just guessing random things rather than reading the script and thinking about the problem.

The problem is that when you check to see if the email address is already in the database your code isn't working. Write something to test if the address is there, and then integrate that code into your script.

Code: Select all

$slq = "SELECT * FROM emails WHERE email_addresses='".trim($_POST["email"])."' ";
$ser = mysqli_num_rows($mysql, $slq);
Try echo'ing $ser and see what it's value is. I suspect it's going to be 0. I never use mysqli because I use database abstraction, but mysqli_num_rows() isn't in the PHP manual, so I think that's where your issue lies.

thank you, but i think you too have neglected to read my post correctly....i know that's what i need to do.....i need to know how to do this?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP MySQL - Check for existing record and redirect according

Post by onion2k »

hairytea wrote:thank you, but i think you too have neglected to read my post correctly....i know that's what i need to do.....i need to know how to do this?
What I posted will help you solve the problem. I'm not going to solve it for you. I rarely post complete solutions because I don't think it's very helpful - it's much more helpful to you if you get to the solution yourself.
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: PHP MySQL - Check for existing record and redirect according

Post by hairytea »

thanks i appreciate what you're saying,

i appreciate your time here...my problem being....i tried many many times, and all attempts i have failed, so have got to desperation point now.....i have a book on php mysql and apache and have copied exactly the instructions for this function from there and still doesnt work.

if i had hair i would have pulled it all out by now lol

here is the code from the book....checked double checked triple checked quadrouple checked lol....it is identical and doesnt work..


<?php
function doDB;() {
global $mysqli;
$mysqli = mysqli_connect("localhost", "root", "terrence1", "mailing_list");

if (mysqli_errno()) {
printf("Connection Failed: %s\n", mysqli_connect_errno());
exit();
}
}

function emailChecker($email) {
global $mysqli, $check_res;

$check_sql = "SELECT id FROM emails WHERE email = '".$email."'";
$check_res = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli));
}

if (($_POST) && ($_POST["action"] == "sub")) {
doDB;
emailChecker($_POST["email"]);
if (mysqli_num_rows($check_res) < 1) {
mysqli_free_result($check_res);
$add_sql = "INSERT INTO emails (email) VALUES('".$_POST["email"]."')";
$add_res = mysqli_query($mysqli, $add_sql) or die (mysqli_error($mysqli));
header( "Location: http://localhost:8088/networking/htdocs ... ningup.htm" );
mysqli_close($mysqli);
} else {
header( "Location: http://localhost:8088/networking/htdocs ... cribed.htm" );
}
} else if (($_POST) && ($_POST["action"] == "unsub")) {
doDB;
emailChecker($_POST["email"]);
if (mysqli_num_rows($check_res) < 1) {
mysqli_free_result($check_res);
header( "Location: http://localhost:8088/networking/htdocs ... tabase.htm" );
} else {
while ($row = mysqli_fetch_array($check_res)) {
$id = $row["id"];
}
$del_sql = "DELETE FROM emails WHERE id = '".$id."'";
$del_res = mysqli_query($mysqli, $del_sql) or die (mysqli_error($mysqli));
header( "Location: http://localhost:8088/networking/htdocs ... cribed.htm" );
}
mysqli_close($mysqli);
}
?>
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: PHP MySQL - Check for existing record and redirect according

Post by hairytea »

FYI: PROBLEM SOLVED :lol:

Thanks for the advice onion2k!! it was great....keep trying myself until i find the solution and i learnt a lot i didn't know.....for those intrested the following script successfully

writes addresses to the databases
checks for duplicates and re-directs to error page without writing that address again to the database
deletes addresses from the database
warns if you try to delete an address from the database that wasn't there in the first place

oh im so happy

and here it is (after 6 hours of confusion)...

Code: Select all

 
 
<?php
function doDB() {
  global $mysqli;
  $mysqli = mysqli_connect("localhost", "root", "terrence1", "testDB");
 
  if (mysqli_errno()) {
  printf("Connection Failed: %s\n", mysqli_connect_errno());
  exit();
  }
}
 
function emailChecker($email) {
  global $mysqli, $check_res;
  
  $check_sql = "SELECT id FROM subscribers WHERE email = '".$email."'";
  $check_res = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli));
}
 
 if (($_POST) && ($_POST["action"] == "sub")) {
  if ($_POST["email"] == "") {
    header("Location: manage.php");
    exit;
  } else {
  doDB();
  emailChecker($_POST["email"]);
  if (mysqli_num_rows($check_res) < 1) {
    mysqli_free_result($check_res);
    $add_sql = "INSERT INTO subscribers (email) VALUES('".$_POST["email"]."')";
    $add_res = mysqli_query($mysqli, $add_sql) or die (mysqli_error($mysqli));
    header( "Location: http://localhost:8088/networking/htdocs ... ningup.htm" );
    mysqli_close($mysqli);
  } else {
    header( "Location: http://localhost:8088/networking/htdocs ... cribed.htm" );
  }
}
} else if (($_POST) && ($_POST["action"] == "unsub")) {
  if ($_POST["email"] == "") {
    header("Location: manage.php");
    exit;
  } else {
  doDB();
  emailChecker($_POST["email"]);
  if (mysqli_num_rows($check_res) < 1) {
    mysqli_free_result($check_res);
    header( "Location: http://localhost:8088/networking/htdocs ... tabase.htm" );
  } else {
    while ($row = mysqli_fetch_array($check_res)) {
      $id = $row["id"];
    }
    $del_sql = "DELETE FROM subscribers WHERE id = '".$id."'";
    $del_res = mysqli_query($mysqli, $del_sql) or die (mysqli_error($mysqli));
    header( "Location: http://localhost:8088/networking/htdocs ... cribed.htm" );
  }
  mysqli_close($mysqli);
  }
}
?>
 
 
:D :) :o :lol: :mrgreen: :drunk: :P
bhups
Forum Newbie
Posts: 1
Joined: Sat Dec 20, 2008 6:10 pm

Re: PHP MySQL - Check for existing record and redirect according

Post by bhups »

Personally what I did was:

Code: Select all

 
    // CHECK FOR EXISTING RECORD THEN ADD ELSE RETURN ERROR
    $sql = "SELECT * FROM tblproductlocation
            WHERE tblproductlocation.ProductID =  ".$lastadded."
            AND tblproductlocation.ProductLocID =  ".$locationid."";
    $result = $prodb->query($sql);
    $num_result = $result->num_rows;
    
    if ($num_result > 0)
    {
        echo '<h3>RECORD IS ALREADY ADDED!</H3>';
        echo 'You cannot add the same record. Please check the UPDATE LOCATION info link';
        exit;
    }
    else
    {
       DO WHAT YOU WANT!;
    }
 
Post Reply