Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems inserting data

Status
Not open for further replies.

MarlaJ

Programmer
Jul 21, 2003
20
US
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:

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!
 
I know its a dumb question but does the user you are using have the authority to add records to the database?
 
Not a dumb question at all but yes, the user definately has the permission to add. Actually the user has permission to do anything it wants to do.
 
Add [tt]or die(mysql_error())[/tt] after you query the database.

//Daniel
 
I have this in there at the end of the query:
or die ("Screwed Up: " . mysql_error());

Did I put it in the wrong place?
 
[tt]$result_registrations = mysql_query($sql_query_registrations) or die(mysql_error());[/tt]
is what it should read.

//Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top