This topic is locked

update field in another table

10/15/2013 12:23:27 PM
PHPRunner General questions
W
wilmer author

Hi
I intend to make a system for use in a career fair. Where the students will be registred in a database fo every company they visit.
I have made 3 tables:
Students with the fields: StudentID, StudentName, Totalvisits
Companies with the fields: CompanyID, CompanyName
Rgister with the fields: RegStudentID, RegCompanyID
When a student visit a company, the company will fill in the StudentID into the Register_Add.php.
Then I thought this event (in before record added) should have increased the field Totalvisits in the Students table:
global $conn;
$strSQLSave = "UPDATE Students SET Totalvisits = Totalvisits+1 WHERE StudentID='".$values["RegStudentID"]."'";

db_exec($strSQLSave,$conn);
return true;
But nothing happens. Why?

Sergey Kornilov admin 10/15/2013

I'm not seeing any obvious errors in your code. Here are some troubleshooting tips.
Modify your code the following way:

$strSQLSave = "UPDATE Students SET Totalvisits = Totalvisits+1 WHERE StudentID='".$values["RegStudentID"]."'";

echo $strSQLSave;

exit();

//db_exec($strSQLSave,$conn);
//return true;


This will print your SQL query on the page. Then run this SQL query manually against your database using Navicat or phpMyAdmin to see if it works. Post results here.

W
wilmer author 10/16/2013



I'm not seeing any obvious errors in your code. Here are some troubleshooting tips.
Modify your code the following way:

$strSQLSave = "UPDATE Students SET Totalvisits = Totalvisits+1 WHERE StudentID='".$values["RegStudentID"]."'";

echo $strSQLSave;

exit();

//db_exec($strSQLSave,$conn);
//return true;


This will print your SQL query on the page. Then run this SQL query manually against your database using Navicat or phpMyAdmin to see if it works. Post results here.

W
wilmer author 10/16/2013





Thanks for your answer.
I have done this in phpMyAAdmin, and got this result:
SQL-spørring:
$strSQLSave = "UPDATE Students SET Totalvisits = Totalvisits+1 WHERE StudentID='".$values[ "RegStudentID"]. "'";
MySQL sa: Dokumentasjon

1064 - 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 '$strSQLSave = "UPDATE Students SET Totalvisits = Totalvisits+1 WHERE StudentID='' at line 1

Sergey Kornilov admin 10/16/2013

Is RegStudentID field in Register table an autoincrement field? If yes, it doesn't exist in BeforeAdd event. Move your code to AfterAdd event.