我想创建一个寄存器php,它将检查表用户是否存在,如果没有则创建它,如果它确实存在,那么它会将POST数据放在表中,然后将其显示在表中的页面上。但是我得到了这个错误
<?php
$host = "[RETRACTED]" ;;
$pass = "[RETRACTED]" ;
$link = mysql_connect ( $host , $user , $pass );
if (! $link ) {
die( 'Could not connect: ' . mysql_error ());
}
$db_selected = mysql_select_db ( $db , $link );
if (! $db_selected ) {
die ( 'Can\'t use $db : ' . mysql_error ());
}
// do stuff
$query = "SELECT ID FROM `users` ";
$result = mysql_query($link, $query);
if(empty($result)) {
$query = "CREATE TABLE `users`(
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PERMISSION_LEVEL int,
APPLICATION_COMPLETED int,
APPLICATION_IN_PROGRESS int,
PRIMARY KEY (ID)
)";
$result = mysql_query($link, $query);
}
mysql_query("insert into `users` (username, password)
values
('$_POST[username]','$_POST[password]')")
or die(mysql_error());
echo "Done!!!!";
$result = mysqli_query($con,"SELECT * FROM `users`");
echo "<table border='1'>
<tr>
<th>Username</th>
<th>Password</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
}
echo "</table>";
$filename = "users/$userName.txt";
$dirname = dirname($filename);
if (!is_dir($dirname))
{
mkdir($dirname, 0755, true);
}
$myfile = fopen("$filename", "w");
fclose($myfile);
mysql_close ( $link );
?>
答案 0 :(得分:1)
而不是
$query = "SELECT ID FROM `users` ";
$result = mysql_query($link, $query);
if(empty($result)) {
$query = "CREATE TABLE `users`(
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PERMISSION_LEVEL int,
APPLICATION_COMPLETED int,
APPLICATION_IN_PROGRESS int,
PRIMARY KEY (ID)
)";
$result = mysql_query($link, $query);
}
用于检查表是否存在并创建表
$query = "CREATE TABLE IF NOT EXISTS `users`(
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PERMISSION_LEVEL int,
APPLICATION_COMPLETED int,
APPLICATION_IN_PROGRESS int,
PRIMARY KEY (ID)
)";
$result = mysql_query($link, $query);