Join 136,577 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,891 people online right now. Registration is fast and FREE... Join Now!
I have created a simple update form for teachers at my university to mark essays. Teachers enter values and comments (not shown in the code below), the total is then added and stored in the array $tot. The student's name etc are uploaded from an external data source.
I am trying to insert the array ($tot) into my MYSQL database. I think that I need to use implode and/or serialize with INSERT but I have not been able to work out what to do. The ($tot) array contains values computed by adding a number of other values together and is to go into the field 'tot' in my MYSQL database.
The calculation was easy enough, the tricky bit is getting the total ($tot) into the MYSQL database (outputting it to the screen is not hard) - can any one help please? The update form was created with Dreamweaver MX.
I'm not quite sure what you are trying to do, you store $tot as a single value, not an array, why would you think you need to use implode?
what columns are in the table you are inserting to? does the row already exist? if so you could use the syntax for the update SQL call if the row doesn't exist, it's a simple insert.
also you do not need to connect to the database more than once, unless they are different databases. If they are different although not required good structure would suggest you should close the connection to the previous database before opening another.
I'm not quite sure what you are trying to do, you store $tot as a single value, not an array, why would you think you need to use implode?
what columns are in the table you are inserting to? does the row already exist? if so you could use the syntax for the update SQL call if the row doesn't exist, it's a simple insert.
also you do not need to connect to the database more than once, unless they are different databases. If they are different although not required good structure would suggest you should close the connection to the previous database before opening another.
OK...let me try to be clearer. Yes, there is a column for the total (tot). I tried inserting the value into the database using:
CODE
GetSQLValueString($_POST['tot'], "int"),
This was able to insert the value into the correct column of the database but it didn't insert the most up-to-date value. I am not sure quite how to answer your questions, perhaps the attached screen shots might help. Please bear with me if I seem a bit thick. Thanks.
GetSQLValueString($_POST['tot'], "int") doesn't do any database updating, it just takes the content of the $_POST variable, sanitizes it and makes it syntactically acceptable for database insertion.
You still have to do the update in a SQL statement like
php
$query = 'UPDATE results SET total = ' . GetSQLValueString($_POST['tot'], "int") . ' WHERE id = ' . GetSQLValueString($_POST['id'], "int"); if (!mysql_query($query, $my_conn)) { echo ("Unable to update results table with query $query: " . mysql_error()); }
GetSQLValueString($_POST['tot'], "int") doesn't do any database updating, it just takes the content of the $_POST variable, sanitizes it and makes it syntactically acceptable for database insertion.
You still have to do the update in a SQL statement like
php
$query = 'UPDATE results SET total = ' . GetSQLValueString($_POST['tot'], "int") . ' WHERE id = ' . GetSQLValueString($_POST['id'], "int"); if (!mysql_query($query, $my_conn)) { echo ("Unable to update results table with query $query: " . mysql_error()); }
Thanks, but does the code below not do the same as your code above?
It is actually updating the field 'tot' but not correctly. If I type: 5, 5, 5, 5, 5 into the update form then hit submit: the onscreen total shows 25. No problem!! If I check the MYSQL database all the individual values have updated but not the total. So I type 5, 5, 5, 5, 6 and hit submit. Again onscreen shows the correct value and all of the individual values update in the DB, including the total but it shows as 25 NOT 26. If I hit submit one more time it updates tot to 26.
GetSQLValueString($_POST['tot'], "int") doesn't do any database updating, it just takes the content of the $_POST variable, sanitizes it and makes it syntactically acceptable for database insertion.
You still have to do the update in a SQL statement like
php
$query = 'UPDATE results SET total = ' . GetSQLValueString($_POST['tot'], "int") . ' WHERE id = ' . GetSQLValueString($_POST['id'], "int"); if (!mysql_query($query, $my_conn)) { echo ("Unable to update results table with query $query: " . mysql_error()); }
Thanks, but does the code below not do the same as your code above?
It is actually updating the field 'tot' but not correctly. If I type: 5, 5, 5, 5, 5 into the update form then hit submit: the onscreen total shows 25. No problem!! If I check the MYSQL database all the individual values have updated but not the total. So I type 5, 5, 5, 5, 6 and hit submit. Again onscreen shows the correct value and all of the individual values update in the DB, including the total but it shows as 25 NOT 26. If I hit submit one more time it updates tot to 26.
After checking it seems as though what I want to do won't work...so I turned to Javascript and it works!! I am aware of the obvious drawbacks of using Javascript, but for what I need it does the job. Script below: