请帮帮我。这是php代码,第一个代码创建表,第二个代码将数据插入数据库,但它无法访问变量。
<?php
$con= mysqli_connect ('localhost','root','adminadmin','votes');
$save;
if(isset($_POST['save'])){
$save=$_POST['cate'];
$tl="CREATE TABLE $save (id INT NOT NULL PRIMARY KEY auto_increment,
name varchar (60) NOT NULL,
category varchar (60) NOT NULL)";
mysqli_query($con, $tl);
}else{
if (isset($_POST['submit'])){
$name=$_POST['name'];
$cate=$_POST['categ'];
$sql= "INSERT INTO $save (name, category) VALUES ('$name','$cate')";
mysqli_query($con, $sql);
}
}
?>
<form method="POST" action="try.php">
<label>Enter category</label>
<input type="text" name="cate" ></input>
<button type="submit" name="save">save</button>
</form>
<form method="POST" action="try.php">
<label>Enter name</label>
<input type="text" name="name" ></input>
<label>Enter category</label>
<input type="text" name="categ" ></input>
<button type="submit" name="submit">save</button>
</form>
提前谢谢
1条答案
按热度按时间hgqdbh6s1#
if/else子句只允许创建表(如果第一个条件适用),
else
将包含数据插入,但不会执行。。。