Can somebody tell me what is wrong in this script


#1

I wrote this script to insert into a database but to my surprise it is not inserting into the database. Please guys help look and the code and if there need to be a correction please do and let me the errors.
Trust you guys sense of judgement
The Code:

<?php require_once("lib/db.php"); session_start(); $firstname=$_POST['fname']; $middlename=$_POST['mname']; $lastname=$_POST['lname']; $sex=$_POST['sex']; $dob=$_POST['dob']; $status=$_POST['status']; $address1=$_POST['address1']; $address2=$_POST['address2']; $city=$_POST['city']; $state=$_POST['state']; $country=$_POST['country']; $zipcode=$_POST['zipcode']; $cellphone=$_POST['cellphone']; $landphone=$_POST['landphone']; $email=$_POST['email']; $membershipstatus=$_POST['membershipstatus']; $workerstatus=$_POST['workerstatus']; $department=$_POST['dept']; if(isset($_POST['Submit'])){ if( $_POST['fname']=="" || $_POST['mname']==""|| $_POST['lname']==""|| $_POST['sex']==""|| $_POST['dob']==""|| $_POST['status']==""|| $_POST['address1']==""|| $_POST['address2']==""|| $_POST['city']==""|| $_POST['state']==""|| $_POST['country']==""|| $_POST['zipcode']=="" || $_POST['cellphone']==""|| $_POST['landphone']==""|| $_POST['email']==""|| $_POST['membershipstatus']==""|| $_POST['workerstatus']=="") { // header('Location:register.php'); } else{ $query="INSERT INTO onlineregistration(fname,mname,lname,sex,dob,status,address1,address2,city,state,country,zipcode,cellphone,landphone,email,membershipstatus,workers,dept) VALUES ($firstname,$middlename,$lastname,$sex,$dob,$status,$address1,$address2,$city,$state,$country,$zipcode,$cellphone,$landphone,$email,$membershipstatus,$workerstatus,$department)"; mysql_query($query); echo "you have successfully"; echo"proceed"; } ?>

#2

Why not try var_dump($_POST) to see the data that was actually posted. Use as

echo ‘< pre />’;
var_dump($_POST);
die;


#3

Oga Silas, am lost what do u think could be wrong with that code?


#4

What kind of error are you getting? You could post it on this thread and then we’ll be able to help you better.


#5

@ takinbo funny enough it is not generating any error, the message i asked to be echoed is working but it is not just inserting into the database

I have decided to put the form and the script up for check maybe I am the one that is wrong. Please it looks quite long but I trust u guys can help out

The form

<tr> 
  <td width="25%" height="30">First Name</td> 
  <td width="75%"><input name="fname" id="fname" type="text" value="" size="40"> 
    <span class="style1">*</span> </td> 
</tr> 
<tr> 
  <td height="29">Middle Name </td> 
  <td><input name="mname" type="text" id="mname" value="" size="40" /></td> 
</tr> 
<tr> 
  <td height="33">Last Name </td> 
  <td><input name="lname" type="text" id="lname" value="" size="40" /> 
  <span class="style1">*</span> </td> 
</tr> 
<tr> 
  <td height="29">Sex</td> 
  <td><select name="sex" id="sex">
        <option>-Select-</option>
        <option value="m">Male</option>
        <option value="f">Female</option>
      </select><span class="style1"> *</span></td> 
</tr> 
<tr> 
  <td height="30">Date of Birth (dd/mm/yy): </td>  
  <td><input type="text" name="dob" id="dob" size="30" />
   <span class="style1">*</span> </td> 
</tr> 
<tr> 
  <td height="32">Marital Status </td> 
  <td> <label>
            <select name="status" id="status">
              <option>-Select-</option>
              <option value="s">Single</option>
              <option value="m">Married</option>
              <option value="w">Widowed</option>
              <option value="S">Seperated</option>
              <option value="r">Remarried</option>
              </select>
            </label><span class="style1"> *</span> </td> 
</tr> 
<tr> 
  <td height="29">Address</td> 
  <td><input name="address1" type="text" id="address1" value="" size="40" /> 
  <span class="style1">*</span> </td> 
</tr> 
<tr> 
  <td height="29">&nbsp;</td> 
  <td><input name="address2" type="text" id="address2" value="" size="35" /></td> 
</tr> 
<tr> 
  <td>City</td> 
  <td><input name="city" type="text" id="city" value="" size="35" /> 
  <span class="style1">*</span> </td> 
</tr> 
<tr> 
  <td height="35">State</td> 
  <td><select name="state" id="state"> 
    <option value=""> State          </option> 
     
  </select> 
  <span class="style1">*</span> </td> 
</tr> 
<tr> 
  <td height="29">Country</td> 
  <td><select name="country" onClick="loadval();"id="country"> 
    <option value="">Country          </option> 
    
  </select> 
  <span class="style1">*</span> </td> 
</tr> 

<tr id="zipcode" style="display:none";> 
  <td height="30">Zipcode</td> 
  <td><input name="zipcode" type="text" id="zipcode"  value="" maxlength="8"/> 
  <span class="style1">*</span> </td> 
</tr> 

<tr> 
  <td height="31">Cell Phone</td> 
  <td><input name="cellphone" type="text" id="cellphone" value="" size="35" />
    <span class="style1">*</span></td> 
</tr> 
<tr> 
  <td height="28">Land Phone </td> 
  <td><input name="landphone" type="text" id="landphone" value="" size="35" /></td> 
</tr> 
<tr> 
  <td height="34">Email </td> 
  <td><input name="email" type="text" value="" size="35" id="email" />
    <span class="style1">*</span></td> 
</tr> 
<tr> 
  <td height="26">Membership Status </td> 
  <td><input name="membershipstatus" type="radio" value="M"    onclick="show_user_DIV('churchw');"/>
  <input type="hidden" name="membershipstatus" value="<?php echo 'M'?>" /> 
    Member 
    <input name="membershipstatus" type="radio"  value="NM"  onClick="hide_user_DIV('churchw');hide_user_DIV('dept');"/> 
    <input type="hidden" name="membershipstatus" value="<?php echo 'NM'?>" />
    Non-Member 
    <input name="membershipstatus" type="radio" value="V"    onclick="hide_user_DIV('churchw');hide_user_DIV('dept');"/> 
    <input type="hidden" name="membershipstatus" value="<?php echo 'V'?>" />
    Visitor
    <input name="membershipstatus" type="radio" value="FC"    onclick="hide_user_DIV('churchw');hide_user_DIV('dept');"/>
    <input type="hidden" name="membershipstatus" value="<?php echo 'FC'?>" /> 
    Friend of Congregation <span class="style1">*</span> </td> 
</tr> 

<tr id="churchw" style="display:none"> 
  <td height="27">Are you a Church Worker? </td> 
  <td><input name="workerstatus" type="radio" value="Y"   onclick="show_user_DIV('dept');"/> 
    Yes 
    <input name="workerstatus" type="radio" value="N"    onclick="hide_user_DIV('dept');"/> 
    No
    
    <span class="style1"> *</span> </td> 
</tr> 

<tr id="dept" style="display:none"> 
  <td>What Department Do You Work? </td> 
  <td><select name="dept" id="dept"> 
    <option value="">Select Department</option>
     <option value="Choir">Choir</option> 
    <option value="Ushering Department">Ushering Department</option> 
    <option value="IT/Maintenance Department">IT/Maintenance Department</option> 
    <option value="Childrens Department">Children's Department</option> 
    <option value="Education Department">Education Department</option> 
    <option value="Pastoral Care">Pastoral Care</option> 
    <option value="Prayer Band">Prayer Band</option> 
    <option value="Followup Department/Evangelism">Followup Department/Evangelism</option> 
    <option value="Other">Other</option> 
  </select> 
  <span class="style1">*</span> </td> 
</tr>

<tr> 
  <td>&nbsp;</td> 
  <td>&nbsp;</td>
</tr> 

<tr> 
  <td>&nbsp;</td> 
  <td><input type="submit" name="Submit" value="Submit"> 
    <input type="reset" name="Submit2" value="Reset"></td>
</tr> 
<tr> 
  <td height="19" colspan="2"><div align="center"></div></td> 
</tr> 

Now the Script

<?php require_once("lib/db.php"); if($_POST && !empty($_POST['fname']) && !empty($_POST['mname']) && !empty($_POST['lname']) && !empty($_POST['sex']) && !empty($_POST['dob']) && !empty($_POST['status']) && !empty($_POST['address1']) && !empty($_POST['address2']) && !empty($_POST['city']) && !empty($_POST['state']) && !empty($_POST['state']) && !empty($_POST['country']) && !empty($_POST['zipcode']) && !empty($_POST['cellphone']) && !empty($_POST['landphone']) && !empty($_POST['email']) && !empty($_POST['membershipstatus']) && !empty($_POST['workerstatus']) && !empty($_POST['other'])): $firstname=$_POST['fname']; $middlename=$_POST['mname']; $lastname=$_POST['lname']; $sex=$_POST['sex']; $dob=$_POST['dob']; $status=$_POST['status']; $address1=$_POST['address1']; $address2=$_POST['address2']; $city=$_POST['city']; $state=$_POST['state']; $country=$_POST['country']; $zipcode=$_POST['zipcode']; $cellphone=$_POST['cellphone']; $landphone=$_POST['landphone']; $email=$_POST['email']; $query="INSERT INTO onlineregistration(fname,mname,lname,sex,dob,status,address1,address2,city,state,country,zipcode,cellphone,landphone,email) VALUES ('$firstname','$middlename','$lastname','$sex','$dob','$status','$address1','$address2','$city','$state','$country','$zipcode','$cellphone','$landphone','$email')"; mysql_query($query); echo "submitted"; endif; ?>

#6

Firstly, always use mysql_affected_rows() to confirm your query as in

mysql_query($query);
if(mysql_affected_rows()){
echo “you have successfully”;
}

Perhaps you need to check you DB details and ur connectn.


#7

First of all the code above should have the variable names unquoted to look like this instead:
$query="INSERT INTO onlineregistration(fname,mname,lname,sex,dob,status,address1,address2,city,state,country,zipcode,cellphone,landphone,email) VALUES ('{$firstname}','{$middlename}','{$lastname}','{$sex}','{$dob}','{$status}','{$address1}','{$address2}','{$city}','{$state}','{$country}','{$zipcode}','{$cellphone}','{$landphone}','{$email}')";

*Yes you should include the curly braces.

Also, you could be having an invalid type in your data for a certain colum e.g. saving a string in an int field.
Rather than declare many variables like above, you could create an associative array with the field names as keys.
Something like:
$userdata = array(‘firstname’=>’’, ‘middlename’=>’’,…);

Sanitize the data by making sure the right type is in what you got.
Like so:
$userdata[‘firstname’] = trim($_POST[‘firstname’]);
For integers you can use an example like:
$userdata[‘id’] = (int)$_POST[‘id’];

Finally, make sure you addslashes if your data has quotes i.e. (’) in it.

I would suggest you use the PEAR database package for database queries.
Note: ext_mysql for php will soon be phased out to favour pdo_mysql.