database - retrieve data from db and display it in table in php .. see this code whats wrong with it? -
$db = mysql_connect("localhost", "root", ""); $er = mysql_select_db("ram"); $query = "insert names values('$name','$add1','$add2','$mail')"; $result = mysql_query($query); print "<p> person's information inserted </p>"; $result = mysql_query("select * names"); ?> <table border="2"> <tr> <th>name</th> <th>address line 1</th> <th>address line 2 </th> <th>e-mail id </th> </tr> <? while ($array = mysql_fetch_row($result)); { print "<tr> <td>"; echo $array[0]; print "</td> <td>"; echo $array[1]; print "</td> <td>"; echo $array[2]; print "</td> <td>"; echo $array[3]; print "</td> </tr>"; } ?>
try this:
<?php # init mysql connection if( !( $db = mysql_connect( 'localhost' , 'root' , '' ) ) ) die( 'failed connect mysql database server - #'.mysql_errno().': '.mysql_error(); if( !mysql_select_db( 'ram' ) ) die( 'connected server, failed connect database - #'.mysql_errno().': '.mysql_error(); # prepare insert query $inserttpl = 'insert `name` values( "%s" , "%s" , "%s" , "%s" )'; $insertsql = sprintf( $inserttpl , mysql_real_escape_string( $name ) , mysql_real_escape_string( $add1 ) , mysql_real_escape_string( $add2 ) , mysql_real_escape_string( $mail ) ); # execute insert query if( !( $insertres = mysql_query( $insertsql ) ) ){ echo '<p>insert of row database failed - #'.mysql_errno().': '.mysql_error().'</p>'; }else{ echo '<p>person\'s information inserted</p>' } # prepare select query $selectsql = 'select * `names`'; # execute select query if( !( $selectres = mysql_query( $selectsql ) ) ){ echo 'retrieval of data database failed - #'.mysql_errno().': '.mysql_error(); }else{ ?> <table border="2"> <thead> <tr> <th>name</th> <th>address line 1</th> <th>address line 2</th> <th>email id</th> </tr> </thead> <tbody> <?php if( mysql_num_rows( $selectres )==0 ){ echo '<tr><td colspan="4">no rows returned</td></tr>'; }else{ while( $row = mysql_fetch_assoc( $selectres ) ){ echo "<tr><td>{$row['name']}</td><td>{$row['addr1']}</td><td>{$row['addr2']}</td><td>{$row['mail']}</td></tr>\n"; } } ?> </tbody> </table> <?php } ?>
notes, cautions , caveats
your initial solution did not show obvious santisation of values before passing them database. how sql injection attacks (or un-intentional errors being passed through sql) occur. don't it!
your database not seem have primary key. whilst these not, technically, necessary in usage, practice, , make more reliable way of referring specific row in table, whether adding related tables, or making changes within table.
you need check every action, @ every stage, errors. php functions nice enough have response return under error condition. job check conditions go - never assume php expect, how expect, , in order expect. how accident happen...
my provided code above contains alot of points where, if error has occured, message returned. try it, see if error messages reported, @ error message, and, if applicable, error code returned , research.
good luck.
Comments
Post a Comment