Author Topic: One field is not writing correctly  (Read 274 times)

0 Members and 1 Guest are viewing this topic.

Offline spocoTopic starter

  • Irregular
  • Posts: 35
    • View Profile
One field is not writing correctly
« on: February 23, 2009, 04:42:01 PM »
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
Code: [Select]
<?
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
Code: [Select]
<?
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);
?>

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: One field is not writing correctly
« Reply #1 on: February 24, 2009, 11:26:16 AM »
Don't just post your entire script -- no one has to time to read them.

What does "e-mail field not working" actually mean?

You're manually stitching together LOTS of fields without a DB class, so there are bound to be errors... yet you haven't provided us with the RAW query string that is being sent to the server.

Try again.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.