$php_mysql$ Posted September 29, 2011 Share Posted September 29, 2011 guys this function below posts the data into table and im also able to send a link in email with caregory name but the issue is getting the id to the particular post i got no isea how do i get that? function insert($postData) { $postData['description'] = clean($postData['description']); if(!EmailExists($postData['email'])){ $sql = " INSERT INTO tbl_emails SET email = '".$postData['email']."', postersname = '".$postData['postersname']."', phone = '".$postData['phone']."' "; executeSql($sql); } if(empty($_FILES['image']["name"])){ $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '', postersname = '".$postData['postersname']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."', time = '".time()."' "; executeSql($sql); }else{ global $uploadPath; $remove_symbols = array('+', '=', '-', '{', '}', '$', '(', ')','&'); $removed_symbols = str_replace($remove_symbols, "_", $_FILES['image']['name']); $randomnum=rand(00000000,99999999); $imagepath = uploadFile($_FILES['image'], $uploadPath); $image = new SimpleImage(); $image->load($imagepath); $image->resize(250,280); $resize_rename = $uploadPath.$randomnum._.$removed_symbols; $image->save($resize_rename); unlink($imagepath); //delete the original file $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '".$resize_rename."', postersname = '".$postData['postersname']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['ocation']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."', time = '".time()."' "; executeSql($sql); } } Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/ Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 Have a read of this: mysql_insert_id() Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273858 Share on other sites More sharing options...
$php_mysql$ Posted September 29, 2011 Author Share Posted September 29, 2011 no i have not, could you please guide me with the usage of this function according to my function please? Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273862 Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 Basically, after you run your INSERT query (assuming it has an AUTO_INCREMENT field), if you do $inserted_id = mysql_insert_id(); $inserted_id will contain the ID of the newest record. Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273864 Share on other sites More sharing options...
$php_mysql$ Posted September 29, 2011 Author Share Posted September 29, 2011 Yes you are right it has a AUTO_INCREMENT field, so it should be something like this? function insert($postData) { $postData['description'] = clean($postData['description']); if(!EmailExists($postData['email'])){ $sql = " INSERT INTO tbl_emails SET email = '".$postData['email']."', postersname = '".$postData['postersname']."', phone = '".$postData['phone']."' "; executeSql($sql); } if(empty($_FILES['image']["name"])){ $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '', postersname = '".$postData['postersname']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."', time = '".time()."' "; $inserted_id = mysql_insert_id();////Does this give me the id now? executeSql($sql); }else{ global $uploadPath; $remove_symbols = array('+', '=', '-', '{', '}', '$', '(', ')','&'); $removed_symbols = str_replace($remove_symbols, "_", $_FILES['image']['name']); $randomnum=rand(00000000,99999999); $imagepath = uploadFile($_FILES['image'], $uploadPath); $image = new SimpleImage(); $image->load($imagepath); $image->resize(250,280); $resize_rename = $uploadPath.$randomnum._.$removed_symbols; $image->save($resize_rename); unlink($imagepath); //delete the original file $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '".$resize_rename."', postersname = '".$postData['postersname']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['ocation']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."', time = '".time()."' "; $inserted_id = mysql_insert_id();////Does this give me the id now? executeSql($sql); } } Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273865 Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 You will have to run it AFTER the SQL is executed. executeSql($sql); $inserted_id = mysql_insert_id(); Dont forget to return the value to whatever it is you need it to be given to. Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273867 Share on other sites More sharing options...
$php_mysql$ Posted September 29, 2011 Author Share Posted September 29, 2011 mate i tried like u said and tried to echo the var $inserted_id but nothing shows, and im kinda not sure return what value? Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273895 Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 How are you using your insert function? Is it supposed to send out the email aswell or is that later in the script. If you can show us the context in which the function is used it might help us understand. Also, is the executeSql function using the mysql_ or mysqli_ function set? Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273897 Share on other sites More sharing options...
$php_mysql$ Posted September 29, 2011 Author Share Posted September 29, 2011 hey mate, yes im planing to add the email function within the insert function. once user post is inserted it should imdtly send out an email with link to the post and to the email which the user posted in form. i tried it like this to get the id ///////////////////////////////////Insert function insert($postData) { $postData['description'] = clean($postData['description']); if(!ifEmailExists($postData['email'])){ $sql = " INSERT INTO tbl__emails SET email = '".$postData['email']."', postersname = '".$postData['postersname']."', phone = '".$postData['phone']."' "; executeSql($sql); } if(empty($_FILES['image']["name"])){ $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '', postersname = '".$postData['postersname']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."', time = '".time()."' "; executeSql($sql); $inserted_id = mysql_insert_id(); echo $inserted_id; /*Here ill add the email function with the link once i get the id*/ }else{ global $uploadPath; $remove_symbols = array('+', '=', '-', '{', '}', '$', '(', ')','&'); $removed_symbols = str_replace($remove_symbols, "_", $_FILES['image']['name']); $randomnum=rand(00000000,99999999); $imagepath = uploadFile($_FILES['image'], $uploadPath); $image = new SimpleImage(); $image->load($imagepath); $image->resize(250,280); $resize_rename = $uploadPath.$randomnum._.$removed_symbols; $image->save($resize_rename); unlink($imagepath); //delete the original file $sql = " INSERT INTO tbl SET title = '".$postData['title']."', image = '".$resize_rename."', postersname = '".$postData['postersname']."', category = '".$postData['category']."', type = '".$postData['type']."', state = '".$postData['state']."', location = '".$postData['location']."', email = '".$postData['email']."', phone = '".$postData['phone']."', description = '".$postData['description']."', time = '".time()."' "; executeSql($sql); $inserted_id = mysql_insert_id(); echo $inserted_id; /*Here ill add the email function with the link once i get the id*/ } } and my executeSql function is function executeSql($sql){ $conn = db_connect(); $result = mysql_query($sql, $conn) or die(mysql_error($conn)); return $result; } Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273914 Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 Hrmm.. if (executeSql($sql)) { $inserted_id = mysql_insert_id(); var_dump($inserted_id); } What does this output give you. Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273916 Share on other sites More sharing options...
$php_mysql$ Posted September 29, 2011 Author Share Posted September 29, 2011 no output , it says Query was empty Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273918 Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 Ok.. Somewhere along the line your script is failing and not getting to that point (i assume there is no entries in your database) Can you check that you have error_reporting turned on. error_reporting(E_ALL); ini_set('display_errors',1); Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273919 Share on other sites More sharing options...
$php_mysql$ Posted September 29, 2011 Author Share Posted September 29, 2011 maybe i did it wrong, is this what u asked me to do? /////////////////////////////////////SQL Execution function executeSql($sql){ $conn = db_connect(); $result = mysql_query($sql, $conn) or die(mysql_error($conn)); return $result; } if (executeSql($sql)) { $inserted_id = mysql_insert_id(); var_dump($inserted_id); } Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273925 Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 That code should replace executeSql($sql); $inserted_id = mysql_insert_id(); echo $inserted_id; Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273928 Share on other sites More sharing options...
$php_mysql$ Posted September 29, 2011 Author Share Posted September 29, 2011 oops ok did that and data is being inserted with no error Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273932 Share on other sites More sharing options...
$php_mysql$ Posted September 29, 2011 Author Share Posted September 29, 2011 ah it is working cheers :-) Quote Link to comment https://forums.phpfreaks.com/topic/248086-email-post-link-to-user-how-to-get-newest-id/#findComment-1273984 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.