How to insert data in db using jsp?

How advanced does you php for database coding need to be?

  • So i know basic php, i can connect, create db's, tables, insert, select, drop record, tables, db...... do it all in a few lines of php really. So i can search a db using what i assume is a basic "select from" in php and display the results in a table on the page or i verify a login etc,,, BUT i look at some php code say from the likes of php code generators and looks a good bit more advanced just to do the same thing i think.....i mean what else can it be doing that needs all that code. What am i missing here, will my basic php manage what else do i need to know? I am taking about really verifiyng logins, retrieving data for to dispaly on the web page, inserting new data, images, all data really, i just can't understand why i see all his advanaced php code for which i assume must be doing the same thing. My php skills don't allow me to eaily see what this adavnced code is doing so...hence i am baffled. ALTHOUGH this advanced code is not singly for working with db's it's part of an overall system of database + displaying the results so maybe most of the code is about displaying, i don't know. Take this code for exmaple, this is what i would us, <?php $con = mysql_connect("localhost","peter","abc12… if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo " "; } mysql_close($con); ?>

  • Answer:

    Well, it depends on the type of relational database that you wish to connect to with your PHP code. And what you wish to do with that database connection after it is made. Even understanding everything about SQL can get a bit tricky at times. And one needs to know that to fully access a well designed relational database. If you keep things simple, then you will never need to worry about learning the advanced coding techniques.

Nahee_En... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

It is because the generators that create the script have no clue what your plan for them is going to be, so it can't have the simpler more broad (broad is the wrong word) way of putting it... basically the program just provides the more precise way to do it. And you wonder why http://w3schools.com is more precise like that script? It is because a HUMAN wrote that script, not a program. -- Best Answers are Appreciated --

Chris B

You are selecting everything when you only need to select one record. If your site was as popular as ebay with a few million users, then your code selects every user, then it uses a loop to act on the results. You can't avoid some loops in code, but any long loop is going to slow down the response at the user end, remember that php is pre-processing, so their page hasn't even started to load while your code is being processed. Instead narrow down as far as possible using SQL. The database engine is optimised for this sort of search, it can get to the required record or subset of records much more quickly then your php can loop through it. So to add what you have learnt, start working with the 'where' statement, also be careful of injection attacts, no programmer allows user input directly into the database without first ensuring they are not injecting SQL. A good way to do this is to make use of stored procedures, if you ever program for a bank you will find that all SQL is ran from stored procedures. There are a few reasons, first it is easier to make it safe, but second because it is much easier to maintain.

Bob M

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.