Newbie here with a question though I'm not sure if it belongs in the PHP forum or the MySQL one. I've been trying to insert data into MySQL via PHP and haven't been successful. A mysql_affected_rows() says "-1" records added, with no other error and no data inserted. My update queries all work fine. Here are the relevant snippets of code:
Connecting to DB:
Inside an if clause, this sets my variables that come from a form:
This part determines if we are adding or updating to the data. Since it's an add, the query lists all fields held in the table with the exception of the UserID which is an autoincrement. The fields below are not in the order they appear in the actual table and some fields are not set in the variables list above because they aren't in the form that sends them here.
--------------------
Not sure what I've done wrong here so any help would be greatly appreciated! Thanks!
Connecting to DB:
Code:
$dblocation = "localhost";
$dbusername = "dbusername";
$dbpassword = "dbpassword";
$database = "database";
$conn = mysql_connect("$dblocation","$dbusername","$dbpassword");
if (!$conn) die ("Could not connect MySQL");
mysql_select_db($database,$conn) or die ("Could not open database");
Inside an if clause, this sets my variables that come from a form:
Code:
$query_type=$_POST['query_type'];
$userid=$_POST['userid'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$department=$_POST['department'];
$phone=$_POST['phone'];
$tt=$_POST['tt'];
$news=$_POST['news'];
$whitepapers=$_POST['whitepapers'];
$dm=$_POST['dm'];
$calendar=$_POST['calendar'];
$yearsexp=$_POST['yearsexp'];
$levelexp=$_POST['levelexp'];
$crm=0;
$mac=$_POST['mac'];
This part determines if we are adding or updating to the data. Since it's an add, the query lists all fields held in the table with the exception of the UserID which is an autoincrement. The fields below are not in the order they appear in the actual table and some fields are not set in the variables list above because they aren't in the form that sends them here.
Code:
else if ($query_type=="add")
{
$sql_query_registrations = "INSERT into users
(fname, lname, email, mac, phone,levelexp,yearsexp,department,tt,news,calendar,whitepapers,dm,crmstaff,dateadded,dateupdated,su
bscriptions)
values
('$fname','$lname','$email','$mac','$phone','$levelexp','$yearsexp','$department','$tt','$news','$ca
lendar','$whitepapers','$dm', '0', getdate(),getdate(),'0')" or die ("Screwed Up: " . mysql_error())
;
$result_registrations = mysql_query($sql_query_registrations);
if (!$result){
echo mysql_affected_rows()." record added";}
else {echo "no record added";}
}
Not sure what I've done wrong here so any help would be greatly appreciated! Thanks!