我是PHP的编程全新,在w3schools上学习这门课程,我正在尝试执行以下操作,数据库被创建但是表格没有,插入也没有.
<form action="sql.PHP" method="post">
<br> Name: <br>
<input type="text" name="pName">
<br> Age: <br>
<input type="text" name="pAge">
<br> Position <br>
<input type="text" name="position">
<br> Goals: <br>
<input type="text" name="goals">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?PHP
if(isset($_POST['submit'])) {
$name = $_POST['pName'];
$age = $_POST['pAge'];
$position = $_POST['position'];
$goals = $_POST['goals'];
echo $name . $age . $position . $goals;
$con = new MysqLi('127.0.0.1', 'root', '');
$sql = 'CREATE DATABASE Arsenal;';
$sql1 = 'CREATE TABLE Players (Name varchar(225) NOT NULL, Age int(11) NOT NULL, Position varchar(225) NOT NULL, Goals int(11) NOT NULL);';
$sql2 = "INSERT INTO players ( Name, Age, Position, Goals) VALUES ( \$name, \$age, \$position, \$goals )";
MysqLi_select_db($con, 'Arsenal');
if(MysqLi_query($con, $sql)) {
echo 'success';
}
if(MysqLi_query($con, $sql1)) {
echo 'success';
}
if(MysqLi_query($con, $sql2)) {
echo 'success';
}
}
解决方法:
您错误放置了MysqLi_select_db(),并且插入查询看起来也不正确
将您的代码更改为此
if(isset($_POST['submit']))
{
$name = $_POST['pName'];
$age = $_POST['pAge'];
$position = $_POST['position'];
$goals = $_POST['goals'];
echo $name . $age . $position . $goals;
$con = new MysqLi('127.0.0.1', 'root', '');
$sql = 'CREATE DATABASE Arsenal;';
$sql1 = 'CREATE TABLE Players (Name varchar(225) NOT NULL, Age int(11) NOT NULL, Position varchar(225) NOT NULL, Goals int(11) NOT NULL);';
$sql2 = "INSERT INTO players ( Name, Age, Position, Goals) VALUES ( '$name', '$age', '$position', '$goals' )";
if(MysqLi_query($con, $sql))
{
echo 'success';
}
MysqLi_select_db($con, 'Arsenal');
if(MysqLi_query($con, $sql1))
{
echo 'success';
}
if(MysqLi_query($con, $sql2))
{
echo 'success';
}
}