php – 我的一些查询没有执行

我是PHP的编程全新,在w3schools上学习这门课程,我正在尝试执行以下操作,数据库被创建但是表格没有,插入也没有.

<form action="sql.PHP" method="post">
  <br> Name: <br>
  <input type="text" name="pName">
  <br> Age: <br>
  <input type="text" name="pAge">
  <br> Position <br>
  <input type="text" name="position">
  <br> Goals: <br>
  <input type="text" name="goals">
  <br><br>
  <input type="submit" name="submit" value="Submit">
</form>

<?PHP
  if(isset($_POST['submit'])) {
    $name = $_POST['pName'];
    $age = $_POST['pAge'];
    $position = $_POST['position'];
    $goals = $_POST['goals'];
    echo $name . $age . $position . $goals;
    $con = new MysqLi('127.0.0.1', 'root', '');
    $sql = 'CREATE DATABASE Arsenal;';
    $sql1 = 'CREATE TABLE Players (Name varchar(225) NOT NULL, Age int(11) NOT NULL, Position varchar(225) NOT NULL, Goals int(11) NOT NULL);';
    $sql2 = "INSERT INTO players ( Name, Age, Position, Goals) VALUES ( \$name, \$age, \$position, \$goals )";

    MysqLi_select_db($con, 'Arsenal');

    if(MysqLi_query($con, $sql)) {
      echo 'success';
    }

    if(MysqLi_query($con, $sql1)) {
      echo 'success';
    }

    if(MysqLi_query($con, $sql2)) {
      echo 'success';
    }
  }

解决方法:

错误放置了MysqLi_select_db(),并且插入查询看起来也不正确

将您的代码更改为此

if(isset($_POST['submit']))
 {
      $name = $_POST['pName'];
      $age = $_POST['pAge'];
      $position = $_POST['position'];
      $goals = $_POST['goals'];

      echo $name . $age . $position . $goals;

      $con = new MysqLi('127.0.0.1', 'root', '');
      $sql = 'CREATE DATABASE Arsenal;';
      $sql1 = 'CREATE TABLE Players (Name varchar(225) NOT NULL, Age int(11) NOT NULL, Position varchar(225) NOT NULL, Goals int(11) NOT NULL);';
      $sql2 = "INSERT INTO players ( Name, Age, Position, Goals) VALUES ( '$name', '$age', '$position', '$goals' )";

      if(MysqLi_query($con, $sql))
       {
           echo 'success';
       }

       MysqLi_select_db($con, 'Arsenal');

       if(MysqLi_query($con, $sql1))
       {
           echo 'success';
       }

        if(MysqLi_query($con, $sql2))
       {
           echo 'success';
       }
 }

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...