jakebur01 Posted February 23, 2009 Share Posted February 23, 2009 I am trying to write a PHP script that will write data to a MySQL database (obviously). However, the e-mail field is not working. (everything is NULL when data is entered). Is there something wrong in my coding? form page require_once("all_includes.php"); session_start(); check_valid_user(); $conn = new db_conn(); //get user id $id = $_SESSION['emp_sess_uid']; $count = 6; //get values for ($i=1; $i<=$count; $i++){ $name[$i] = addslashes(trim($_POST['ref_name' . $i])); $position[$i] = addslashes(trim($_POST['ref_pos' . $i])); $street[$i] = addslashes(trim($_POST['ref_street' . $i])); $city[$i] = addslashes(trim($_POST['ref_city' . $i])); $state[$i] = addslashes(trim($_POST['ref_state' . $i])); $zip[$i] = addslashes(trim($_POST['ref_zip' . $i])); $phone1_[$i] = addslashes(trim($_POST['ref_phone1_' . $i])); $phone2_[$i] = addslashes(trim($_POST['ref_phone2_' . $i])); $phone3_[$i] = addslashes(trim($_POST['ref_phone3_' . $i])); $phone[$i] = $phone1_[$i].$phone2_[$i].$phone3_[$i]; $email[$i] = addslashes(trim($_POST['ref_email' . $i])); } //do records already exist? for ($i=1; $i<=$count; $i++){ $ref_exists[$i] = $conn->record_exists_with_num('refs',$id,$i); //echo $i . ": ".$ref_exists[$i]; } //construct query //construct fail_to_reemploy queries for ($i=1; $i<=$count; $i++){ if (!$ref_exists[$i]){ $query = "insert into refs ("; $query .= "id, num, name, position, "; $query .= "street, city, state, zip, phone, email) "; $query .= "values ($id, "; $query .= $i . ", "; $query .= "'" . $name[$i] . "', "; $query .= "'" . $position[$i] . "', "; $query .= "'" . $street[$i] . "', "; $query .= "'" . $city[$i] . "', "; $query .= "'" . $state[$i] . "', "; $query .= "zip = " . $zip[$i] . ", "; $query .= "phone = " . $phone[$i] . " "; $query .= "email = " . $email[$i] . " "; $query .= " where id=$id "; } else{ $query = "update refs set "; $query .= "name = '" . $name[$i] . "', "; $query .= "position = '" . $position[$i] . "', "; $query .= "street = '" . $street[$i] . "', "; $query .= "city = '" . $city[$i] . "', "; $query .= "state = '" . $state[$i] . "', "; $query .= "zip = " . $zip[$i] . ", "; $query .= "phone = " . $phone[$i] . " "; $query .= "email = " . $email[$i] . " "; $query .= " where id=$id "; $query .= "and num=$i"; } //echo "<br>query: ".$query; //execute query $conn->query($query); } header("Location:".$page8); post page require_once("all_includes.php"); session_start(); check_valid_user(); $conn = new db_conn(); //get user id $id = $_SESSION['emp_sess_uid']; $count = 6; //get values for ($i=1; $i<=$count; $i++){ $name[$i] = addslashes(trim($_POST['ref_name' . $i])); $position[$i] = addslashes(trim($_POST['ref_pos' . $i])); $street[$i] = addslashes(trim($_POST['ref_street' . $i])); $city[$i] = addslashes(trim($_POST['ref_city' . $i])); $state[$i] = addslashes(trim($_POST['ref_state' . $i])); $zip[$i] = addslashes(trim($_POST['ref_zip' . $i])); $phone1_[$i] = addslashes(trim($_POST['ref_phone1_' . $i])); $phone2_[$i] = addslashes(trim($_POST['ref_phone2_' . $i])); $phone3_[$i] = addslashes(trim($_POST['ref_phone3_' . $i])); $phone[$i] = $phone1_[$i].$phone2_[$i].$phone3_[$i]; $email[$i] = addslashes(trim($_POST['ref_email' . $i])); } //do records already exist? for ($i=1; $i<=$count; $i++){ $ref_exists[$i] = $conn->record_exists_with_num('refs',$id,$i); //echo $i . ": ".$ref_exists[$i]; } //construct query //construct fail_to_reemploy queries for ($i=1; $i<=$count; $i++){ if (!$ref_exists[$i]){ $query = "insert into refs ("; $query .= "id, num, name, position, "; $query .= "street, city, state, zip, phone, email) "; $query .= "values ($id, "; $query .= $i . ", "; $query .= "'" . $name[$i] . "', "; $query .= "'" . $position[$i] . "', "; $query .= "'" . $street[$i] . "', "; $query .= "'" . $city[$i] . "', "; $query .= "'" . $state[$i] . "', "; $query .= "zip = " . $zip[$i] . ", "; $query .= "phone = " . $phone[$i] . " "; $query .= "email = " . $email[$i] . " "; $query .= " where id=$id "; } else{ $query = "update refs set "; $query .= "name = '" . $name[$i] . "', "; $query .= "position = '" . $position[$i] . "', "; $query .= "street = '" . $street[$i] . "', "; $query .= "city = '" . $city[$i] . "', "; $query .= "state = '" . $state[$i] . "', "; $query .= "zip = " . $zip[$i] . ", "; $query .= "phone = " . $phone[$i] . " "; $query .= "email = " . $email[$i] . " "; $query .= " where id=$id "; $query .= "and num=$i"; } //echo "<br>query: ".$query; //execute query $conn->query($query); } header("Location:".$page8); Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/ Share on other sites More sharing options...
jakebur01 Posted February 24, 2009 Author Share Posted February 24, 2009 bump. . Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770048 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 yep, you need to quote strings ie change $query .= "email = " . $email[$i] . " "; to $query .= "email = '" . $email[$i] . "' "; Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770054 Share on other sites More sharing options...
jakebur01 Posted February 24, 2009 Author Share Posted February 24, 2009 Wow! Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770059 Share on other sites More sharing options...
jakebur01 Posted February 24, 2009 Author Share Posted February 24, 2009 That isn’t working. But, all of the other fields are writing. Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770067 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 can you uncomment and post the results from //echo "<br>query: ".$query; also did you update BOTH of the email lines ? $query .= "email = " . $email[$i] . " "; Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770072 Share on other sites More sharing options...
jakebur01 Posted February 24, 2009 Author Share Posted February 24, 2009 I am starting to get lost in the code. I have attached the actual files. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770084 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 yep you missed the 2nd email line for ($i=1; $i<=$count; $i++){ if (!$ref_exists[$i]){ $query = "insert into refs ("; $query .= "id, num, name, position, "; $query .= "street, city, state, zip, phone, email) "; $query .= "values ($id, "; $query .= $i . ", "; $query .= "'" . $name[$i] . "', "; $query .= "'" . $position[$i] . "', "; $query .= "'" . $street[$i] . "', "; $query .= "'" . $city[$i] . "', "; $query .= "'" . $state[$i] . "', "; $query .= "zip = " . $zip[$i] . ", "; $query .= "phone = " . $phone[$i] . " "; $query .= "'" . $email[$i] . "' ";// <-------------you got this one $query .= " where id=$id "; } else{ $query = "update refs set "; $query .= "name = '" . $name[$i] . "', "; $query .= "position = '" . $position[$i] . "', "; $query .= "street = '" . $street[$i] . "', "; $query .= "city = '" . $city[$i] . "', "; $query .= "state = '" . $state[$i] . "', "; $query .= "zip = " . $zip[$i] . ", "; $query .= "phone = " . $phone[$i] . " "; $query .= "email = '" . $email[$i] . "' "; // <-------------you missed this one $query .= " where id=$id "; $query .= "and num=$i"; } echo "<br>query: ".$query; //execute query $conn->query($query); } Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770113 Share on other sites More sharing options...
jakebur01 Posted February 24, 2009 Author Share Posted February 24, 2009 We must have something else going on. I made that change and it wouldn’t write any of the data. Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770121 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 can you post the results from this line echo "<br>query: ".$query; Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770133 Share on other sites More sharing options...
jakebur01 Posted February 24, 2009 Author Share Posted February 24, 2009 query: update refs set name = 'ITD Testing', position = 'ITD Testing', street = 'ITD Testing', city = 'ITD Testing', state = 'AK', zip = 39042, phone = 5555555555 email = myemail@email.com where id=967 and num=1 query: update refs set name = 'ITD Testing', position = 'ITD Testing', street = 'ITD Testing', city = 'ITD Testing', state = 'MT', zip = 55555, phone = 5555555555 email = where id=967 and num=2 query: update refs set name = 'ITD Testing', position = 'ITD Testing', street = 'ITD Testing', city = 'ITD Testing', state = 'ND', zip = 55555, phone = 5555555555 email = where id=967 and num=3 query: update refs set name = 'ITD TestingITD Testing', position = 'ITD Testing', street = 'ITD Testing', city = 'ITD Testing', state = 'NC', zip = 55555, phone = 5555555555 email = where id=967 and num=4 query: update refs set name = 'ITD Testing', position = 'ITD Testing', street = 'ITD Testing', city = 'ITD Testing', state = 'MT', zip = 55555, phone = 5555555555 email = where id=967 and num=5 query: update refs set name = 'ITD Testing', position = 'ITD Testing', street = 'ITD Testing', city = 'ITD Testing', state = 'NC', zip = 55555, phone = 5555555555 email = where id=967 and num=6 Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770204 Share on other sites More sharing options...
cooldude832 Posted February 24, 2009 Share Posted February 24, 2009 you need to realize what you are doing wrong In MySQL You should follow these rules 1) Escape table and field names with ` ` (key to the left of 1 in QWERTY keyboard) 2) any value needs to be quoted with '' around the value (your phone/zip aren't) 3) Use break lines in your queries to help you decipher what it is saying. Do you see any errors now? Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770208 Share on other sites More sharing options...
jakebur01 Posted February 24, 2009 Author Share Posted February 24, 2009 I noticed I do not have a comma after phone. $query .= "phone = " . $phone[$i] . " "; should be $query .= "phone = " . $phone[$i] . ", "; Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770259 Share on other sites More sharing options...
MadTechie Posted February 24, 2009 Share Posted February 24, 2009 Cool.. anything else ? about the email "value" Quote Link to comment https://forums.phpfreaks.com/topic/146587-solved-data-is-not-writing-to-the-e-mail-field/#findComment-770262 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.