Php Mysql查询插入行而不是插入行

Php Mysql查询插入行而不是插入行,php,mysql,Php,Mysql,我在这里翻了翻,搜索了无数的教程,但似乎一切都井然有序- 我正在创建的页面做了它应该做的一切,但它似乎跳过了mysql代码 首先,我有一个表单页面create.php: <head> <link rel="stylesheet" type="text/css" href="/css/index.css" /> <?php include ($_SERVER['DOCUMENT_ROOT'].'/menu/nav.php'); ?> </head>

我在这里翻了翻,搜索了无数的教程,但似乎一切都井然有序- 我正在创建的页面做了它应该做的一切,但它似乎跳过了mysql代码

首先,我有一个表单页面create.php:

<head>
<link rel="stylesheet" type="text/css" href="/css/index.css" />
<?php include ($_SERVER['DOCUMENT_ROOT'].'/menu/nav.php');
?>
</head>
<table class="cnrc">
<td>
<?php
$username= $_COOKIE['username'];
$con=mysqli_connect('localhost','root','password','perms')or die("cannot connect");
$sql = "SELECT * FROM permissions WHERE `name`='".$username."'";
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
$new_array = $row;
if ($res === false) {
    echo mysqli_error();
}else{
if(mysqli_num_rows($res) == 0){
echo 'It seems you do not have permission to create a town!';
}
elseif (in_array('town.mayor',$new_array,true)){
echo ' <form method="post" action="filecheck.php">
Please name your new town!: <input type="text" name="tname"><br>
<center><input type="submit" name="ctown" value="Create"></center></form> ';
}
}
?>
</td>
</table>

然后,创建页面-filecheck.php的过程:

<?php
$tname = mysql_real_escape_string($_POST['tname']);
$user= mysql_real_escape_string($_COOKIE['username']);
$dir="towns/$tname/";
$path="towns/$tname/$user.php" ;
$path2="towns/$tname/index.php" ;
$path3="towns/$tname/config.ini" ;
$default= "default/user.php" ;
$default2= "default/index.php" ;
$default3= "default/config.ini" ;

if(isset($_POST['ctown'])){

  if(!file_exists($dir)){
        $con=mysqli_connect('localhost','root','password','towns');
        $sql= "INSERT INTO users (username, town, check) VALUES ('".$user."', '".$tname."', 'created')";
        mysqli_query ($con,$sql);
        mkdir ($dir);
  if (!file_exists($path)) {
        copy ($default, $path);
        }
  if (!file_exists($path2)) {
        copy ($default2, $path2);
        }
  if (!file_exists($path3)) {
        copy ($default3, $path3);
    }
    }
    header("Location: editor.php");
}else{
echo 'You have accessed this page incorrectly!';
}

使用
MySQL.*
连接到资源后需要选择数据库,而您没有选择。在
MySQLi\uu
中,您不需要额外的步骤。为什么您开始使用
MySQL\u*
而不是坚持使用
MySQLi\u*

问题是您在这里使用了
MySQL\u*
,但应该是
MySQLi\u*

mysqli\u connect
接受4个参数,
mysql\u connect
接受3个参数

$con=mysqli_connect('localhost','root','weed45654','towns')or die("cannot connect");
mysqli_query ($con,"INSERT INTO users
      (username, town, check)
      VALUES
      ('patey', 'test', 'created')");

不确定问题是什么,但这样做有效:

if(isset($_POST['ctown'])){
  if(!file_exists($dir)){
        $con= new mysqli('localhost','root','password','towns');
        if ($con->errno) {
        printf("Connect failed: %s\n", $con->error);
        exit();
        }
        $sql = "INSERT INTO `towns`.`users` (`username`, `town`, `check`) VALUES ('".$user."', '".$tname."', 'created')";
        $stmt = $con->prepare($sql);
        $stmt->bind_param('s', $username_value);
        $username_value = $user;
        if ($result = $stmt->execute()){
        $stmt->free_result();
        }
        else {
        echo "error";
        }
        $con->close();
      mkdir ($dir);
}
但是,即使它有效,这是最好的方法吗?
注意-这是代码中唯一更改的部分,filecheck.php

mysql\u query()是错误的,请参阅手册;查询,然后是资源。您有参数backwards学习使用调试器。此外,您的代码有安全漏洞(未被替换的
$tname
$user
组成文件系统路径),并使用了不推荐的
mysql\u
函数。@Dagon:他使用的是
mysqli\u query
(不是
mysql\u query
)参数顺序也很好。@ExpertSystem:他在不同的文件中同时使用了
mysqli\uu
mysql\u
。没有插入它的
mysql\u查询
谢谢,习惯了使用mysql,最近才开始使用mysqli,但仍然没有插入行:Sthanks,习惯了使用mysql,最近才开始使用mysqli,但它仍然没有插入行:我已经用我所做的更改编辑了我的问题