Here is the error I am getting:

Unknown column 'Lo' in 'field list'

Here is my code. Any suggestions to get rid of this error:

global $conn,$dal;
$customerID = mysql_insert_id();

$strSQLInsert = "insert into Orders (CustomerID, LastName) values (".$customerID.", ".$values["LastName"].")";
db_exec($strSQLInsert,$conn);

$rs = $dal->Orders->Query("CustomerID=".$customerID." and LastName=".$values["LastName"],"");
$data = db_fetch_array($rs);

$idvalue = $data["ID"];
header("Location: Orders_edit.php?editid1=".$idvalue);
exit();

Dani AI

Generated

Two separate failures are happening here and both need small but different fixes.

The MySQL error ("Unknown column 'Lo'") is a SQL parsing problem: the lastname value was sent to the server as an unquoted token, so MySQL treated the letters (for example, Lo) as a column name. As hinted, the real solution is to stop building SQL by concatenating raw input and instead use parameterized queries so names with apostrophes or spaces never break the statement and you avoid SQL injection.

Also, the "Call to a member function Query() on a non-object" means the DAL object you expect is not initialized or does not expose an Orders object. Before calling Query() inspect the DAL variable (for example, with var_dump or similar) and confirm the expected properties/methods exist and that you included/instantiated the DAL class correctly. Mixing raw mysql_* calls and a separate DAL instance can easily lead to confusion about which connection or object is in use; pick one DB API and use it consistently.

Practical pattern (recommended): use PDO with a transaction when you need to insert a customer and then create an order linked to that customer. Example:

$pdo->beginTransaction();

$insertCust = $pdo->prepare("INSERT INTO customers (FirstName, LastName) VALUES (:fn, :ln)");
$insertCust->execute([':fn'=>$firstName, ':ln'=>$lastName]);
$customerId = $pdo->lastInsertId();

$insertOrder = $pdo->prepare("INSERT INTO orders (CustomerID, LastName) VALUES (:cid, :ln)");
$insertOrder->execute([':cid'=>$customerId, ':ln'=>$lastName]);
$orderId = $pdo->lastInsertId();

$pdo->commit();

If you must keep the existing DAL, locate its docs/API and call the DAL methods in the documented way rather than mixing raw queries. Quick checklist: (1) switch to prepared statements, (2) ensure the DAL is instantiated before use, (3) use the same DB connection for related operations, and (4) test the final SQL in the DB client if you still see syntax errors. For PDO reference, see the manual pages for prepared statements and lastInsertId: PDO prepared statements and PDO::lastInsertId.

Recommended Answers

All 7 Replies

put single qoute for LastName as it is varchar.

global $conn,$dal;
$customerID = mysql_insert_id();

$strSQLInsert = "insert into Orders (CustomerID, LastName) values (".$customerID.", '".$values["LastName"]."')";
db_exec($strSQLInsert,$conn);

$rs = $dal->Orders->Query("CustomerID=".$customerID." and LastName='".$values["LastName"],"'");
$data = db_fetch_array($rs);

$idvalue = $data["ID"];
header("Location: Orders_edit.php?editid1=".$idvalue);
exit();

Thank you for the help. I tried the code and received this error:


Fatal error: Call to a member function Query() on a non-object

Still need an assist.

after inserting order what you exactly want?
i means what is the need for below code:

$rs = $dal->Orders->Query("CustomerID=".$customerID." and LastName='".$values["LastName"],"'");

if you want just last inserted id then it can be directly taken by mysql_insert_id

BTW above line use Orders as a class.
check your connection class and how to use that class functions for fetch table records.

I want to be able to insert the two fields into the customer table, then have the customer have the next page which is orders, with the two fields in the orders page and table.

Here is what I am trying to do. I have two tables, customers and orders. When the fields CustomerID and LastName are entered into the customers table, I want the same variables put into the new form which will then be inserted into the orders table when the rest of the form is filled.

The issue seems to be escaping variables. you should use ' and " in sql stmt to clearly distinguish variables into sql query. There can be some chances the code is getting failed before reaching to this point. Good luck. Keep us updated, what works for you.

Member Avatar for Member #334542

Form as below clearly in your code

$query = "INSERT INTO symbols (country, animal) VALUES ('$country', '$animal')";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.