Jump to content

select and update database records

- - - - -

  • Please log in to reply
6 replies to this topic

#1
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
Good day everyone,please I need help.
//In this program, Mutago with student_regno of 20094145 has total_score of 10 already existing in the database. So i want to get that
total_score of 10 from database and add it to incoming student_score.
the problem is that the database student_totalscore is not updated and it seems that student_totalscore eg 10
is not selected from database and hence not added to the incoming student_score. Any help please. Is there anyway to solve this problem using object oriented programming. All am trying to do is to keep on updating the database each time studentscore were entered

thanks


<?php

mysql_connect("localhost", "mutago", "mut67RT") or die(mysql_error()); 

 mysql_select_db("student_database") or die(mysql_error()); 


$student_totalscore='';

$student_regno=$_POST['student_regno'];

$student_score=$_POST['student_score'];


// get student_totalscore from database and add it to incoming student_score

function student_record () 

 { 

 $data = mysql_query("SELECT student_totalscore FROM student_result where student_regno='student_regno'") 

 or die(mysql_error()); 

 $result = mysql_fetch_array( $data ); 


// get Mutago totalscore of 10 from database and add it to incoming score(student_score) 

 $total = $result[student_totalscore] + $_POST['student_score']; 


// now update the database


$up = "UPDATE student_result SET student_totalscore='total' WHERE student_regno='student_regno'"; 

$result=mysql_query($up);


if($result){

echo "congratulations ";


}


else {

echo "you cannot update";

}


mysql_close();

?>




#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You are doing a lot of things incorrect in your code, and I will try to explain each one.

$student_totalscore='';
You likely should initialize this as a number and not an empty string, as you seem to be adding score to it later on. PHP will allow this, it can make for messy code however.
where student_regno='student_regno'
This will search for a student with a record number 'student_regno' and not the one you had gotten through $_POST. It should be a variable, yes?

$result = mysql_fetch_array( $data ); 
mysql_fetch_array will by default return a numeric index such as $result[0], not $result['some_row_name']. You should use mysql_fetch_assoc or mysql_fetch_array($result, MYSQL_ASSOC);

$total = $result[student_totalscore] + $_POST['student_score'];
You seem to be using $total, instead of $student_totalscore that you had initialized earlier. Had you meant to correct this?

$result[student_totalscore] 
Remember, if you define a string index, you should use a string such as 'student_totalscore'. PHP may allow the omission, although this is slow an insecure (PHP will need to check if it is a constant before assuming it is a string and will issue a warning)

I would highly much so recommend you walk through the PHP manual on basic variable and string use before trying to write any further code, otherwise writing code could be very error prone. Try going through this: PHP: PHP Manual - Manual

For object oriented code, mysqli (an improved mysql extension that is often used and is newer) or PDO can be used to access the database in an object oriented manner. I would really recommend you try to learn those, it could make working with the database a little more fun and safer (if you utilize prepared statements)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
thanks Mr.Alexander. Now to get me started with your corrections, how do I initialize student_totalscore to number. Is it like this

$student_totalscore=0;



#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Yes, that is what you would do. I would recommend following the PHP guide to learn more.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
Hello Mr. Alexander am still on the move. how do I convert this update code to mysqli update using prepared statement


$add = $student_totalscore + $_POST['student_score'];

$sql = "UPDATE student SET student_totalscore='$add' WHERE id='$id'"; 

$result=mysql_query($sql);





#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
We've got a nice tutorial here: http://forum.codecal...statements.html

The official documentation on mysqli can be found here: PHP: Mysqli - Manual

Note that the documentation on PHP.net has sufficient examples and synopsis to get you started, I would look through each function and see if you understand it so that you can use it comfortably in your code.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
thanks. Am on it.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users