Any idea why this code doesn't work when the same code (except for the query VALUES field names) works in another file. $link = mysql_connect('localhost', $username, $password); if (!$link){die('Not connected : ' . mysql_error());} $db_selected = mysql_select_db($database, $link); if (!$db_selected){die ('Can\'t use ' .$database .':'. mysql_error());} $query = "INSERT INTO invoices VALUES('$Ref','$CoEmpName',". "'$InvDate','$Branch','$BrAddr','$BrApt','$BrCity','$BrProv','$BrCtry',". "'$BrZip','$AssDesc[0]','$AssPrice[0]')"; mysql_query($query, $link); mysql_close(); I dont' get any errors and nothing gets added to the database. Thanks, Michael
<?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Insert data into mysql $sql="INSERT INTO $tbl_name (Ref, CoEmpName, InvDate, Branch, BrAddr, BrApt, BrCity, BrProv, BrCtry, BrZip, AssDesc, AssPrice) VALUES ('$Ref','$CoEmpName',". "'$InvDate','$Branch','$BrAddr','$BrApt','$BrCity','$BrProv','$BrCtry',". "'$BrZip','$AssDesc[0]','$AssPrice[0]')"; $result=mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result) { echo "Successful"; } else { echo "ERROR"; } // close connection mysql_close(); ?> Code (markup): untested, but i use to use that sort of way. should work, if not make sure your table fields are the same and remember to $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name Code (markup): fill out that!
Change: $query = "INSERT INTO invoices VALUES('$Ref','$CoEmpName',"."'$InvDate','$Branch','$BrAddr','$BrApt','$BrCity','$BrProv','$BrCtry',"."'$BrZip','$AssDesc[0]','$AssPrice[0]')"; mysql_query($query, $link); Code (markup): To: $query = "INSERT INTO invoices VALUES('$Ref','$CoEmpName','$InvDate','$Branch','$BrAddr','$BrApt','$BrCity','$BrProv','$BrCtry','$BrZip','$AssDesc[0]','$AssPrice[0]')"; mysql_query($query, $link) or die(mysql_error()); Code (markup): Peace,
The code: or die(mysql_error()); gave me the answer. It gave me an error message saying that the number of fields I was trying to insert did not match the number of fields in my database table. Many thanks, Michael