Mysql查询插入行不插入行

时间:2013-05-31 04:06:34

标签: 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>
<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>

然后创建一个page- 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!';
}

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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')");

答案 2 :(得分: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