fullyloaded Posted August 8, 2012 Share Posted August 8, 2012 Hey, I was wondering if its ok to have my code the way i have it below, 2 inserts one after the other? or is there a proper way of doing this any help would be great thanks. $query = "INSERT INTO users (user,pass,salt,email,firstname,lastname,gender,country,actnum ) VALUES (:user,:pass,:salt,:email,:firstname,:lastname,:gender,:country,:0)"; $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); $pass = hash('sha256', $_POST['pass'] . $salt); $query_params = array( ':user' => $_POST['user'], ':pass' => $pass, ':salt' => $salt, ':email' => $email, ':firstname' => $firstname, ':lastname' => $lastname, ':gender' => $gender, ':country' => $country, ':actnum' => $actnum ); try { $stmt = $db->prepare($query); $result = $stmt->execute($query_params); } catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); } $query = "INSERT INTO users_msg_sent (user,emails,bdaygreetings,totalsent ) VALUES (:user,:0,:0,:0)"; $query_params = array( ':user' => $_POST['user'], ':emails' => $emails, ':bdaygreetings' => $bdaygreetings, ':totalsent' => $totalsent ); try { $stmt = $db->prepare($query); $result = $stmt->execute($query_params); } catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); } Link to comment https://forums.phpfreaks.com/topic/266822-pdo-create-insert-question/ Share on other sites More sharing options...
requinix Posted August 8, 2012 Share Posted August 8, 2012 There's nothing wrong with having two INSERT statements... But there is something with your code: if the first statement fails the second still tries to get executed. Is that intentional? Link to comment https://forums.phpfreaks.com/topic/266822-pdo-create-insert-question/#findComment-1367874 Share on other sites More sharing options...
fullyloaded Posted August 8, 2012 Author Share Posted August 8, 2012 hi thanks for the reply how would i go about stopping the second one from being executed if the first one fails? again thanks. Link to comment https://forums.phpfreaks.com/topic/266822-pdo-create-insert-question/#findComment-1367877 Share on other sites More sharing options...
requinix Posted August 8, 2012 Share Posted August 8, 2012 Actually I'm wrong: I saw the try/catch but skipped past the "die" part (which means the script will just up and quit if there's a problem). Link to comment https://forums.phpfreaks.com/topic/266822-pdo-create-insert-question/#findComment-1367880 Share on other sites More sharing options...
fullyloaded Posted August 8, 2012 Author Share Posted August 8, 2012 ok thanks a lot for the help. Link to comment https://forums.phpfreaks.com/topic/266822-pdo-create-insert-question/#findComment-1367888 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.