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:
Why not try var_dump($_POST) to see the data that was actually posted. Use as
echo ‘< pre />’;
var_dump($_POST);
die;
Oga Silas, am lost what do u think could be wrong with that code?
What kind of error are you getting? You could post it on this thread and then we’ll be able to help you better.
@ 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
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; ?>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.
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.