The result of PHP code is wrong? -


when tried make simple php code, had problem result had showed things weren't expected such "' . "\n"; echo' ". code wrong?

here code:

   <html> <head> <title>putting data in db</title> </head> <body> <?php /*insert students db*/ if(isset($_post["submit"])) { $db = mysql_connect("mysql", "martin"); mysql_select_db("martin"); $date=date("y-m-d");  /* current date in right sql format */ $sql="insert students  values(null,'" . $_post["f_name"] . "','" .  $_post["l_name"] . "'," . $_post["student_id"] . ",'" . $_post["email"] .  "','" . $date . "'," . $_post["gr"] . ")";  /* construct query */ mysql_query($sql);  /* execute query */  mysql_close(); echo"<h3>thank you. data has been entered.</h3> \n"; echo'<p><a href="data_in.php">back registration</a></p>' . "\n"; echo'<p><a href="data_out.php">view student lists</a></p>' ."\n"; } else { ?>  <h3>enter items database</h3> <form action="data_in.php" method="post"> first name: <input type="text" name="f_name" /> <br/> last name: <input type="text" name="l_name" /> <br/> id: <input type="text" name="student_id" /> <br/> email: <input type="text" name="email" /> <br/> group: <select name="gr"> <option value ="1">1</option> <option value ="2">2</option> <option value ="3">3</option> </select><br/><br/> <input type="submit" name="submit" /> <input type="reset" /> </form> <?php } /* end of "else" block */ ?> </body> </html> 

result here:

thank you. data has been entered. \n"; echo' registration

' . "\n"; echo' view student lists

' ."\n"; } else { ?> enter items database

correct code:

<html> <head>     <title>putting data in db</title> </head> <body> <?php if (isset($_post["submit"])): ?>     <?php         /*insert students db*/         $db = mysql_connect("mysql", "martin");         mysql_select_db("martin");         $date = date("y-m-d");  /* current date in right sql format */         $sql = "insert students  values(null,'" . $_post["f_name"] . "','" .             $_post["l_name"] . "'," . $_post["student_id"] . ",'" . $_post["email"] .             "','" . $date . "'," . $_post["gr"] . ")";  /* construct query */         mysql_query($sql);  /* execute query */         mysql_close();     ?>     <h3>thank you. data has been entered.</h3>     <p><a href="data_in.php">back registration</a></p>     <p><a href="data_out.php">view student lists</a></p> <?php else: ?>     <h3>enter items database</h3>     <form action="data_in.php" method="post">         first name: <input type="text" name="f_name"/> <br/>         last name: <input type="text" name="l_name"/> <br/>         id: <input type="text" name="student_id"/> <br/>         email: <input type="text" name="email"/> <br/>         group: <select name="gr">             <option value="1">1</option>             <option value="2">2</option>             <option value="3">3</option>         </select><br/><br/>         <input type="submit" name="submit"/> <input type="reset"/>     </form> <?php endif ?> 

by way, please try use pdo
http://www.w3schools.com/php/php_mysql_prepared_statements.asp
otherwise can enter "); truncate table students; -- , clear data.

it's classic - https://xkcd.com/327/


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -