Welcome Guest, Not a member yet? Register   Sign In
How to test if INSERT worked?
#1

[eluser]yello[/eluser]
Hi

Code:
$entry['state'] = $_POST['state'];
            $entry['state_uri'] = url_title($_POST['state']);
            $entry['country'] = $_POST['country'];
            $entry['country_uri'] = url_title($_POST['country']);
            $entry['details'] = $_POST['details'];
            $entry['date'] = date("Y-m-d");
            /*var_dump($entry);*/

            if ($this->db->insert('users', $entry))
            {
                echo 'success';
            }
            else
            {
                echo("failed");
            }

If there is a duplicate entry in the insert, I want to redirect the user to my error page. Right now all they see is the CI error:

Quote:An Error Was Encountered

Error Number: 1062

Duplicate entry 'asdasdfdass' for key 2

INSERT INTO users (services, ip, username, username_uri, password, email, city, city_uri, state, state_uri, country, country_uri, details, date) VALUES ('xbox|xbox360|ps1|repairs', '127.0.0.1', 'asdasdfdass', 'asdasdfdass', '7815696ecbf1c96e6894b779456d330e', '[email protected]', 'sadas', 'sadas', 'quebec', 'quebec', 'Canada', 'canada', 'asdasd asd as', '2007-10-08')

Can anyone help?

Thanks Smile
#2

[eluser]sharpe[/eluser]
try this :

$data = array('name' => $name, 'email' => $email, 'url' => $url);

$str = $this->db->insert_string('table_name', $data);

if($str) {
...etc
}else {
... on duplicate
}

You have to set db debug to false

or

INSERT MyTable (MyKey, MyField1) VALUES (1, ‘Hi!’) ON DUPLICATE KEY UPDATE MyField1 = ‘Hi!’ WHERE MyKey = 1


see http://ellislab.com/forums/viewthread/61318/
#3

[eluser]llbbl[/eluser]
I like to do it myself.

Code:
$this->db->query("INSERT INTO `databasename` SET field1='$something', field2='$something', field3='$something'");

You should check to see if the duplicate entry exists already before you insert. It doesn't have to be every field you check right just the ones that matter.
#4

[eluser]yello[/eluser]
[quote author="sharpe" date="1191880268"]try this :

...
You have to set db debug to false
...

[/quote]

that fixed the problem Smile
#5

[eluser]Derek Allard[/eluser]
A different approach would be to use $this->db->insert_id(), but I agree with the methods above that checking if it exists first is probably the way to go.




Theme © iAndrew 2016 - Forum software by © MyBB