search

Home  >  Q&A  >  body text

PHP failed to connect to database

<?php

header("content-type:text/html;charset=gb2312");

$conn=mysqli_connect("locallost","root","root","study");

mysqli_set_charset($conn,"utf-8");

if($conn){

    switch($_GET["action"]){

       case 'add';

           $name=$_POST['name'];

           $sex=$_POST['sex'];

           $age=$_POST['age'];

           $class=$_POST['class'];

           $sql="insert into stu(name,sex,age,class)values('$name','$sex','$age','$class')";

           $rw=mysqli_query($conn,$sql);

             if($rw>0){

                 echo "<script>alert('add success添加成功');</script>";

             }else{

                 echo "<script>alert('add failure添加失败');</script>";

             }

             header(‘Location:index.php’);

             break;

       }

}

?>


phpcn_u39497phpcn_u394972594 days ago1155

reply all(2)I'll reply

  • 虫儿飞

    虫儿飞2017-10-09 11:31:11

    What error is reported~

    reply
    0
  • 散步咏凉天

    散步咏凉天2017-10-07 15:13:16

    Pay more attention to whether the conditions during link configuration are written incorrectly. For example, password, user name, etc., and check the code for errors

    reply
    0
  • phpcn_u39497

    The code is correct. I wrote it in Notepad. Is there any problem related to this?

    phpcn_u39497 · 2017-10-07 16:07:13
  • Cancelreply