我有一个项目来创建一个sql表并删除并列出所有表。我尝试用php创建sql表并使用下面的代码但不起作用。实际上我的错可能太容易了。在一个while循环我可以创建html表单但无法从form获取信息。它有一个$ sql:CREATE TABLE oldumu4(PRIMARY KEY(field1));错误:无法创建表:一个表必须至少有一列。< / p>
<?php
$number=$_POST["number"];
$num=(int)$number;
$i=1;
$conn=mysql_connect("","","");
$sql="CREATE TABLE oldumu4(";
while ($i<=$num ){
echo "please enter fieldname $i";
$fieldname = "field" . $i;
?>
<form name="X" action="<?php $_PHP_SELF ?>" method="POST">
<input type="text" name="<?php echo htmlspecialchars($fieldname);?>">
<input type="submit" name="field2" id="field2">
</form>
<?php
$fieldNow=$_POST[$fieldname];
echo $fieldNow;
$sql=$sql . $fieldname . " VARCHAR(30) NOT NULL, ";
$i=$i+1;
}
?>
<form name="X" action="<?php $_PHP_SELF ?>" method="POST">
<input type="submit" name="field3" id="field3">
</form>
<?php
if(isset($_POST["field3"])){
$sql=$sql . $_POST["field1"];
$sql=$sql . "PRIMARY KEY ( field1 )); ";
echo $sql;
mysql_select_db( 'TUTORIALS' );
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
mysql_close($conn);
}
?>