php can not execute after selected mysql db -


this question has answer here:

here code, can see, wanna connect mysql insert info field, seems not execute next code after selected db code, newer in php, , did not return error, not know wrong..

<html> <body>  welcome <?php echo "show" ?><br> email address is:    <?php $servername = "localhost"; $username = "root"; $password = "123456";  // 创建连接 $conn = mysqli_connect($servername, $username, $password);  // 检测连接 if (!$conn) {     die("connection failed: " . mysqli_connect_error()); } echo "mysql successed connected.";   mysql_select_db("flowers", $conn) or die("database flowers failed".mysql_error()) ;  echo "database successed";  $sql="insert flowers (username, password) values ('$_post[name]','$_post[email]')";  if (!mysql_query($sql,$conn))   {   die('error: ' . mysql_error());   }  echo "1 record added";   mysql_close($conn);  ?> </body> </html> 

test demo info , here db info: dn info

i use ubuntu 16.04 apache2

so main issue here using combination of mysql_ , mysql functions. note mysql has been depreciated since php5 , has been removed in php7 should using newer mysqli or pdo. use pdo, have kept code mysqli.

<?php $servername = "localhost"; $username = "root"; $password = "56lj0721";  // 创建连接 $conn = mysqli_connect($servername, $username, $password);  // 检测连接 if (!$conn) {     die("connection failed: " . mysqli_connect_error()); } echo "mysql successed connected.";   mysqli_select_db($conn,"flowers") or die("database flowers failed".mysqli_error()) ;  echo "database successed";  $sql="insert flowers (username, password) values ('$_post[name]','$_post[email]')";  if (!mysqli_query($conn, $sql))   {   die('error: ' . mysql_error());   }  echo "1 record added";   mysqli_close($conn);  ?> 

you should using prepared statements prevent sql injection. can read more here mysqli.

it's important remember should validating $_post['name'] , $_post['email'] values well, have not included.

if want read further pdo, take here.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -