Insert New Data to an other table

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
webrider
User
Posts: 6

Insert New Data to an other table

Post by webrider »

I have 2 tables
Students & StudentsLog
When New Record is added to Students Table I like to insert the value of Student_Status, Student_Name to Students_Log
Here’s what I did
// Row Inserted event
function Row_Inserted($rsold, &$rsnew) {
//echo "Row Inserted"
$Student_Status = $rsnew["Student_Status"];
$Student_Name = $rsnew["Student_Name"];
$MyResult = ew_Execute("INSERT INTO students
log (Student_Status, Student_Name) VALUES ( $Student_Status, $Student_Name)");
}
I’m getting error
Case 1: If I type the Student name: FrankLee no space between First & Last Name
Failed to execute SQL. Error: You have an error in your SQL syntax; check the manual that corresponds Failed to execute SQL. Error: Unknown column 'FrankLee' in 'field list'
Case 2: If I type the Student name: Frank Lee with space between First & Last Name
Failed to execute SQL. Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Lee)' at line 1

FrankLee or Lee is the student name not a field
Can someone Help me please
Thank you for your help & time


uwiii
User
Posts: 12

Post by uwiii »

Try this: INSERT INTO students__log (Student_Status, Student_Name) VALUES ('$Student_Status', '$Student_Name')


kirondedshem
User
Posts: 642

Post by kirondedshem »

In sql, when dealing with strings, you must surround them with qoutes. Its only if the fields are of type number formats like integer or float type otherwise all others need to have quotes sorruonding there values.
Try what uwiii says.

NOTE: in some cases users might put quotes into thier input or some other symbols and characters that need to be escaped.
eg if a user enters input as "John's brother's", this will cause errors even if you surround it with qoutes.
So beofre you paste them into a query you can use phpmaker function to escape them. eg
//ewAdjustSql is similar to using mysql_escape php functions.
$Student_Status = ewAdjustSql($rsnew["Student_Status"]);
$Student_Name = ewAdjustSql($rsnew["Student_Name"]);


webrider
User
Posts: 6

Post by webrider »

Thank you for your reply
I did try
INSERT INTO studentslog (Student_Status, Student_Name) VALUES ('$Student_Status', '$Student_Name')
No Error however Student_Status is captured and inserted to Student
Log but Not Student Name Blank nothing
I even removed Student_ID and Left just Student_Name new record is added but student_name is Blank

When I use:
$Student_Status = ewAdjustSql($rsnew["Student_Status"]);
$Student_Name = ewAdjustSql($rsnew["Student_Name"]);
I get an error: Fatal error: Call to undefined function ewAdjustSql() in www\cta3\studentsinfo.php on line 1802
I’m lost I can’t see where the issue is
Thank you for your Time & Help


mobhar
User
Posts: 11703

Post by mobhar »

Change "ewAdjustSql" to "ew_AdjustSql".


webrider
User
Posts: 6

Post by webrider »

Thank you for your reply
no Error but Student_Name blank
// Row Inserted event
function Row_Inserted($rsold, &$rsnew) {
//echo "Row Inserted"
// $Student_Status = ew_AdjustSql($rsnew["Student_Status"]);
$Student_Name = ew_AdjustSql($rsnew["Student_Name"]);
$MyResult = ew_Execute("INSERT INTO studentslog (Student_Name) VALUES ('$Student_Name')");
}
I just disabled Student_Status and try to get to insert Student_Name but it doesn’t work
New Record is Added in to Students
Log with Student_Name Blank


mobhar
User
Posts: 11703

Post by mobhar »

Change this code:
$Student_Name = ew_AdjustSql($rsnew["Student_Name"]);
$MyResult = ew_Execute("INSERT INTO students__log (Student_Name) VALUES ('$Student_Name')");

to:
$Student_Name = ew_QuotedValue($rsnew["Student_Name"], EW_DATATYPE_STRING, "DB");
$MyResult = ew_Execute("INSERT INTO students__log (Student_Name) VALUES ($Student_Name)");


webrider
User
Posts: 6

Post by webrider »

No error but Student-Name is Blank

I used the following
$Student_Name = ew_QuotedValue($rsnew["Student_Name"], EW_DATATYPE_STRING, "DB");
$MyResult = ew_Execute("INSERT INTO students__log (Student_Name) VALUES ($Student_Name)");

and

$Student_Name = ew_QuotedValue($rsnew["Student_Name"], EW_DATATYPE_STRING, "Students");
$MyResult = ew_Execute("INSERT INTO students__log (Student_Name) VALUES ($Student_Name)");
Still the same Student_Name empty, blank


mobhar
User
Posts: 11703

Post by mobhar »

  1. Did you have any PHP code in the Row_Inserting server event which perhaps will cause the Student_Name field will become blank string?
  2. Did you have any Javascript code in Startup Script which perhaps will cause the Student_Name field will become blank string?
  3. Are you sure you have already filled-in the data in Student_Name field before saving the data?

Post Reply