Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

select and update database records

records

  • Please log in to reply
6 replies to this topic

#1 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 22 May 2011 - 12:47 PM

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();
?>


  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 22 May 2011 - 09:06 PM

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)
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 23 May 2011 - 01:11 PM

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;

  • 0

#4 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 23 May 2011 - 03:12 PM

Yes, that is what you would do. I would recommend following the PHP guide to learn more.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#5 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 25 May 2011 - 02:02 PM

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);



  • 0

#6 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 25 May 2011 - 02:10 PM

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.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#7 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 26 May 2011 - 12:27 PM

thanks. Am on it.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download