0

我在这里挖掘并搜索了无数教程,但一切似乎都井井有条——我正在创建的页面做所有事情,但它就像跳过了 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!';
}
4

3 回答 3

0

不知道问题是什么,但这样做是有效的:

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

于 2013-06-01T02:23:34.033 回答
0

您需要在MySQL_*连接到资源后选择数据库,而您没有。在MySQLi_你不需要那个额外的步骤。你为什么开始使用MySQL_*而不是坚持MySQLi_???

于 2013-05-31T04:17:13.060 回答
0

问题是你mysql_*在这里使用,但它应该是mysqli_*

mysqli_connect接受 4 个参数并mysql_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')");
于 2013-05-31T04:18:15.737 回答